Running median

Hello,

I am having problems using the Running Median code.

#include <RunningMedian.h>

// RunningMedian samples = RunningMedian(9);
RunningMedian samples = RunningMedian(5);

void setup()
{
  Serial.begin(9600);
  Serial.print("Running Median Version: ");  
  Serial.println(RUNNINGMEDIANVERSION);
}

void loop()
{
  test1();
}

void test1()
{
  int x = analogRead(A0);
  samples.add(x);
  long l = samples.getLowest();
  long m = samples.getMedian();
  long a = samples.getAverage();
  long h = samples.getHighest();
  Serial.print(millis());
  Serial.print(" ");  
  Serial.print(x);
  Serial.print(" ");
  Serial.print(l);
  Serial.print(" ");
  Serial.print(a);
  Serial.print(" ");
  Serial.print(m);
  Serial.print(" ");
  Serial.println(h);
  delay(100);
}

error: ‘RunningMedian’ does not name a type
cpp: In function ‘void setup()’:

I thought that I might have to import the library but I can’t seem to find it anywhere. The solution might be really simple but I am at a loss.

Thanks for your help!

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Just like the compiler, I have no idea what RunningMedium is. Where did you get this code? Does it have a downloadable library?

You need to download the running median source files into your Arduino environment. Where did you see the running median code originally?

http://www.instructables.com/id/How-to-install-Libraries-in-arduino/

I got it from here:

http://arduino.cc/playground/Main/RunningMedian

I have googled RunningMedian Library with no luck.

rebcarot: I got it from here:

http://arduino.cc/playground/Main/RunningMedian

I have googled RunningMedian Library with no luck.

They have the .h and .cpp files right there on that page, you need to put that data into new tabs in your Arduino IDE.

They have the .h and .cpp files right there on that page, you need to put that data into new tabs in your Arduino IDE.

What do you mean by tab? And I don't know what you mean by IDE...

Thanks again.

So I added the .cpp/.h files to the Arduino libraries. Now I am getting this error when I try and compile ..

Applications/Arduino.app/Contents/Resources/Java/libraries/RunningMedian/RunningMedian.cpp:109: error: expected unqualified-id before '[' token

Most likely when you "copied" that code from the internet you got a html formatted page rather than a text file.

And I don't know what you mean by IDE...

Neither do Arduino...

rebcarot: What do you mean by tab? And I don't know what you mean by IDE...

Thanks again.

A quick Google of "arduino tab":

http://liudr.wordpress.com/2011/02/16/using-tabs-in-arduino-ide/

Now I am getting the error

RunningMedian.cpp:16: error: ‘template<class T, int N> class RunningMedian’ used without template parameters
RunningMedian.cpp:16: error: ISO C++ forbids declaration of ‘RunningMedian’ with no type
RunningMedian.cpp: In function ‘int RunningMedian(uint8_t)’:
RunningMedian.cpp:16: error: ‘int RunningMedian(uint8_t)’ redeclared as different kind of symbol
RunningMedian.h:16: error: previous declaration of ‘template<class T, int N> class RunningMedian’
RunningMedian.cpp:18: error: ‘_size’ was not declared in this scope
RunningMedian.cpp:18: error: ‘MEDIAN_MIN’ was not declared in this scope
RunningMedian.cpp:18: error: ‘MEDIAN_MAX’ was not declared in this scope
RunningMedian.cpp:18: error: ‘constrain’ was not declared in this scope

I have the .cpp/.h files from

http://arduino.cc/playground/Main/RunningMedian

in two separate tabs with the sample code from the same place.

Thanks.

There are two different options on that page. The first one uses int as the data type, the other one uses templates allowing you to choose the input type. Each option has a separate sample program, so you need to make sure you are not mixing and matching sample programs with .h/.cpp files.

majenko:

And I don't know what you mean by IDE...

Neither do Arduino...

LOL, totally agree ;)

and more to the point what is supposed to be the use of running median??

here is an plot of the frequency response of the ever popular data distorter called running-mean and that of a clean filter : gaussian.

http://oi41.tinypic.com/nevxon.jpg

Here is the the result of those two filters on the same data (though you would not know it) http://i44.tinypic.com/351v6a1.png

Now what the result of the running median is or what the hell use it is I can't imagine.

RUNNING MEAN MUST DIE.

ardnut:
and more to the point what is supposed to be the use of running median??

here is an plot of the frequency response of the ever popular data distorter called running-mean and that of a clean filter : gaussian.

