TactivergeLite

Lite / Engineering Complex Systems / Week 22

A filter with memory: first-order IIR

One multiply, one add and a single remembered number can smooth a sensor as well as averaging dozens of readings. How?

About 40 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

The exponential moving average needs almost no memory, which is why it runs inside battery monitors, drone flight controllers and stock-price charts. Its single knob α trades smoothness for responsiveness, and the maths that tunes it is the same maths as an RC circuit.

Predict

y[n] = 0.2x[n] + 0.8y[n−1], starting from y = 0, with x jumping to 10 and staying there. Where does y end up?

  1. 2
  2. 8
  3. 10
  4. It keeps growing
Guess first, then reveal

It settles at 10. In steady state y[n] = y[n−1] = y, so y = 0.2·10 + 0.8y, giving 0.2y = 2 and y = 10. The filter has DC gain exactly 1: it never distorts a constant, it only delays changes.

Discover

In the Code Lab, write a loop that applies y = a*x + (1 - a)*y to a step input of 10 for 30 samples, printing n and y. Try a = 0.5, 0.2 and 0.05. For each, find the first n where y passes 6.3 (63 % of the way).

What the interactive shows (text version)

Running the filter on a step of 10: with α = 0.5 the output reaches 6.3 at n = 1 (7.5); with α = 0.2 at n = 4 (6.7); with α = 0.05 at about n = 19. Smaller α is smoother but slower; the final value is always 10.

You can also open the interactive on the full site when you have a faster connection.

Understand

Infinite memory in one number

Unlike a moving average, the IIR (infinite impulse response) filter feeds its own output back. An impulse never fully disappears; its influence decays by (1 − α) every sample. The weights on past inputs are α, α(1 − α), α(1 − α)², …: an exponentially fading average.

One knob

Large α (close to 1) follows the input almost immediately; small α smooths heavily but responds slowly (time constant about 1/α samples). It behaves like an RC low-pass with RC ≈ T/α, so the cut-off is roughly fc ≈ α·fs/(2π) for small α.

High-pass for free

x[n] − y[n] keeps what the low-pass removed: fast changes pass and any steady offset decays to zero. Use it to remove a slow drift, such as a gyroscope's bias, while keeping quick motions. Combining a high-passed gyro with a low-passed accelerometer is the complementary filter used on drones.

Explain it another way

Intuitive

Each new reading nudges your estimate a fraction α of the way towards it. Small α: you're hard to convince. Large α: you believe the latest reading.

Technical

Single pole at z = 1 − α inside the unit circle, so stable for 0 < α < 2 (and non-oscillating for 0 < α ≤ 1). Group delay at low frequency ≈ (1 − α)/α samples.

Mathematical

y[n] = Σ_{k≥0} α(1 − α)^k x[n−k]. Weights sum to α/(1 − (1 − α)) = 1. Step response y[n] = X(1 − (1 − α)^{n+1}).

Applied

A battery fuel gauge low-passes the noisy current measurement with α around 0.01 at 100 Hz (time constant about 1 s), so the displayed percentage doesn't flicker when a motor starts.

Derivation: Steady state and time constant of the first-order IIR
  1. Filter: y[n] = αx[n] + (1 − α)y[n−1], with 0 < α ≤ 1.

  2. Steady state for constant input X: set y[n] = y[n−1] = y. Then y = αX + (1 − α)y, so αy = αX and *y = X**. DC gain is 1.

  3. Step response from y[−1] = 0: the gap X − y shrinks by (1 − α) each sample, so y[n] = X·(1 − (1 − α)^{n+1}).

  4. Time constant: the gap falls to 1/e (37 %) after n with (1 − α)^n = e^{−1}, so n = −1/ln(1 − α) ≈ 1/α for small α. α = 0.1 gives about 9.5 samples.

  5. Frequency response: H = α/(1 − (1 − α)e^{−jω}). At ω = 0 this is 1; at Nyquist (ω = π) it is α/(2 − α), the strongest attenuation.

Worked example 1

α = 0.2, step input x = 1 from n = 0, y[−1] = 0. Find y[0], y[1], y[2].

y[0] = 0.2. y[1] = 0.2 + 0.8 × 0.2 = 0.36. y[2] = 0.2 + 0.8 × 0.36 = 0.488. Check: 1 − 0.8³ = 0.488.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 2. For a constant input X, the first-order IIR low-pass settles at…

samples

Practice 5. A gyroscope reading has a slow, steady bias of 0.3 °/s. Which output removes it?

Apply it

Your greenhouse temperature sensor is sampled at 1 Hz and you want readings to settle within about 30 s without flicker. Choose α, justify it with the time-constant formula, and test it on a step from 18 °C to 24 °C in code.

Reflect

When would you prefer a moving average to a first-order IIR, and the other way round?

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

← The moving average, seen in frequencyTransforms: why engineers change the question →