Re: Efficient Moving Average and Moving Variance Calculations



On May 19, 12:28 am, "John E. Hadstate" <jh113...@xxxxxxxxxxx> wrote:
Steven Smith in "Digital Signal Processing" describes an
efficient algorithm for computing a moving average. This
algorithm is also mentioned in the Wikipedia article describing
Moving Average:

http://en.wikipedia.org/wiki/Moving_average

Rick Lyons once asked in this newsgroup about an efficient
algorithm for computing "moving variance":

http://groups.google.com/group/comp.dsp/browse_frm/thread/330ac90a92f...

With minimal effort, one can modify the "Moving Average"
algorithm to efficiently compute a "Moving Variance" and a
"Moving Average" simultaneously at each time step.

Notice that one of the equations for computing a sample
variance over a window with N samples can be written as:

N * Sum(X**2) - (Sum(X)**2)
V = ---------------------------
N * (N - 1)

where X is the input.

To implement this efficiently, allocate two history buffers,
one for values of X and one for values of X**2, each containing
room for N points. These buffers need to be initialized,
perhaps to the first sample of X and X**2 or perhaps to zero,
your choice. Then initialize two variables, SX1 to be the
Sum(elements in X history buffer) and SX2 to be Sum(elements in
X**2 history buffer).

Then, at each time-step k, compute:

X1 = (new sample value)
X2 = X1 * X1

Y1 = (oldest X1 value from X1 history buffer)
Y2 = (oldest X2 value from X2 history buffer)

Overwrite oldest value in X1 history buffer with X1.
Overwrite oldest value in X2 history buffer with X2.

SX1(k) = SX1(k-1) + X1 - Y1
SX2(k) = SX2(k-1) + X2 - Y2

Calculate Moving Average:
M = SX1 / N

Calculate Moving Variance:
V = (N * SX2 - (SX1 * SX1)) / (N * (N - 1))

A simpler way is to do this....

s(k+1)=beta*s(k)+(1-beta)*u^2(k)


where beta is a forgetting factor less than unity.

u is the input and s is the variance.

The correct equation for recursive variance can be found by addoing an
extra sample to batch variance

s(k+1)=s(k)+(1/(k+1)[s(k)-u^2(k)]

if I rememebr right which converges for stationary u when k->
infinity. No use for tracking though - you need the forgetting factor
version or a moving window as has been explained.

There is one more method which estimated the inverse of variance
should you need that one!

K.
.



Relevant Pages

  • Efficient Moving Average and Moving Variance Calculations
    ... efficient algorithm for computing a moving average. ... Overwrite oldest value in X1 history buffer with X1. ...
    (comp.dsp)
  • Non stationary process feature extraction
    ... Is there a known process for normalizing the variance of a ... multivariate function using a moving average? ...
    (sci.stat.math)
  • Re: Moving average filter ...
    ... - is this a valid moving average algortithm? ... - does this algorithm have a name? ... not 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. ...
    (comp.arch.embedded)
  • Re: Moving average filter ...
    ... Algortihm is; ... - is this a valid moving average algortithm? ... - does this algorithm have a name? ... "Applied Control Theory for Embedded Systems" gives you just what it says. ...
    (comp.arch.embedded)
  • Re: Moving average filter ...
    ... This is not the same thing as the moving average, although it is certainly useful algorithm. ... void MovingAvg ... - is this a valid moving average algortithm? ...
    (comp.arch.embedded)

Loading