http://oi41.tinypic.com/nevxon.jpg

Here is the the result of those two filters on the same data (though you would not know it)
http://i44.tinypic.com/351v6a1.png

Now what the result of the running median is or what the hell use it is I can’t imagine.

RUNNING MEAN MUST DIE.

You seem to be using mean and median interchangeably. Also, those pictures don’t make any sense.

I’ve used a running median to filter out bad data while using one of those RC transmitters that outputs a servo pulse.

hm runing median, if thats your only interest, then i supose this will do too.

in a loop :

read sx // by whatever means of reading data x = (x*8 + sx) /9 // 8 times previous plus new reading divided by 9 is runing median (an average of the last 9 measurements)

PGTBOOS: hm runing median, if thats your only interest, then i supose this will do too.

in a loop :

read sx // by whatever means of reading data x = (x*8 + sx) /9 // 8 times previous plus new reading divided by 9 is runing median (an average of the last 9 measurements)

What you provide is a running mean, not a running median. Both mean and median are averages or measures of central tendency, but they are not indicators of the same thing. Indeed if the data distribution is significantly non gausian (bell curve) than the two values can be significantly different. Running means are easy, running medians require the storage of however many samples one wishes for the median calculation... When storing those values, and insertion sort is the most efficient way, since the median will then be the value at sample size divided by 2. So for a 9 sample running median the median will be the value stored at position 5 (assuming they are sorted).

WizenedEE:

ardnut: and more to the point what is supposed to be the use of running median??

here is an plot of the frequency response of the ever popular data distorter called running-mean and that of a clean filter : gaussian.

http://oi41.tinypic.com/nevxon.jpg

Here is the the result of those two filters on the same data (though you would not know it) http://i44.tinypic.com/351v6a1.png

Now what the result of the running median is or what the hell use it is I can't imagine.

RUNNING MEAN MUST DIE.

You seem to be using mean and median interchangeably. Also, those pictures don't make any sense.

I've used a running median to filter out bad data while using one of those RC transmitters that outputs a servo pulse.

No, your are simply not reading what I wrote.

I showed the distortion produced by running mean , then stated that I did not know what the effect of running median would be.

It will not even be definable in such terms because it is determined by order and not magnitude. In short it will be an awful filter of unstable and unpredictable qualities. That's probably why no one has ever heard of it !

I've used a running median to filter out bad data while using one of those RC transmitters that outputs a servo pulse.

Well, you could use a median to eliminate glitches but what running-median would do to the rest of the data is anyone's guess.

If you can read the frequency filter I posted for running mean you will realise how bad it is as a filter with all those huge stop band lobes letting what you thought you filtered out get through.

The other plot shows you how running mean can distort peaks left and right , even invert them, depending on how the variations compare to width of the running mean window. Running median will be all of that plus other non predictable distortions that will likely be as big or bigger.

Sure the results are "smoother" . Don't confuse that with thinking you have correctly filtered you data.

Most people use R-Mean through ignorance. A few know the problems but ignore them because it's easy to calculate.

That is why I say RUNNING MEAN MUST DIE.

Once I get up to speed on programming Ard' I will post a code example for a proper filter like gaussian that does something like what most people imagine running mean does.

For you RC example, if you have known glitches in the data it may be better to eliminate them directly then filter the remaining data.

Gaussian and binomial filters are efficient and calculated just like a weighted running mean. You pre-calculate a weight for each point in the window in setup(). Since Ard' has fast on chip multiplier this will probably be quicker that the sorting algo for median.

ardnut: No, your are simply not reading what I wrote.

I showed the distortion produced by running mean , then stated that I did not know what the effect of running median would be.

Oh, I hope you'll excuse my confusion -- nobody had yet mentioned running mean and then you started talking about why it was terrible out of the blue.

I've used a running median to filter out bad data while using one of those RC transmitters that outputs a servo pulse.

Well, you could use a median to eliminate glitches but what running-median would do to the rest of the data is anyone's guess.

Not really. It will delay any sudden changes a few milliseconds, but nothing else.

The data were mostly centered on one value, with a bunch of points above the value and some below it. So, I used a variation on the library mentioned and modified it to take the first quartile of the data, which seemed to give me consistent results.

If you can read the frequency filter I posted for running mean you will realise how bad it is as a filter with all those huge stop band lobes letting what you thought you filtered out get through.

I think the discrepancy is that you're trying to take a Fourier transform of an AC signal, while the people in this thread just wanted to ignore some values that were obviously too high or too low.