School/□ Systems Engineer/Week 22
The moving average, seen in frequency
Averaging the last four readings smooths a sensor beautifully. So why does it completely erase one particular frequency?
About 35 min · 3 goals · Checked by automated tests
1.Why should I care?
The moving average is the most used filter in embedded code: one line, no multiplications. Its frequency response explains both its strength (it kills noise and specific interference) and its weaknesses (lag and leaky high frequencies), so you can choose M on purpose instead of by trial.
- ◇ Compute the output of an M-point moving average.
- ◇ Use |H(f)| = |sin(πMf/fs)/(M·sin(πf/fs))| and find its nulls at multiples of fs/M.
- ◇ Relate window length to smoothing and lag of (M − 1)/2 samples.
2.Predict
A 4-point moving average runs at 1 kHz. What happens to a 250 Hz sine?
Guess first. Being wrong here is how the surprise works.
3.Discover
Choose 'Moving average', low-pass. Set M = 4 and slide the test frequency to find the null. Change M to 8 and 2: where do the nulls go? Watch the step response: how many samples does it take to reach the top?
Loading the interactive…
Can't use the interactive? Read a description instead
In the explorer, the 4-point moving average has gain 1 at 0, falls to about 0.77 at f/fs = 0.1, and reaches 0 at f/fs = 0.25 (a null), with a small bump after it. With M = 8 the first null moves to 0.125. The step response climbs in M equal steps to 1.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
A moving average with M = 10 runs at fs = 500 Hz. What is the lowest frequency it removes completely, in Hz?
Practice 3
How many samples of delay does a 9-point moving average add?
5.Understand
One line of code
y[n] = (x[n] + x[n−1] + … + x[n−M+1])/M. It is a finite impulse response (FIR) filter: an impulse affects exactly M outputs, then is forgotten. Random noise of standard deviation σ comes out with σ/√M, because independent errors partly cancel.
Nulls and side lobes
Its gain is 1 at DC and falls to exactly zero at fs/M, 2fs/M, …, with bumps (side lobes) in between: the first is about 0.22 for large M. Great for removing a known interference, like 50 Hz mains when fs/M = 50 Hz, but a mediocre general low-pass because the side lobes leak.
The price: lag
The output is centred (M − 1)/2 samples in the past. With M = 9 at 100 Hz that is 40 ms: fine for a thermometer, dangerous inside a fast control loop where delay erodes stability. Doubling M halves the noise power again but doubles the lag.
Explain it another way
Averaging the last few readings irons out quick jitters but makes the reading arrive late, like a crowd repeating what the person at the front said.
FIR filter with M equal taps of 1/M. Linear phase (constant delay), always stable, frequency response a periodic sinc ('Dirichlet kernel').
|H(f)| = |sin(πMf/fs)/(M·sin(πf/fs))|. Example: M = 4, f/fs = 0.1: sin(0.4π)/(4·sin(0.1π)) = 0.951/(4 × 0.309) ≈ 0.769.
Digital multimeters often average over exactly one mains period (20 ms in the UK), placing a null at 50 Hz and every harmonic, which is why they read steadily next to noisy wiring.
Derivation: The moving-average frequency response
Input x[n] = e^{jωn} with ω = 2πf/fs. Output y[n] = (1/M)·Σ_{k=0}^{M−1} e^{jω(n−k)} = e^{jωn}·H, with H = (1/M)·Σ e^{−jωk}.
That is a geometric series: H = (1/M)·(1 − e^{−jωM})/(1 − e^{−jω}).
Factor out half-angles: (1 − e^{−jθ}) = e^{−jθ/2}·2j·sin(θ/2).
So |H| = |sin(ωM/2) / (M·sin(ω/2))| = |sin(πMf/fs)/(M·sin(πf/fs))|, with a pure delay of (M − 1)/2 samples.
It is 1 at f = 0 and 0 whenever Mf/fs is a whole number (other than multiples of M): nulls at f = fs/M, 2fs/M, …
Worked example 1
A 5-point moving average filters x = [2, 4, 6, 8, 10, 12]. Find y[4] and y[5].
y[4] = (2 + 4 + 6 + 8 + 10)/5 = 6. y[5] = (4 + 6 + 8 + 10 + 12)/5 = 8. A ramp comes out as the same ramp delayed by (5 − 1)/2 = 2 samples.
Common mix-ups
- A longer moving average is always better.
✓ It smooths more but lags more; inside a control loop the lag can cause oscillation. - A moving average blocks all frequencies above its cut-off.
✓ Its side lobes let some high frequencies leak through; it is only zero at the nulls.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A 2-point moving average y[n] = (x[n] + … + x[n−1])/2 filters x = [8, 2, 1, 8, 9, 0, 9] (n = 0…6). What is y[1]?
Practice 4 · a fresh version every time
An M = 4 point moving average runs at fs = 8000 Hz. Using |H(f)| = |sin(πMf/fs) / (M·sin(πf/fs))|, what is its gain at f = 2000 Hz? Give 3 decimal places.
Practice 5
Noise on a sensor is random with standard deviation 4 mV. Roughly what is it after a 16-point moving average?
7.Apply it
Your rover's 200 Hz IMU picks up 50 Hz interference from a motor driver. Choose M so a moving average nulls 50 Hz, compute its lag in milliseconds, and say whether that lag is acceptable for a balance controller that must react within 20 ms.
8.Reflect
Concepts: Digital filters and frequency response · Noise and filtering