Re: Moving average filter ...



Tim Wescott wrote:
On Thu, 30 Oct 2008 01:14:33 -0700, group wrote:

Hello -

I'm trying to implement a 'smart' moving average algorithm, which
consumes as little memory (RAM) as possible.

Imagine two 8bits registers reg_8_hi and reg_8_lo. These two registes
can be combined into a 16bit register, reg_16.

Algortihm is;

1 - Samples are added to reg_16.
2 - High order of reg_16, reg_8_hi is subtracted from reg_16. 3 -
Average value is high order of reg_16, reg_8_hi. 4 - Registers are
preloaded with reg_8_hi = 127, and reg_8_lo = 255. Which is 'best guess'
on average.

Code:
void MovingAvg(uchar sample)
{
reg_16 += sample; //add sample

reg_8_hi = reg_16 >> 8; //get high order reg_16 -=
reg_8_hi;
//subtract high order from total

reg_8_hi = reg_16 >> 8; //new high order (average)
reg_8_lo = reg_16
& 0xFF; //new loworder
}

A statement is that # of samples is size of reg_8_lo (8 bit = 255
samples),

Questions are now:

1 - is this a valid moving average algortithm ?

2 - does this algorithm have a name ?

3 - if I need lower number of samples can this algorithm be changed to;
reg_8_hi and reg_3_lo. Which would then give me 8 samples. Reason for
this question is that if I do;

Code:
void MovingAvg(uchar sample)
{
reg_16 += sample; //add sample

reg_8_hi = reg_16 >> 3; //get high order reg_16 -=
reg_8_hi;
//subtract high order from total

reg_8_hi = reg_16 >> 3; //new high order (average)
reg_3_lo = reg_16
& 0x07; //new loworder
}

I get an error on the average calculation, which I'm not really able to
compensate for.

Any comments ?


Best regards

As Vladimir mentioned you have rediscovered the 1-st order exponentially decaying filter. This is going to respond with

out(n) = sum from k = n-7 to n (in(k) (7/8)^(k-n))


I don't think that equation is right - as you say below, he has an IIR, not an FIR (I suppose rounding will make it technically an FIR).

I like to think of these things as an approximate digital RC filter - they have a similar effect on the signals, and are just as easy to implement.


So you'll never get an exact moving average.

If you choose a shift-varying difference equation you can get an average over some interval, but you can't get a _moving_ average.

What you have is a simple case of an infinite impulse response (IIR in the literature) filter, where a moving average filter is a simple case of a finite impulse response (FIR in the literature) filter.

.



Relevant Pages

  • Re: Any help about FIR filter algorithm
    ... This will let you focus on the FIR ... algorithm instead of the data types. ... Get it to work using known float data and then you can modify it for limited ... >>basic FIR filter algorithm. ...
    (comp.dsp)
  • Re: Large moving average
    ... glen herrmannsfeldt wrote: ... taps/computation required would have made the filter impractical. ... A moving average filter's spectrum is the sinc function. ... In multi-rate systems, these are often used for adjustable decimation/interpolation ratios and large ratio systems, but they are almost invariably coupled with a low pass FIR filter with the desired passband characteristic. ...
    (comp.dsp)
  • how calculate the group delay in a FIR filter ?
    ... looking for a way to measuring the time of my filter and my algorithm ... and how can I calculate the group delay in my filters (FIR)!?? ...
    (comp.dsp)
  • Re: Moving average filter ...
    ... Algortihm is; ... - is this a valid moving average algortithm? ... - does this algorithm have a name? ... decaying filter. ...
    (comp.arch.embedded)
  • Re: Need Ideas!!
    ... The Remez exchange algorithm allows minimax solutions to approximation ... Digital filter design is one of the applications. ... the stop band (so all the zeros in the stop band are double zeros). ...
    (comp.dsp)

Loading