Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

RE: [AVR-Chat] Sensor filtering

2005-09-11 by Larry Barello

A little more detail on sensor used, how it is read and the nature of the
noise that is troublesome and what the application is would help in
determining what, if any, filtering is needed.  

You can get only so much resolution using RC filter.  In the case of the AVR
the ADC is only 10 bits.  However, if you use an IIR filter (i.e. weighted
average, or exponential filter) you can not only reduce bandwidth and filter
out noise, but you can also increase your resolution one or two bits. The
weighted average filter is the digital version of a single stage RC filter.
A weighted average filter looks something like this:

Output = (prev_output - input)* K1 + input

Where K1 is equal to e^-(dt/tau).  Dt = the sampling period and Tau is the
rise time desired (in seconds).  The frequency response is 1/2*PI*tau.  Or,
tau = 1/2*pi*frequency.  I recently implemented this filter in the following
way:

Int32_t results;

Void InitFilter(int16_t val)
{
	Results = val * 256;
}

void DoFilter(int16_t val)
{
	int32_t t = (int32_t)val * 256;	// Turn into 24.8 fraction

	results = ((((int32_t)K1 * (results - t) + 128L) / 256L) + t);
}

uint16_t GetFilter(void)
{
	return results / 256;	// Divide by 128 or 64 for more 
}					// bits resolution...

K1 is a 8.8 binary fraction e.g.  K1 = 256 * e^-(dt/tau) 

The intermediate result is a 24.8 bit number.  I picked an 8 bit fraction
since it is so easy to convert back & forth if the compiler is clever: just
shuffle bytes.

Weighted average filters (IIR) are compact, computationally efficient and
don't require storing lots of previous values, but they have limited
frequency response & phase shift characteristics.  I recommend Smith's "The
Scientist and Engineers Guide to Digital Signal Processing" for an easy to
read tutorial on practical filter design.

Cheers!

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.