TactivergeLite

Lite / Textbooks / Signals and Systems / Chapter 5

5. Smoothing filters

Version 1 · Technically checked (automated checks only) · 3 source lessons · Full interactive version

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.

Figure 5.1. Smoothing costs lag (described in words)

The response of two smoothers to a sudden step from 0 to 1. An exponential filter with α = 0.5 gives 0.5, 0.75, 0.875, 0.9375; a 4-point moving average gives 0.25, 0.5, 0.75, 1 and only reaches the new value after four samples.

Explanation

Built from 3 School lessons. Each part is the lesson's own explanation.

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

Open the Lite 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
  1. Start from yₙ = yₙ₋₁ + α(xₙ − yₙ₋₁) = αxₙ + (1 − α)yₙ₋₁.

  2. Substitute the same rule for yₙ₋₁: yₙ = αxₙ + (1 − α)[αxₙ₋₁ + (1 − α)yₙ₋₂].

  3. Keep going: yₙ = αxₙ + α(1 − α)xₙ₋₁ + α(1 − α)²xₙ₋₂ + α(1 − α)³xₙ₋₃ + …

  4. 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.

  5. 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.

  6. 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.

  7. 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

Open the Lite 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

Open the Lite 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, in words

Figure 5.2 from The moving average and the noise–lag trade-off

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?

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.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

Practice

Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.

  1. Readings 4, 8, 6, 10. What does a k = 2 moving average output now?
    Answer it in “The moving average and the noise–lag trade-off”
  2. Raw noise is σ = 6 mm. Roughly what noise remains after a 9-sample moving average (independent noise)?
    Answer it in “The moving average and the noise–lag trade-off”
  3. 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?
    Answer it in “The moving average and the noise–lag trade-off”
  4. y = 50, new reading 40, α = 0.1. What is the new output?
    Answer it in “The exponential filter: one number of memory”
  5. With α = 0.25, what weight does the reading from 2 steps ago carry in the current output?
    Answer it in “The exponential filter: one number of memory”
  6. A temperature EMA starts with y = 0 °C, but the room is 21 °C. What happens?
    Answer it in “The exponential filter: one number of memory”
  7. What is the median of 12, 97, 11, 13, 12?
    Answer it in “Spikes, median filters and filtering inside a controller”
  8. Adding a heavy filter to a heater's temperature reading most likely causes…
    Answer it in “Spikes, median filters and filtering inside a controller”

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)

Streaming video uses a lot of data. Everything you need is in the text.

Summary

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. Field Guide
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. Field Guide
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. Field Guide
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. 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

Write it up on paper or in your own notes. Lite never stores what you write.

References

← 4. Quantisation and resolution6. Sinusoids and Fourier series →