MorganS:
The moving average is a GREAT filter. See this filter comparison
I would disagree with the conclusion of that comparison.
An EMA (single-pole recursive filter) can be implemented much more efficiently if the pole lies a negative power of 2 away from one, and requires just one word of memory.
The SMA on the other hand requires N words of memory, and requires expensive integer divisions if N is not a power of 2. Even when N is a power of 2, it's still slightly slower than the EMA.
I do not get his point about development time, the EMA algorithm is trivial, it's just a straightforward implementation of the difference equation, and the SMA requires a bit of fiddling with circular buffers, but it's pretty straightforward as well.
Here are the speed differences between some of the different filters mentioned here, as tested on an Arduino Leonardo:
[color=#000000]EMA[/color] [color=#000000]([/color][color=#000000]K[/color] [color=#434f54]=[/color] [color=#000000]4[/color][color=#000000])[/color]
[color=#000000]358731.53[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
[color=#000000]EMA[/color] [color=#000000]([/color][color=#000000]K[/color] [color=#434f54]=[/color] [color=#000000]6[/color][color=#000000])[/color]
[color=#000000]297867.28[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
[color=#000000]EMA[/color] [color=#000000]([/color][color=#000000]K[/color] [color=#434f54]=[/color] [color=#000000]8[/color][color=#000000])[/color]
[color=#000000]657375.75[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
[color=#000000]SMA[/color] [color=#000000]([/color][color=#000000]N[/color] [color=#434f54]=[/color] [color=#000000]16[/color][color=#000000])[/color]
[color=#000000]162951.37[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
[color=#000000]SMA[/color] [color=#000000]([/color][color=#000000]N[/color] [color=#434f54]=[/color] [color=#000000]10[/color][color=#000000])[/color]
[color=#000000]51198.03[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
[color=#000000]SMA[/color] [color=#000000]([/color][color=#000000]N[/color] [color=#434f54]=[/color] [color=#000000]100[/color][color=#000000])[/color]
[color=#000000]53278.78[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
[color=#000000]Median[/color] [color=#000000]even[/color] [color=#000000]([/color][color=#000000]N[/color] [color=#434f54]=[/color] [color=#000000]10[/color][color=#000000])[/color]
[color=#000000]16338.37[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
[color=#000000]Median[/color] [color=#000000]odd[/color] [color=#000000]([/color][color=#000000]N[/color] [color=#434f54]=[/color] [color=#000000]11[/color][color=#000000])[/color]
[color=#000000]17778.92[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
[color=#000000]Median[/color] [color=#000000]even[/color] [color=#000000]([/color][color=#000000]N[/color] [color=#434f54]=[/color] [color=#000000]100[/color][color=#000000])[/color]
[color=#000000]2162.23[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
[color=#000000]Median[/color] [color=#000000]odd[/color] [color=#000000]([/color][color=#000000]N[/color] [color=#434f54]=[/color] [color=#000000]101[/color][color=#000000])[/color]
[color=#000000]2658.79[/color] [color=#000000]filter[/color] [color=#000000]operations[/color] [color=#000000]per[/color] [color=#000000]second[/color]
Observations:
The EMA is much faster than the EMA, even if N is a power of two.
If K = 4, the EMA is very fast, because the AVR instruction set has an instruction to swap nibbles in a byte.
If K = 8, the EMA is even faster, because this just corresponds to removing the least significant byte.
Otherwise, multiple bit shift instructions are used, which is slower.
The SMA with N a power of two is significantly faster than SMA with N not a power of two, because the division by N can then be implemented as a series of bit shifts. (It should be noted that the library uses special code for signed integers, otherwise the compiler can't optimize it.)
Making N 10 times larger doesn't really impact the performance.
The median filter is a bit slower, because each step involves copying and partially sorting the ring buffer. As N increases, the performance drops, because the sorting algorithm used has an average complexity of O(N).
Odd N is faster, as the median requires only one middle element, when N is even, you have to find the two middle elements, which requires N/2 extra comparisons.
Filters used:
EMA Filter
SMA Filter
Median Filter