RUNAVG:
This post demonstrates an extremely efficient algorithm for smoothing of real time data.
It is based on a recursive mechanism that I have shown here before, but here it comes in two versions
One for signed Integer data and one for single floating point data.
The method is interesting for three reasons:
1. It works "in line" so there is no need for storing data. It consumes very little RAM. Just a 32bit accumulator
2. It is very fast as no division is involved. only shifting.
3. The floating point version called RUNAVG_SING is also very fast as it too use shifting rather than division
It is not obvious that dividing a FP-number by 2^N works by simply shifting. But it does...
Check these three lines. They are the essence of the algo.
[size=9:b407757a31] Runavg_sing = Sum
Shift Runavg_sing , Right , Factor , Signed 'compute data average from accumulated values
Sum = Sum - Runavg_sing 'subtract the old average
Sum = Sum + Newval 'add the new value
[/size:b407757a31]
Both routines can be found in their respective testprograms called RUNAVG.BAs and RUNAVG_SING.BAS
You can run then in any avr-processor. I have tested them in an Arduino mega 1280.
Running in the Bascom internal compiler also works fine.
I hope you find these routines useful. I use them all the time.
Comments and improvements invited.
/Per
↧