Lite / Intelligent Robotic Systems / Week 23
The exponential filter: one number of memory
A filter that remembers just one number can still 'remember' every reading it has ever seen. How?
Why should I care?
Microcontrollers with tiny memories filter thousands of sensors with exactly this line of code. It needs no buffer, runs in constant time and has a single knob to tune, which makes it the default smoother in embedded systems.
- Apply one step of an exponential moving average y ← y + α(x − y).
- Show that the EMA weights past readings by a geometric sequence.
- Relate α to smoothing and lag, and compare the EMA with a moving average.
Predict
y = 20, a new reading of 30 arrives, and α = 0.2. What is the new output?
- 30
- 22
- 26
Guess first, then reveal
Move a fifth of the way from 20 towards 30: 20 + 0.2 × 10 = 22. Small α means each new reading nudges the output only a little.
Discover
Hide the moving average. Sweep α from 1 down to 0.05 on the Step signal and record noise left and lag. Then find the moving-average window k that gives about the same noise as α = 0.2. Which of the two reaches 90% of the step sooner?
What the interactive shows (text version)
With the EMA alone on the step signal, α = 1 follows the noise exactly; α = 0.3 smooths it and reaches 90% of the step in about 7 samples; α = 0.05 is very smooth and takes about 45 samples. The EMA reacts immediately to the step and then curves towards the new level, unlike the straight ramp of the moving average.
Understand
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.
Explain it another way
Intuitive
Your opinion of a friend: each new thing they do shifts it a little. Recent behaviour counts most, but last year still leaves a faint trace.
Technical
The EMA is a first-order infinite impulse response (IIR) low-pass filter, the discrete version of an RC circuit, with time constant τ ≈ Δt/α for small α.
Mathematical
yₙ = Σₘ α(1 − α)ᵐ xₙ₋ₘ. Step response 1 − (1 − α)ⁿ⁺¹; white-noise variance ratio α/(2 − α).
Applied
The complementary filter used to estimate a drone's tilt is built from exponential filters: a low-pass on the accelerometer's angle and a high-pass on the integrated gyro.
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.
Worked example 1
α = 0.5, y starts at 10. Readings 20, 20, 20. Outputs?
15, then 17.5, then 18.75: each step halves the remaining gap to 20.
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.
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 an EMA class with a time constant in seconds (not α) and a sample interval. It should compute α = Δt/(τ + Δt) internally. Show that changing the sample rate from 50 Hz to 200 Hz leaves the step response in seconds roughly unchanged.
Reflect
When would you choose an EMA over a moving average on a small microcontroller, and when the reverse?
Write your answer on paper or in your own notes. Lite never stores what you write.