Textbooks/Signals and Systems/Chapter 5Lite (plain text) version
5Smoothing filters
Version 1 · Technically checked (automated checks only) · 3 source lessons · 8 practice items
Introduction
Noisy readings tempt you to average. A moving average of the last N samples cuts random noise but makes every change appear late. The exponential filter keeps just one number of memory and blends each new reading in with a weight α; small α means smooth and slow. A single spike can still drag an average a long way, which is when a median filter earns its place. Inside a control loop, every bit of lag has a cost.
Explanation
This chapter is built from 3 School lessons. Each part below is the lesson's own explanation; open the lesson for its predict-and-discover activities.
5.1 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?
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.
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.
Open the full lesson: The moving average and the noise–lag trade-off →
5.2 The exponential filter: one number of memory
A filter that remembers just one number can still 'remember' every reading it has ever seen. How?
One line of code
y = y + alpha * (x - y). The output moves a fraction α of the way towards each new reading. α = 1 copies the input (no filtering); α near 0 barely moves (heavy smoothing). Only one number, y, is stored.
Fading memory
Unrolled, the EMA is a weighted average of all past readings, with weights shrinking by a factor (1 − α) per step. Recent readings matter most; old ones fade but never quite disappear. That gives a smooth response with a quick initial reaction to a change.
Choosing α
Smaller α: less noise, more lag. A rough match to a k-sample moving average is α ≈ 2/(k + 1). If readings arrive every Δt seconds, the filter's time constant is about Δt/α; changes much faster than that are smoothed away.
Start-up and units
Initialise y with the first reading, not zero, or the output will spend many steps climbing from nothing. If the sample rate changes, α must change too to keep the same time constant.
Derivation: Unrolling the exponential filter
Start from yₙ = yₙ₋₁ + α(xₙ − yₙ₋₁) = αxₙ + (1 − α)yₙ₋₁.
Substitute the same rule for yₙ₋₁: yₙ = αxₙ + (1 − α)[αxₙ₋₁ + (1 − α)yₙ₋₂].
Keep going: yₙ = αxₙ + α(1 − α)xₙ₋₁ + α(1 − α)²xₙ₋₂ + α(1 − α)³xₙ₋₃ + …
Each older reading's weight is (1 − α) times the next newer one's: a geometric sequence. The weights add to α × 1/(1 − (1 − α)) = 1, so the output is a true weighted average.
Old readings never quite vanish, but they fade: after m steps a reading's weight has shrunk by (1 − α)ᵐ. With α = 0.2, after 10 steps it is 0.8¹⁰ ≈ 0.11 of its starting weight.
For a unit step the output after m samples is 1 − (1 − α)ᵐ. It reaches 90% when (1 − α)ᵐ = 0.1, so m = ln(0.1)/ln(1 − α): about 10.3 samples for α = 0.2.
For independent noise σ, the output's noise is σ·√(α/(2 − α)). α = 0.2 gives about 0.33σ, similar to a 9-sample moving average (σ/3). The two are roughly matched when k ≈ 2/α − 1.
Common mix-ups
- An EMA forgets readings older than some window.
✓ Every past reading keeps a (shrinking) weight; there is no hard edge. - α is a fixed property of the filter regardless of sample rate.
✓ The same α at a faster sample rate gives a shorter time constant; adjust α when you change the rate.
Open the full lesson: The exponential filter: one number of memory →
5.3 Spikes, median filters and filtering inside a controller
One glitch reading of 85 °C in a greenhouse at 18 °C. An average says 24 °C. What does the plant actually feel?
The median ignores outliers
A median filter outputs the middle value of the last k readings. A spike shorter than half the window lands at one end of the sorted list and is ignored, while real steps still pass through (after a delay of about half the window). Averages, by contrast, spread a spike's error over the whole window.
Filters live inside loops
In a feedback loop the controller acts on the filtered reading. Lag in the filter means the controller sees the world late: it keeps heating after the target is reached and overshoots. A filter that looks perfect on a recorded log can make a live loop oscillate.
Smoothing versus hysteresis
An on/off thermostat near its setpoint chatters as noise crosses the threshold back and forth. Heavy smoothing reduces chatter but adds lag. Hysteresis (switch on below one temperature, off above a higher one) stops chatter without lag, because noise smaller than the gap cannot cross both thresholds.
Common mix-ups
- Every filter is a kind of average.
✓ The median is not an average; it is a ranking, which is why it can ignore outliers that averages can't. - If a filter makes the plot look nicer, the controller will work better.
✓ Inside a loop, lag can make control worse even when the trace looks smoother.
Open the full lesson: Spikes, median filters and filtering inside a controller →
Worked examples
The moving average and the noise–lag trade-off: example 1
Readings: 12, 15, 11, 18, 14. Window k = 3. What is the latest output?
Solution
Mean of the last three: (11 + 18 + 14) ÷ 3 = 43 ÷ 3 ≈ 14.33.
The exponential filter: one number of memory: example 1
α = 0.5, y starts at 10. Readings 20, 20, 20. Outputs?
Solution
15, then 17.5, then 18.75: each step halves the remaining gap to 20.
Spikes, median filters and filtering inside a controller: example 1
A 3-sample median over readings 5, 5, 40, 6, 6. Outputs from the third reading onward?
Solution
Windows (5,5,40) → 5; (5,40,6) → 6; (40,6,6) → 6. The spike never appears in the output.
Interactive figures
Interactive figure (online). Description: 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.
Laboratory
Everything runs in your browser. No kit, nothing mains-powered. Simulations are simplified educational models.
Simulation OS experiment
Simulation OS: Two-sensor fusion
Smooth the noisy camera reading with an exponential filter and see how the lag affects the fused estimate.
Open Simulation OS: Two-sensor fusion →Studio
Code Lab
Apply a moving average, an exponential filter and a median filter to a log with one spike, and compare.
Open Code Lab →
Guided practice
Answers are checked on the server; wrong answers get a hint and another try. Signed in, solved items also count towards the source lesson.
- Readings 4, 8, 6, 10. What does a k = 2 moving average output now?
- Raw noise is σ = 6 mm. Roughly what noise remains after a 9-sample moving average (independent noise)? (mm)
- 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? (s)
- y = 50, new reading 40, α = 0.1. What is the new output?
- With α = 0.25, what weight does the reading from 2 steps ago carry in the current output?
- A temperature EMA starts with y = 0 °C, but the room is 21 °C. What happens?
- Nothing: the first reading fixes it
- The output climbs slowly from 0 and reports far too cold for a while
- The filter becomes unstable
- What is the median of 12, 97, 11, 13, 12?
- Adding a heavy filter to a heater's temperature reading most likely causes…
- faster warm-up
- more overshoot, because the controller sees changes late
- lower energy use, always
Engineering applications
Fuel gauges
Fuel sloshes as a car corners, so the raw tank reading jumps about. Gauges filter heavily, so the needle moves smoothly, accepting that it lags behind the true level by a minute or so.
Glitchy sensors
Ultrasonic rangers sometimes return a wild echo. A short median filter throws such outliers away entirely, where a mean would let one bad reading shift the estimate.
Watch
Optional verified videos. Nothing loads from YouTube until you press play.
- How Blurs & Filters Work - Computerphile (Computerphile)
Chapter summary
- A moving average of N samples reduces random noise by about √N and lags by about (N − 1) / 2 samples.
- The exponential filter is y = y + α(x − y): one number of memory, tuned by α.
- Median filters reject isolated spikes that would drag a mean.
- Every smoother trades noise for lag, and lag inside a control loop can cause overshoot.
Glossary
- Low-pass filter (moving average and EMA)
- A low-pass filter lets slow changes through and attenuates fast ones. In firmware the simplest are the moving average and the exponential moving average.
- Mean and median
- The mean is the sum of the values divided by how many there are; the median is the middle value once the values are sorted.
- Standard error of the mean
- The standard error of the mean estimates how much a sample mean would vary between repeated experiments: the sample standard deviation divided by the square root of the number of readings.
- Outliers and honest data
- An outlier is a reading far from the rest. It may be a genuine effect or a fault; either way it must be investigated and reported, and any exclusion rule decided before seeing the results.
Definitions from the Field Guide.
Project: Tune a smoother
Take a noisy log from a range sensor approaching a wall. Choose a moving average length, an exponential α and a median window, apply each, and measure both the remaining noise and the lag when the robot stops. Recommend one filter for a robot that must brake in time.
Deliverables
- Plots of the raw and three filtered signals
- A table of noise and lag for each filter
- A recommendation with its trade-off stated
Keep your work in your Engineering Journal; it stays private unless you choose to share it.
References
- Steven W. Smith (1997). The Scientist and Engineer's Guide to Digital Signal Processing. California Technical Publishing.
- Richard G. Lyons (2010). Understanding Digital Signal Processing, 3rd edition. Prentice Hall.
Publication record
- Version
- 1
- Status
- Technically checked (automated checks only)
- Technical review
- Automated checks only, last run 2026-11-29. No human, educator or mentor review is claimed.src/content/textbooks/textbooks.test.ts: schema, every lesson, diagram, practice item, video, Field Guide entry, lab and reference resolves; figure numbers recomputed; no practice answers in client payloads.
- Written by
- Compiled and written by the Tactiverge team with AI assistance, from Tactiverge School lessons that were themselves written with AI assistance.
- Licence
- All rights reserved by the founders unless a licence is approved.
- Corrections
- None recorded yet.Spotted a mistake? Use “Report a problem” on any chapter, or see the corrections page.