Re: Moving average filter ...
- From: Vladimir Vassilevsky <antispam_bogus@xxxxxxxxxxx>
- Date: Thu, 30 Oct 2008 07:51:26 -0500
It looks like you are trying to invent a 1-st order exponential averager:
y += (x - y) >> shift
This is not the same thing as the moving average, although it is certainly useful algorithm.
Vladimir Vassilevsky
DSP and Mixed Signal Design Consultant
http://www.abvolt.com
group@xxxxxxxxxxxxxx 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
- Follow-Ups:
- Re: Moving average filter ...
- From: Meindert Sprang
- Re: Moving average filter ...
- References:
- Moving average filter ...
- From: group
- Moving average filter ...
- Prev by Date: Re: Fate of PIC32 If Microchip buys Atmel MPU business.
- Next by Date: Re: Moving average filter ...
- Previous by thread: Moving average filter ...
- Next by thread: Re: Moving average filter ...
- Index(es):
Relevant Pages
|