Use a revolving buffer to store your last 16 values.
Do an initial sum from the first 16 values.
Then at each step:
if(++i>15) i=0; // get the next mafdata slot
mafsum = mafsum + data - mafdata[i]; // update running sum
mafdata[i] = data; // remember latest value in slot
data = mafsum / 16; // calculate running average
Quoting Adeilton Oliveira <adeiltonjunior@terra.com.br>:
David
> Hello.
>
> I don't know how the division operation is internally implemented, but I
> think in your case you can use >>4 instead of /16.
>
>
>
> ?????????? ????? ?????????? wrote:
> >
> > Hello!
> >
> > I using WinAVR 20090313 and ATMega32.
> > I write code for time critical application.
> > There is a moving average filter algorithm.
> >
> > unsigned char data = 0;
> > unsigned char i = 0;
> > unsigned char mafinit = 0;
> > unsigned int mafsum = 0;
> > unsigned char mafdata[16];
> >
> > if (!mafinit)
> > {
> > for (i = 0; i < 16; i++)
> > mafdata[i] = data;
> > mafinit = 1;
> > }
> >
> > for (i = 0; i < 15; i++)
> > {
> > mafdata[i] = mafdata[i+1];
> > mafsum += mafdata[i];
> > }
> > mafdata[15] = data;
> > mafsum += data;
> > data = mafsum / 16;
> >
> > Are there any ways to speedup algorithm?
> >
> > --
> > Best regards,
> > Roman Antoshchenkov
> > mailto:djantoxa@rambler.ru <mailto:djantoxa%40rambler.ru>
> >
> >
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>
>Message
Re: [AVR-Chat] Moving average filter
2009-07-18 by dpharris@telus.net
Attachments
- No local attachments were found for this message.