About a year and a half ago someone (several someones actually) replied to a request for a simple digital low pass filter. I boiled it down to a single line of C, tested it and it worked great. Since then I have lost it. Used it to filter A/D readings. Anybody have this design handy?
Thanks! Doug
Didn't find your answer? Ask the community — no account required.
"it's the network..." "The Journey is the reward"
speff@interlog.com Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog Info for designers: http://www.speff.com
Right! I picked n to be a power of 2 (8 actually in my application) and in C it turns into:
y += (x - y) >> n;
Works great!
Doug
to a
single
Used it
D
Dave Hansen
I presume you mean "y += (x - y) >> p;" where "p" is the power rather than n (e.g., 3 rather than 8).
But beware...
On your platform. This revision.
There are no guarantees that right shifting a signed value will yield the result you desire. You might get zeros shifted in regardless of the actual sign of the value being shifted.
Most compilers are smart enough to make the "shift to divide by a power of two" optimization on their own. The following
y += (x - y) / n;
will be guaranteed to work, and should produce nearly identical code on your system.
Regards,
-=Dave
Change is inevitable, progress is not.
C
CBFalconer
If it doesn't, and the speed turns out to be critical, he could use unsigned ints and a bias, such as INT_MAX. The input y would have to first be corrected to INT_MAX + yin, where yin is int and y is unsigned. The filtered y would need to be initialized to INT_MAX also. This assumes that INT_MAX is roughly 1/2 of UINT_MAX.
So the result would be, more or less:
#include
unsigned int avg = INT_MAX; unsigned int sig; int input;