⚡ Spark Academy53 lessons

Filters in Software

One line of arithmetic — y += α(x − y) — and the RC filter you built with parts is reborn as code you can retune in a keystroke.

lesson 3 of 3 in this unit

Builds on: 5.3 Impedance & RC Filters11.2 Reading the Analog World13.2 Fourier: Thinking in Frequencies

The capacitor, discretised

Recall the RC charging law (2.3): the capacitor voltage moves toward the input at a rate set by how far away it is. Write that for sampled data and you get the exponential moving average:

y = y + α · (x − y)x = new sample, y = filtered value · small α = big “capacitor” · τ ≈ T_sample/α

This one line is an RC low-pass filter — same exponential step response, same −6 dB/octave rolloff, same everything — except its “R” and “C” are a number you can change while the system runs. Every smooth sensor readout, every “smoothed” game statistic, every thermostat display runs something like it.

The moving average — and the eternal trade

Its sibling averages the last N samples outright. Great noise-flattening, with a quirk: it’s blind to any periodic signal whose cycle exactly fits the window (an N-sample average of one full cycle is zero — a notch!). Engineers exploit that: average over exactly one mains cycle and 50 Hz hum vanishes from your measurement.

Both filters charge the same toll you have now met three times: the detector RC (9.3), the analog filter’s phase lag (5.3), and here — smoothness costs lag. Filter hard and your night-light answers slowly; filter lightly and it jitters. There is no free smoothing, only a well-chosen trade.

Why software filters won the war

  • Retunable at runtime — imagine re-soldering a capacitor every time conditions change.
  • Perfectly repeatable — no tolerance cloud (15.1 will show what clouds cost).
  • Shapes impossible in RC: sharp brick-walls, notches, matched filters — chains of these one-liners.
  • But: they only exist after the ADC — the anti-alias filter before it must stay analog forever (13.1's law).
The professional split

Modern design puts the minimum analog filtering needed for honesty (anti-alias) in hardware and all the character in software. Your capstone scope and every digital oscilloscope on Earth follow exactly this split.

⚡ Lab — The One-Line Filter

A noisy sensor, filtered live by code you can retune while it runs.

  • EMA with α = 0.5, then 0.05: watch noise die and lag grow.
  • Inject a step and measure the reaction delay at each setting.
  • Moving average with a big window: glassy smooth, glacially late. Choose your trade.
0.15
The punchline
y += α(x − y) is the capacitor equation from Lesson 2.3 with the physics replaced by a multiply
The eternal trade
smaller α (or bigger N) = smoother but slower — exactly the detector RC dilemma from 9.3

Check your understanding

Q1. The code y += α(x − y) implements…

Q2. Making α smaller (or the averaging window bigger)…

Q3. One filter must always remain analog hardware:

Q4. Averaging over exactly one 50 Hz mains cycle is popular because…