Textbooks/Introduction to Intelligent Systems/Chapter 2Lite (plain text) version
2Noise and filtering
Version 1 · Technically checked (automated checks only) · 4 source lessons · 8 practice items
Introduction
Every sensor reading is the truth plus noise, and an intelligent system that believes every reading will twitch at shadows. This chapter describes signals by amplitude, frequency and signal-to-noise ratio, then smooths them with three filters: the moving average, the exponential filter with one number of memory, and the median filter that ignores spikes. Each buys smoothness with delay, and the chapter shows how to choose the trade.
Explanation
This chapter is built from 4 School lessons. Each part below is the lesson's own explanation; open the lesson for its predict-and-discover activities.
2.1 Signals: amplitude, frequency, period and SNR
A heartbeat, a wobbling arm and a humming motor all draw wiggly lines. Which three numbers describe each wiggle?
The sine wave
x(t) = A·sin(2πft + φ). Amplitude A is the swing from the middle to a peak (peak-to-peak is 2A). Frequency f is cycles per second, in hertz (Hz). Period T = 1/f is the time for one cycle. Phase φ shifts the wave in time. Most real signals are sums of sine waves at different frequencies.
Frequency tells you the source
A motor at 3000 rpm turns 50 times a second, so it shakes its mount at 50 Hz and its multiples. A walking robot's body bobs at its step rate. Knowing the frequency of the signal you want, and of the noise you don't, is the starting point for choosing a sample rate and a filter.
Signal-to-noise ratio
SNR compares the size of the signal with the size of the noise. Using RMS values (a sine's RMS is A/√2): SNR = RMS signal ÷ RMS noise, often quoted in decibels as 20·log₁₀(that ratio). 20 dB means the signal is 10× the noise; 0 dB means they are equal, and the signal is hard to see in a single look.
Common mix-ups
- Amplitude is the distance from the lowest point to the highest.
✓ That is peak-to-peak, which is twice the amplitude. - A signal with lower SNR is a weaker signal.
✓ SNR is a ratio: a strong signal with even stronger noise has low SNR.
Open the full lesson: Signals: amplitude, frequency, period and SNR →
2.2 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 →
2.3 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 →
2.4 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
Signals: amplitude, frequency, period and SNR: example 1
x(t) = 0.3·sin(2π·25t) V with noise σ = 0.05 V. Find T, peak-to-peak and SNR in dB.
Solution
T = 1/25 = 40 ms. Peak-to-peak = 0.6 V. RMS = 0.3/√2 ≈ 0.212 V; ratio = 0.212/0.05 ≈ 4.24; SNR = 20·log₁₀(4.24) ≈ 12.6 dB.
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 signal scope draws A·sin(2πft) over one second. With A = 1 and f = 4 Hz there are four full cycles; the green markers show the amplitude and one 250 ms period. Adding noise σ makes the trace fuzzy; at σ = 0.7 the SNR is about 0 dB and the wave is hard to pick out by eye.
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: Repeated distance measurements
Increase the sensor noise and find the moving-average length that keeps the error under 1 cm, then note the delay it adds.
Open Simulation OS: Repeated distance measurements →BUILD level
BUILD: Smart greenhouse, level 3
Add a filter to the greenhouse's temperature reading and check that the heater still reacts in time to a cold draught.
Open BUILD: Smart greenhouse, level 3 →
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.
- A signal's RMS is 100 times its noise RMS. What is the SNR in decibels? (dB)
- Two sensors give the same signal; sensor B's SNR is 6 dB higher. What does that mean?
- B's signal is 6 times bigger
- B's noise is about half of A's, relative to the signal
- B samples 6 times faster
- Raw noise is σ = 6 mm. Roughly what noise remains after a 9-sample moving average (independent noise)? (mm)
- An obstacle sensor must react within 0.05 s at 100 samples/s. Which window is the largest you could use?
- k = 5
- k = 11
- k = 21
- 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
- A 5-sample median filter faces a glitch that lasts 4 samples. What happens?
- It is removed completely
- It gets through, because it fills most of the window
- The filter crashes
- 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
Obstacle sensors
An ultrasonic sensor occasionally reports a wild echo. A short median filter removes the spike without the delay a long average would add, so the robot still brakes in time.
Battery gauges
A phone's battery percentage is a heavily filtered estimate. Strong filtering hides the jumps caused by changing load, at the price of reacting slowly when the battery really drops.
Watch
Optional verified videos. Nothing loads from YouTube until you press play.
- How Blurs & Filters Work - Computerphile (Computerphile)
- Overview of FIR and IIR Filters (Barry Van Veen)
Chapter summary
- Signal-to-noise ratio compares signal and noise power; in decibels, 20·log10 of the RMS ratio.
- A k-sample moving average cuts random noise by about √k and delays the signal by about (k − 1)/2 samples.
- The exponential filter y ← y + α(x − y) needs one stored number; small α smooths more and lags more.
- Median filters reject short spikes that would drag an average; inside a control loop, every filter adds lag.
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.
- Frequency and period
- Frequency is how many cycles happen per second; the period is the time for one cycle. Each is the reciprocal of the other.
- Logarithms and decibels
- A logarithm answers 'what power gives this number?'. Decibels use logarithms to express ratios: 10 log10 for power ratios and 20 log10 for amplitude ratios.
- Standard deviation
- Standard deviation measures how spread out values are around their mean: the square root of the average squared distance from the mean.
Definitions from the Field Guide.
Project: Choose a filter against a deadline
A robot must react to an obstacle within 0.1 s, and its sensor runs at 50 Hz with occasional spikes. Compare a moving average, an exponential filter and a median filter on simulated readings, and pick the one that meets the deadline with the least noise.
Deliverables
- Simulated readings with stated noise and spike rate
- Noise and delay for each filter
- The choice, with the deadline check
Keep your work in your Engineering Journal; it stays private unless you choose to share it.
References
- Sebastian Thrun et al. (2005). Probabilistic Robotics. MIT Press.
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.