Skip to content
Tactiverge

School/□ Systems Engineer/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

1.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.

  • ◇ Implement y[n] = αx[n] + (1 − α)y[n−1] and compute its step response by hand.
  • ◇ Derive its steady state (DC gain 1) and its time constant in samples.
  • ◇ Make a high-pass filter as x − (low-pass) and say what it removes.

2.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).

Can't use the interactive? Read a description instead

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.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1

y[n] = 0.5x[n] + 0.5y[n−1], y[−1] = 0, and x[n] = 4 for n ≥ 0. What is y[1]?

Practice 2

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

4.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.

Open BUILD 007: Design a Smart Greenhouse →

5.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

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.

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

  • The IIR filter makes a constant reading smaller.
    ✓ Its DC gain is 1; constants pass unchanged once it has settled.
  • α is a cut-off frequency.
    ✓ α is a mixing fraction; the cut-off is roughly α·fs/(2π) for small α.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3 · a fresh version every time

y[n] = 0.2·x[n] + 0.8·y[n−1], with y[−1] = 0. The input is a step: x[n] = 5 for n ≥ 0. What is y[3]? Give 3 decimal places.

Practice 4

Using n ≈ −1/ln(1 − α), about how many samples is the time constant for α = 0.1 (1 decimal place)?

samples

Practice 5

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

7.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?

Guess first. Being wrong here is how the surprise works.

8.Reflect

Concepts: Digital filters and frequency response · Noise and filtering