# When should we using moving average in algorithm design?

I'm new to signal processing. And I just read G.720.1. I find there is a lot of moving average used. And I can also recall there is a lot of moving average been used in other audio processing algorithm.

For example in G.720.1, the ZCR (The zero crossing rate of the input 10ms frame) are input of moving average algorithm and the result are used for sound detect. But I assume ZCR should have big change in continues frames.

• So when will you think you need moving average in an algorithm design?
• And if we need one, how we should select the weight?

So when will you think you need moving average in an algorithm design?

If you mean moving average filters; moving averages as the name suggest are computed as averages of samples say, $M-1$ previous samples (+ current sample) from input $x[n]$ to get an average output $y[n]$, repeating the process to get all $y$ samples. Computing their mean to get an averaged $y[n]$ signal. What you get is a smoothing effect.

As explained by Sanjit Mitra in the chapter on Discrete-Time Systems in his book. When your data is corrupted with noise and you happen to have multiple measurements of the same data samples, a good estimation of your signal is doing the ensemble average. But if you only have one set of measurement, then moving averages is a way of doing it. The result is a smoothing of your noisy signal. An M-point moving average is defined as a FIR system as:

$$y[n] = \frac{1}{M}\sum_{k=0}^{M-1} x[n-k],\quad 0\leq n-k \leq M-1$$

And if we need one, how we should select the weight?

As seen in the equation, all the weights are equal and dependent on the length of the chosen filter:

$$h[n] = \frac{1}{M}$$

The choice of the length is critical. An efficient implementation however is done by introducing a recursive form and requires only 2 additions and 1 division.

Again, depending on your application, there are other variants of the moving average that may be more suited to your signals.

A moving average is simply a finite impulse response (FIR) process with a constant finite impulse response. If you feed an impulse into a moving average, you get the constant weights as the output, followed by zeroes once you're past the length of the response. The constant is the weight of the average. The impulse response looks like a rectangular step if you plot it out.

Such a process happens to act like a low-pass filter with steepest cut-off and poorest out-of-band rejection for a given finite impulse response length. It also has a comb of frequencies that are completely rejected - this might be useful if there are strong narrowband interfering signals you wish to reject. The number of rejected frequencies is proportional to M, the length of the finite response in samples. Adjusting the number of averaged samples tunes the filter, obviously with only discrete frequencies to choose from.

A moving average is cheap to compute, requiring M additions and 1 multiplication to produce one output sample. That's why it's often used if the refined control over the response of a generalized, multi-valued finite impulse response, is not needed. If it works and when it works, it's cheap.

• Good answer as this covered the salient points. To note we would often eliminate the multiplication of 1/M as well by either accepting the growth by M, or using an M that is a power of 2 such that the 1/M is accomplished with a simple bit shift. Also see dsp.stackexchange.com/questions/31548/… regarding CIC implementations of moving average filters (MAF), and dsp.stackexchange.com/questions/19584/… for how to compensate for the passband droop of a MAF with a 3 tap FIR. – Dan Boschen Jul 7 '16 at 11:32