The moving average and the noise–lag trade-off
Average the last 20 readings and the jitter melts away. So why does the robot now brake late?
About 35 min · 3 goals · Checked by automated tests
1.Why should I care?
The moving average is the first filter almost every engineer writes, and it teaches the deepest rule of filtering: you cannot remove noise for free. Smoother means slower, and for a robot slower can mean a crash.
- ◇ Compute the output of a k-sample moving-average filter.
- ◇ Explain that a wider window reduces noise (about √k) but adds lag (about (k − 1)/2 samples).
- ◇ Choose a window for a stated noise and response requirement.
2.Discover
Use the Step signal and hide the EMA and median. Try windows of 1, 5, 15 and 25. For each, write down the 'noise left' and the 'lag' from the readout. Plot the two numbers against each other in your notes: what shape does the trade-off make?
Loading the interactive…
Can't use the interactive? Read a description instead
The filter tuner plots 120 noisy samples of a step from 0 to 1 at sample 40. A moving average with k = 1 follows the noise exactly; with k = 5 noise drops by about √5 and the step takes about 5 samples to rise; with k = 25 the line is very smooth but takes about 23 samples to reach 90% of the step.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
Readings 4, 8, 6, 10. What does a k = 2 moving average output now?
4.Predict
A distance reading steps suddenly from 0 to 1. With a 9-sample moving average, how many samples until the output reaches 1?
Guess first. Being wrong here is how the surprise works.
5.Understand
How it works
A moving average of window k outputs the mean of the most recent k readings: yₙ = (xₙ + xₙ₋₁ + … + xₙ₋ₖ₊₁) ÷ k. Each new reading pushes the oldest out. It is cheap: keep a running sum, add the new value, subtract the one leaving.
Noise falls like √k
If the noise in each reading is independent with standard deviation σ, averaging k of them leaves about σ/√k. A window of 4 halves the noise; 16 quarters it. Noise that is not independent (a slow drift, a vibration at a steady frequency) is reduced less, or not at all.
Lag grows like k
The output describes the average of the last k samples, centred about (k − 1)/2 samples in the past. After a sudden change it takes k samples to catch up fully. At 100 samples a second, a 21-sample window responds about 0.1 s late, which at 2 m/s is 20 cm.
Explain it another way
Judging the weather from the last week's average temperature is steady but slow to notice a sudden cold snap. Judging from today alone is quick but jumpy.
The moving average is a finite impulse response (FIR) filter with k equal weights 1/k. Its group delay is (k − 1)/2 samples, and for white noise its output variance is σ²/k.
Var(yₙ) = Var((1/k)Σxᵢ) = (1/k²)·k·σ² = σ²/k, so the noise standard deviation is σ/√k. For a unit step, yₙ = min(n + 1, k)/k: a straight ramp lasting k samples.
Battery monitors often use long moving averages because charge changes slowly and smooth numbers matter more than speed; obstacle sensors use short ones because a late answer is worse than a noisy one.
Worked example 1
Readings: 12, 15, 11, 18, 14. Window k = 3. What is the latest output?
Mean of the last three: (11 + 18 + 14) ÷ 3 = 43 ÷ 3 ≈ 14.33.
Common mix-ups
- A bigger window is always better because it is smoother.
✓ Every extra sample of smoothing is extra delay; the right window depends on how fast the signal really changes. - A moving average removes all noise eventually.
✓ It reduces independent noise by √k and leaves slow drifts and bias untouched.
6.Watch
→ Watch the first part, where a moving average is drawn as a sliding window of weights. This week's filter and next week's image blur turn out to be the same operation.
One idea, many uses: moving averages, image blurs and probability all turn out to be the same sliding multiply-and-add.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A filter keeps the mean of the last k = 5 readings. The readings so far, oldest first, are: 35, 16, 15, 35, 38, 10. What does the filter output now?
Practice 3 · a fresh version every time
A filter keeps the mean of the last k = 5 readings. The readings so far, oldest first, are: 26, 28, 27, 23, 15, 12, 32. What does the filter output now?
Practice 4
Raw noise is σ = 6 mm. Roughly what noise remains after a 9-sample moving average (independent noise)?
Practice 5
A 21-sample moving average runs at 50 samples per second. Using a delay of (k − 1)/2 samples, how late is its output, in seconds?
Practice 6
An obstacle sensor must react within 0.05 s at 100 samples/s. Which window is the largest you could use?
8.Apply it
Write movingAverage(readings, k) in Python using a running sum (no re-adding the whole window). Test it on a step with noise and measure, in samples, the noise left and the 90% rise time for k = 3, 9 and 27. Compare with the diagram.
9.Reflect
Concepts: Noise and filtering · Describing data