C1219 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C12 · Lesson 19 · Filtering in firmware

Moving averages, exponential smoothing and medians in integer arithmetic

Raw ADC readings jitter by a few codes, sensors pick up hum, and a loose connector throws in the odd wild spike. Most embedded programs filter their inputs, and almost all of them use one of three filters: a moving average, an exponential smoother, or a median. Each fits in a handful of integer instructions, each has a delay and a noise reduction you can calculate, and each has a classic integer bug worth knowing before it bites.

  • The stems are the step responses of two filters: a moving average of N samples and an exponential smoother with α=2−k.
  • The moving average reaches the new value in exactly N samples, in a straight line. The smoother approaches it gradually and never quite arrives.
  • The readouts compare their noise reduction: how much of the input's random noise survives each filter.

Predict

A program smooths ADC readings with y = y + ((x - y) >> 4); in integers. The input sits steady at 1000 and y starts at 0. Where does y end up?

Name it

Moving average

yn=1N∑i=0N−1xn−i. Keep a running sum: add the new sample, subtract the one leaving the window (stored in a ring buffer). It reduces random noise by N, delays the signal by (N−1)/2 samples, and has nulls at multiples of fs/N: choosing N so that fs/N = 50 or 60 Hz removes mains hum completely. The sum needs ⌈log2⁡(N⋅xmax+1)⌉ bits.

Exponential smoother

yn=yn−1+α(xn−yn−1), a one-pole low-pass. With α=2−k the multiply is a shift. Its time constant is −1/ln⁡(1−α)≈2k samples, it reaches within 1% after ln⁡0.01/ln⁡(1−α) samples, and it passes a fraction α/(2−α) of white noise's rms. It needs no buffer, only one variable, kept with k extra fraction bits to avoid the stall.

Median

The middle of the last 3 or 5 samples. It ignores an isolated spike completely, where an average smears it over N samples, and it preserves sharp steps. It's not linear, so it has no frequency response to speak of; it's often followed by a smoother.

Worked example

ADC readings have 4 codes of rms noise and arrive at 1 kHz. What do a 16-sample moving average and a k = 4 smoother do to the noise, and what delays do they add?

Show the solution
  1. Moving average noise. 416=1code
  2. Moving average delay. 7.5 samples, which is 7.5 ms at 1 kHz.
  3. Smoother noise. With α=1/16 it passes a fraction 1/1631/16=0.180 of the noise, so 0.72 codes.
  4. Smoother time constant. 15.5 samples.
  5. Compare them. The smoother reduces noise more because its memory is longer: matching its noise needs a moving average of 31 samples.

Answer the moving average leaves 1 code of noise and delays by 7.5 ms; the smoother leaves 0.72 codes, with a time constant of 15.5 samples.

Worked example

Readings arrive at 1 kHz with 50 Hz hum. What moving-average length removes the hum entirely?

Show the solution
  1. Where the nulls fall. A moving average has nulls at multiples of fs/N.
  2. Put one on the hum. 1000N=50soN=20
  3. Why it works. The average then spans exactly one hum cycle, whose average is zero.
  4. 60 Hz hum. At 1 kHz, 1000/60 isn't whole; sampling at 1.2 kHz with N = 20 works instead.

Answer a 20-sample moving average.

Worked example

How long must the running sum be for a 64-sample average of 12-bit readings?

Show the solution
  1. Largest sum. 64×4095=262080
  2. Bits needed. That needs 18 bits, so it fits easily in 32 bits.
  3. A 16-bit sum. It would overflow at a sum of 65,535, which is reached with a reading of about 1024 on every sample.

Answer 18 bits, so a 32-bit running sum holds it easily.

Check

A moving average of 16 samples runs on readings with 2 codes of rms noise, at 1000 Hz.

(a)

How much noise is left?

(b)

How much delay does it add?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

Readings arrive at 1000 Hz with hum at 50 Hz.

(a)

What moving-average length puts its first null on the hum?

(b)

What delay does that average add?

Write units the way you normally would: 1.5 ms, 1500 µs and 0.0015 s are the same answer.

Check

An exponential smoother uses α=2−3.

(a)

What is its time constant?

(b)

After how many samples is a step within 1% (whole samples)?

(c)

What fraction of white noise's rms passes?

Check

A running sum averages 64 samples of a 10-bit unsigned ADC.

(a)

What is the largest sum?

(b)

How many bits does the sum need?

Check

A smoother y = y + ((x - y) >> 4) without extra fraction bits starts at 0 with a steady input of 700.

(a)

What is the largest gap at which it stops moving?

(b)

How many fraction bits must the state carry to fix it?

Check

A filter costs 60 cycles per sample on the 10 MHz board and runs at 1 kHz.

(a)

What share of the processor does it use?

(b)

How many such filters could run, at most?

Check

A pressure sensor is read at 100 Hz and occasionally returns a single wild value from a loose connector. Which filter suits it best before display?

Check

Write integer-only filters: moving_average(xs, n) with a running sum (output the floor of the sum over n once n samples have arrived, nothing before), and smoother(xs, k) keeping s = 2^k y (output s >> k after each sample, s starting at 0).

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why does an exponential smoother written as y += (x - y) >> 4 in integers settle short of a steady input, and how does keeping extra fraction bits fix it?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.