Skip to content

Sensors & Signal Processing

First-order vs second-order low-pass filters, in code

A first-order low-pass filter, like an RC network or a one-line exponential filter, lets slow changes through and shrinks fast ones in proportion: above its cutoff, a wave ten times faster comes out about ten times smaller. Put two in a row and you have a second-order filter. Each stage cuts the fast part again, so ten times faster comes out about a hundred times smaller.

How much gets through

Above the cutoff, a first-order filter passes about cutoff ÷ frequency of a wave:

0.2 Hz cutoff, 2 Hz wave: 0.2 ÷ 2 = a tenth gets through

On a Bode plot that's a slope of −20 dB per decade, or −6 dB per octave. Two identical stages pass a tenth of a tenth, a hundredth: −40 dB per decade. Three stages, −60.

In code

Each stage is an exponential filter, and the second follows the first, not the raw reading:

stage1 = stage1 + (raw - stage1) * 0.025;
stage2 = stage2 + (stage1 - stage2) * 0.025;
level = stage2;

At 100 samples a second, alpha 0.025 puts each stage's cutoff near 0.4 Hz, from cutoff ≈ alpha × sample rate ÷ 2π. Alpha is per sample, so change the sample rate and the cutoff moves with it: the same alpha at 1000 samples a second cuts off ten times higher.

Live experiment

Fix the flicker, keep the speed

A boat's fuel gauge reads 1000 times a second while the fuel sloshes twice a second. Try no filter, the alpha of 0.02 meant for 100 samples a second, then 0.002 and 0.0002, and watch the LOW light's flicker and how long each takes to find 20%.

Try it yourself: free lesson

“Keep the slow part” is lesson 3 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 15 before it first.

The trade: smooth or quick

A gauge reads a boat's tank at 60% while the fuel sloshes ±10% twice a second, and it starts from 0 when switched on. One stage slow enough to wobble less than 1% still reads only 50% after 3 s. One quick enough to arrive in time wobbles 4%. Two stages at 0.025 do both: they reach the level in about 2 s, and each passes about a fifth of the 2 Hz slosh, so together they pass a twenty-fifth, a wobble under 1%.

Not just a lower cutoff

Two stages bend more gently at the corner than one: two identical first-order stages are already 6 dB down at their own cutoff, not 3 dB. They also delay a step about twice as long as one stage. For a sharp knee, designers use a Butterworth second-order filter: a Sallen-Key op-amp circuit in hardware, or a biquad in code. Either way the slope beyond the corner is −40 dB per decade.

Where you'll meet it

A car's fuel gauge is filtered so heavily it takes minutes to settle after you fill up. Anti-aliasing filters in front of an ADC are often second order or more, and a loudspeaker's crossover uses second-order sections to split bass from treble cleanly.

Free lesson

Now build it yourself

Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.

Try it yourself: free lesson

“Keep the slow part” is lesson 3 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 15 before it first.

Or start from the very first lesson, no account needed

Practice questions

Three quick questions from the lessons. Pick an answer to check it.

  1. Question 1 of 3The same filter, alpha 0.02, now runs at 1000 samples a second. Its cutoff…
    Show the answer

    Answer: Rises ten times, to about 3 Hz. The filter takes the same fraction of each step, but there are ten times as many steps a second, so it follows ten times faster.

    • Why not “Disappears”? It's still a low-pass filter, just with a higher cutoff that lets more of the slosh through.
    • Why not “Stays at 0.32 Hz”? Alpha is per sample, not per second. Ten times the samples a second means ten times the steps.
    • Why not “Falls ten times”? More samples a second make the filter follow faster, so the cutoff rises, not falls.
  2. Question 2 of 3A filter with a cutoff of 0.2 Hz. Roughly how much of a 2 Hz slosh gets through?
    Show the answer

    Answer: About a tenth. Above the cutoff it lets through about cutoff ÷ frequency: 0.2 ÷ 2 = 0.1.

    • Why not “All of it”? Only changes well below the cutoff get through whole. 2 Hz is ten times above it.
    • Why not “Half”? About half gets through near the cutoff itself. Ten times above, it's about cutoff ÷ frequency: a tenth.
    • Why not “None at all”? A simple filter shrinks fast changes but never removes them completely.
  3. Question 3 of 3Two identical filters in a row each let a tenth of the slosh through. Together they let through…
    Show the answer

    Answer: A hundredth. The second filter gets a tenth of the slosh and passes a tenth of that: 0.1 × 0.1 = 0.01.

    • Why not “A tenth”? The second filter shrinks what the first let through by another ten times.
    • Why not “Twice as much”? Each filter only shrinks the slosh. Two in a row can't let through more than one.
    • Why not “A fifth”? Shrinkings multiply, they don't add: 0.1 × 0.1 is 0.01, not 0.2.
Keep practising: free lesson

“Keep the slow part” is lesson 3 of 6 in Alias Atoll, Sensors & Signal Processing World 3. Free with an account. Lessons unlock in order, so you’ll do the 15 before it first.

Questions people ask

What is the difference between first-order and second-order filters?

Their slope past the cutoff. A first-order filter falls by 20 dB per decade, a factor of ten in voltage for every tenfold rise in frequency. A second-order filter falls by 40 dB per decade, a factor of a hundred, so it rejects noise well above the cutoff far better.

How do I make a second-order low-pass filter in code?

Run two exponential filters in a row: the first follows the raw reading, the second follows the first. For a sharper corner, use a biquad with Butterworth coefficients, which many audio and DSP libraries provide.

How do I choose the cutoff frequency?

Put it between what you want to keep and what you want to remove, as far from each as you can. A cutoff ten times below the noise frequency shrinks that noise about ten times per stage.

Every topic