The exponential filter: one number of memory
A filter that remembers just one number can still 'remember' every reading it has ever seen. How?
About 35 min · 3 goals · Checked by automated tests
1.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.
2.Predict
y = 20, a new reading of 30 arrives, and α = 0.2. What is the new output?
Guess first. Being wrong here is how the surprise works.
3.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?
Loading the interactive…
Can't use the interactive? Read a description instead
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.
4.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
y = 50, new reading 40, α = 0.1. What is the new output?
5.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
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.
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 α.
yₙ = Σₘ α(1 − α)ᵐ xₙ₋ₘ. Step response 1 − (1 − α)ⁿ⁺¹; white-noise variance ratio α/(2 − α).
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.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
An exponential filter uses y_new = y_old + α·(x − y_old) with α = 0.2. Its last output was 26.5 and the new reading is 21.8. What is the new output?
Practice 3 · a fresh version every time
An exponential filter uses y_new = y_old + α·(x − y_old) with α = 0.25. Its last output was 20.4 and the new reading is 21.8. What is the new output?
Practice 4
With α = 0.25, what weight does the reading from 2 steps ago carry in the current output?
Practice 5
Which α roughly matches the smoothing of a 9-sample moving average?
Practice 6
A temperature EMA starts with y = 0 °C, but the room is 21 °C. What happens?
7.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.
8.Reflect
Concepts: Noise and filtering