Lite / Intelligent Robotic Systems / Week 23
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?
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.
Predict
A distance reading steps suddenly from 0 to 1. With a 9-sample moving average, how many samples until the output reaches 1?
- 1 sample
- About 4 samples
- 9 samples
Guess first, then reveal
The window only contains all-new readings after 9 samples, so the output ramps from 0 to 1 over those 9 samples. It is halfway after about 4–5: that is the lag.
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?
What the interactive shows (text version)
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.
Watch (optional)
Watch for: 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.
“But what is a convolution?” by 3Blue1Brown (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
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
Intuitive
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.
Technical
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.
Mathematical
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.
Applied
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.
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
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.
Reflect
Where in a robot would you accept lag for smoothness, and where never?
Write your answer on paper or in your own notes. Lite never stores what you write.