Lite / Engineering Complex Systems / 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?
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.
Predict
A 4-point moving average runs at 1 kHz. What happens to a 250 Hz sine?
- It passes unchanged
- It is halved
- It is removed completely
- It is doubled
Guess first, then reveal
250 Hz has a period of exactly 4 samples, so every window holds one whole cycle, which averages to zero. Nulls sit at fs/M = 250 Hz, 500 Hz… Find the dip in the response explorer.
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?
What the interactive shows (text version)
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.
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
Intuitive
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.
Technical
FIR filter with M equal taps of 1/M. Linear phase (constant delay), always stable, frequency response a periodic sinc ('Dirichlet kernel').
Mathematical
|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.
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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.
Reflect
When would you choose a moving average over a smarter filter, even knowing its weaknesses?
Write your answer on paper or in your own notes. Lite never stores what you write.