> Does anyone have any code to implement a median filter for a small number of > samples (say 5, 7, 9) that can operate in a "moving/rolling" fashion (i.e. > results are available after each new data point using the previous n data > points), that operates in a time and space (I know I'm asking for too much > here!) efficient way preferably using n (or less!) memory locations for n > samples. I've looked around but all of the examples I've seen so far end up > changing the order of the data set and so make it hard to discard the oldest > item in the set. What exactly to you mean by "median filter". It finds the center of the min/max range of the numbers? Or do you mean box filter which finds the average? In any case, this is not hard to do. The processing can be done without regard to the order of the samples. This means you can keep a circular buffer with a pointer to the oldest entry. A new entry overwrites the oldest, then the pointer is advanced by one. If the pointer advances past the end of the buffer, you reset it to the beginning. To do the computations, just loop thru the buffer from beginning to end, since it appears your operation doesn't depend on the order of the samples. > Note this does not have to be a perfect median filter so an efficient > implementation that has similar noise removal properties would also work for > me. A simple low pass filter usually is easier to compute, requires less state, and has better frequency characteristics. Consider FILT <-- FILT + (NEW - FILT) * FF where FILT is the filtered output and also the only persistant state, NEW is the new sample value that the filter is being updated with, and FF is the filter fraction. Useful values of FF are from 0 to 1, control the "heaviness" of the filter. FF = 0 makes the filter infinitely heavy in that its output never changes. FF = 1 disables all filtering as the filter output is just the value of the last input sample. If samples are coming at regular intervals, then FF directly controls the filter time constant or the -3dB rolloff point. Note that if FF is 1 / 2**n, then the multiply by FF can be performed with a right shift by N. Multiple filter stages can be combined to create multi-pole filters. This increases the computation required, but also increases the rolloff slope by -6dB/octave for each stage. all else being equal, I like to run real measured values thru a two pole filter before using them. ***************************************************************** Embed Inc, embedded system specialists in Littleton Massachusetts (978) 742-9014, http://www.embedinc.com -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics