digital filter

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hi Group,

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




Re: digital filter
Quoted text here. Click to load it

Try executing, at some interval,

  filtered = (filtered + newsample) / 2.

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.
Re: digital filter

Quoted text here. Click to load it

I like having a time constant:
  filtered = (0.9 * filtered + 0.1 * newsample)

Play around with it, it's not hard to get useful filtering.

Kelly



Re: digital filter

Quoted text here. Click to load it

A very slight modification of this may be useful in some cases:

The equation above is in the form:

    filtered = (1-k) * filtered + k * newsample

This can be written:

    filtered = filtered + k * (newsample - filtered)

Mathematically, these are identical. The second one does have some
practical advantages, though:

1. with small k (long decay time) and limited calculation
   precision the round-off errors are smaller
2. with non-constant k there is no need for 1-k

Computationally, the relative cost of these two ways depends on
the relative cost of multiplication and addition (2 mult/1 add
vs. 1 mult/2 add).

- Ville

--
Ville Voipio, Dr.Tech., M.Sc. (EE)

Re: digital filter
I believe this is the one I remember:

Quoted text here. Click to load it

I picked k to be a power of 2 so that a shift instruction could be
used for speed. I try these out.

Thanks to all!
Doug

Quoted text here. Click to load it



Re: digital filter

Quoted text here. Click to load it

   Yes, it's under "advanced search" on this webpage:
http://groups.google.com

Quoted text here. Click to load it

-----
http://mindspring.com/~benbradley

Re: digital filter
Hummm. That's where I looked first. I try again.

Doug

Quoted text here. Click to load it
to a
single
it



Re: digital filter
On Wed, 1 Oct 2003 13:59:09 -0400, the renowned "Doug Dotson"

Quoted text here. Click to load it

Here, and this includes my contribution to the thread:

http://www.google.com/groups?q=filter+group :*embedded*+author:%22Doug+Dotson%22&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=W3Bt8.16024%24A%253.132932%40ord-read.news.verio.net&rnum=1


Best regards,
Spehro Pefhany
--
"it's the network..."                          "The Journey is the reward"
snipped-for-privacy@interlog.com             Info for manufacturers: http://www.trexon.com
We've slightly trimmed the long signature. Click to see the full one.
Re: digital filter
That's the one! Thanks!

Doug

Quoted text here. Click to load it
http://www.google.com/groups?q=filter+group :*embedded*+author:%22Doug+Dotson%22&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=W3Bt8.16024%24A%253.132932%40ord-read.news.verio.net&rnum=1
Quoted text here. Click to load it
http://www.trexon.com
Quoted text here. Click to load it
http://www.speff.com



Re: digital filter

Here is the very simplest digital lowpass:

Y += (X-Y)*k

X = input, Y = output

Here is how k is defined:

k = 1 - exp(-Fc/Fdis))

where Fc = cutoff  frequency, Fdis = sample rate

Vladimir Vassilevsky

DSP and Mixed Signal Design Consultant

http://www.abvolt.com


Doug Dotson wrote:
Quoted text here. Click to load it

Re: digital filter
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

Quoted text here. Click to load it
to a
single
Used it



Re: digital filter

Quoted text here. Click to load it

I presume you mean "y += (x - y) >> p;" where "p" is the power rather
than n (e.g., 3 rather than 8).

But beware...

Quoted text here. Click to load it

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.

Re: digital filter
Quoted text here. Click to load it

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 <limits.h>

unsigned int avg = INT_MAX;
unsigned int sig;
int          input;

#define  FACTOR 3

....

   sig = (unsigned)input + INT_MAX)
   avg += (sig - avg) >> FACTOR;

which also avoids triggering UB via integer overflows.

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.
Re: digital filter
Oops! You ae correct.

I didn't include all the contect of my application. 12 bit positive values,
32 bit words.
I've been doing this programming thing for a while :)

Doug


Quoted text here. Click to load it



Site Timeline