TactivergeLite

Lite / Textbooks / Introduction to Intelligent Systems / Chapter 2

2. Noise and filtering

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

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.

Figure 2.1. Smoothing costs delay (described in words)

Unit-step responses of two filters over 10 samples. A 5-sample moving average climbs in equal steps and reaches 1 after 5 samples. An exponential filter with α = 0.2 rises quickly then slowly: 0.672 after 5 samples and 0.893 after 10, never quite reaching 1.

Explanation

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

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

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

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

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

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

Figure 2.2 from Signals: amplitude, frequency, period and SNR

With sampling off, change amplitude and frequency and watch the period marker. Set f = 4 Hz and count the cycles in one second. Then add noise: at what σ does the wave become hard to see with A = 1? Note the SNR at that point.

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.

Figure 2.3 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. A signal's RMS is 100 times its noise RMS. What is the SNR in decibels?
    Answer it in “Signals: amplitude, frequency, period and SNR”
  2. Two sensors give the same signal; sensor B's SNR is 6 dB higher. What does that mean?
    Answer it in “Signals: amplitude, frequency, period and SNR”
  3. 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”
  4. An obstacle sensor must react within 0.05 s at 100 samples/s. Which window is the largest you could use?
    Answer it in “The moving average and the noise–lag trade-off”
  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. A 5-sample median filter faces a glitch that lasts 4 samples. What happens?
    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

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)

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
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. Field Guide
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. Field Guide
Standard deviation
Standard deviation measures how spread out values are around their mean: the square root of the average squared distance from the mean. 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

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

References

← 1. Sense, think, act3. Machine vision →