Lite / Textbooks / Signals and Systems / Chapter 8
8. Filters seen in frequency
Introduction
Look at a filter through the frequency lens and its behaviour becomes a single graph: how much of each sinusoid gets through. The moving average passes slow signals, attenuates fast ones and completely blocks certain frequencies, its nulls. The first-order IIR filter, a cousin of the exponential smoother and of the RC circuit, rolls off smoothly above its cut-off. Choosing a filter is choosing that curve.
Figure 8.1. The moving average is a low-pass filter (described in words)
Gain against frequency from 0 to 50 Hz for a 4-point moving average sampled at 100 Hz. The gain is 1 at 0 Hz, 0.769 at 10 Hz, and falls to zero at 25 Hz and 50 Hz, multiples of fs / N.
Explanation
Built from 3 School lessons. Each part is the lesson's own explanation.
8.1 The moving average, seen in frequency
Averaging the last four readings smooths a sensor beautifully. So why does it completely erase one particular frequency?
One line of code
y[n] = (x[n] + x[n−1] + … + x[n−M+1])/M. It is a finite impulse response (FIR) filter: an impulse affects exactly M outputs, then is forgotten. Random noise of standard deviation σ comes out with σ/√M, because independent errors partly cancel.
Nulls and side lobes
Its gain is 1 at DC and falls to exactly zero at fs/M, 2fs/M, …, with bumps (side lobes) in between: the first is about 0.22 for large M. Great for removing a known interference, like 50 Hz mains when fs/M = 50 Hz, but a mediocre general low-pass because the side lobes leak.
The price: lag
The output is centred (M − 1)/2 samples in the past. With M = 9 at 100 Hz that is 40 ms: fine for a thermometer, dangerous inside a fast control loop where delay erodes stability. Doubling M halves the noise power again but doubles the lag.
Derivation: The moving-average frequency response
Input x[n] = e^{jωn} with ω = 2πf/fs. Output y[n] = (1/M)·Σ_{k=0}^{M−1} e^{jω(n−k)} = e^{jωn}·H, with H = (1/M)·Σ e^{−jωk}.
That is a geometric series: H = (1/M)·(1 − e^{−jωM})/(1 − e^{−jω}).
Factor out half-angles: (1 − e^{−jθ}) = e^{−jθ/2}·2j·sin(θ/2).
So |H| = |sin(ωM/2) / (M·sin(ω/2))| = |sin(πMf/fs)/(M·sin(πf/fs))|, with a pure delay of (M − 1)/2 samples.
It is 1 at f = 0 and 0 whenever Mf/fs is a whole number (other than multiples of M): nulls at f = fs/M, 2fs/M, …
Common mix-ups
A longer moving average is always better.
✓ It smooths more but lags more; inside a control loop the lag can cause oscillation.A moving average blocks all frequencies above its cut-off.
✓ Its side lobes let some high frequencies leak through; it is only zero at the nulls.
Open the Lite lesson: The moving average, seen in frequency
8.2 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?
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.
Derivation: Steady state and time constant of the first-order IIR
Filter: y[n] = αx[n] + (1 − α)y[n−1], with 0 < α ≤ 1.
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.
Step response from y[−1] = 0: the gap X − y shrinks by (1 − α) each sample, so y[n] = X·(1 − (1 − α)^{n+1}).
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.
Frequency response: H = α/(1 − (1 − α)e^{−jω}). At ω = 0 this is 1; at Nyquist (ω = π) it is α/(2 − α), the strongest attenuation.
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 α.
Open the Lite lesson: A filter with memory: first-order IIR
8.3 The RC time constant, from measurements
A capacitor charging through a resistor never quite finishes. So how can engineers say exactly how long it 'takes'?
Why it slows down
At switch-on the capacitor is empty, so the whole supply voltage sits across the resistor and a big current I = V/R flows. As charge builds up, the capacitor's voltage rises, the resistor's share falls, and the current shrinks. Less current means slower charging. The voltage approaches the supply ever more slowly: it never quite arrives, but it gets close very quickly.
The curve and its landmarks
The charging voltage is v(t) = V(1 − e^(−t/τ)) with τ = R·C. Ohms times farads gives seconds (and kΩ × µF gives milliseconds). After 1τ the capacitor is at 63.2 % of V, after 2τ 86.5 %, after 3τ 95 %, after 5τ 99.3 %. Engineers usually call it 'charged' after about 5τ. Discharging follows v = V·e^(−t/τ): 36.8 % left after one τ.
Measuring τ from data
On a measured curve, the quickest estimate is the time at which the voltage reaches 63 % of its final value. A better one uses every point: rearrange to τ = −t / ln(1 − v/V), compute it at several times and average. If ln(1 − v/V) plotted against t is a straight line, the circuit really is first-order, and the slope is −1/τ. Knowing R, the capacitance follows as C = τ/R.
Hitting a threshold
Many circuits care about when the capacitor crosses a voltage, such as a chip's reset threshold. Solve v = V(1 − e^(−t/τ)) for t: t = −τ·ln(1 − v/V). Crossing half the supply takes τ·ln 2 ≈ 0.69τ; crossing 90 % takes τ·ln 10 ≈ 2.3τ. Thresholds near the supply voltage take much longer and are sensitive to component tolerances.
Derivation: Solving the charging equation
Around the loop: V = i·R + v. The capacitor's current is i = C·dv/dt, so R·C·dv/dt = V − v.
Separate variables: dv/(V − v) = dt/(RC).
Integrate from v = 0 at t = 0: −ln(V − v) + ln V = t/(RC), so ln((V − v)/V) = −t/(RC).
Exponentiate: (V − v)/V = e^(−t/RC), so v(t) = V(1 − e^(−t/τ)) with τ = RC.
At t = τ: v = V(1 − e^(−1)) ≈ 0.632·V. Taking logs of the same line gives the measurement formula τ = −t/ln(1 − v/V).
Common mix-ups
The time constant is how long the capacitor takes to charge fully.
✓ τ is the time to reach about 63 %. 'Fully' (99 %) takes about 5τ, and mathematically it never quite finishes.A bigger supply voltage makes the capacitor charge faster.
✓ τ = RC does not depend on V. A bigger supply reaches any fixed voltage sooner, but the shape and the 63 % time stay the same.The capacitor value printed on the can is exact.
✓ Electrolytic capacitors often have ±20 % tolerance. Measuring τ and dividing by R gives the real value in your circuit.
Open the Lite lesson: The RC time constant, from measurements
Worked examples
The moving average, seen in frequency: example 1
A 5-point moving average filters x = [2, 4, 6, 8, 10, 12]. Find y[4] and y[5].
Solution
y[4] = (2 + 4 + 6 + 8 + 10)/5 = 6. y[5] = (4 + 6 + 8 + 10 + 12)/5 = 8. A ramp comes out as the same ramp delayed by (5 − 1)/2 = 2 samples.
A filter with memory: first-order IIR: example 1
α = 0.2, step input x = 1 from n = 0, y[−1] = 0. Find y[0], y[1], y[2].
Solution
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.
The RC time constant, from measurements: example 1
With 9 V, 1 kΩ and 1,000 µF, find τ, the voltage at 0.5 s and 2 s, and the time to reach 5 V.
Solution
τ = 1,000 Ω × 0.001 F = 1 s. v(0.5) = 9(1 − e^(−0.5)) ≈ 3.54 V; v(2) = 9(1 − e^(−2)) ≈ 7.78 V. Time to 5 V: t = −1 × ln(1 − 5/9) = −ln(0.444) ≈ 0.811 s.
The RC time constant, from measurements: example 2
A capacitor of unknown value charges from 9 V through 1 kΩ. You measure 6.0 V at 1.1 s. Estimate C.
Solution
τ = −1.1/ln(1 − 6/9) = −1.1/ln(0.333) = 1.1/1.0986 ≈ 1.00 s. C = τ/R = 1.00/1,000 ≈ 1,000 µF. Repeating at several times and averaging makes the estimate less sensitive to one bad reading.
Interactive figures, in words
Figure 8.2 from The moving average, seen in frequency
Choose 'Moving average', low-pass. Set M = 4 and slide the test frequency to find the null. Change M to 8 and 2: where do the nulls go? Watch the step response: how many samples does it take to reach the top?
In the explorer, the 4-point moving average has gain 1 at 0, falls to about 0.77 at f/fs = 0.1, and reaches 0 at f/fs = 0.25 (a null), with a small bump after it. With M = 8 the first null moves to 0.125. The step response climbs in M equal steps to 1.
Laboratory
These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.
- Simulation OS: RC charging and discharging (Simulation OS experiment)
Measure the RC time constant and compute the cut-off frequency 1 / (2πRC) of the same circuit used as a filter.
Open on the full site - Code Lab (Studio)
Pass sines of 5, 10, 25 and 40 Hz through a 4-point moving average at 100 Hz and measure each output amplitude.
Open on the full site
Practice
Each question links to its Lite lesson, where your answer is checked on our server and a wrong answer gets a hint. Answers are never sent to your device.
- A moving average with M = 10 runs at fs = 500 Hz. What is the lowest frequency it removes completely, in Hz?
Answer it in “The moving average, seen in frequency” - How many samples of delay does a 9-point moving average add?
Answer it in “The moving average, seen in frequency” - Noise on a sensor is random with standard deviation 4 mV. Roughly what is it after a 16-point moving average?
Answer it in “The moving average, seen in frequency” - y[n] = 0.5x[n] + 0.5y[n−1], y[−1] = 0, and x[n] = 4 for n ≥ 0. What is y[1]?
Answer it in “A filter with memory: first-order IIR” - For a constant input X, the first-order IIR low-pass settles at…
Answer it in “A filter with memory: first-order IIR” - Using n ≈ −1/ln(1 − α), about how many samples is the time constant for α = 0.1 (1 decimal place)?
Answer it in “A filter with memory: first-order IIR” - A gyroscope reading has a slow, steady bias of 0.3 °/s. Which output removes it?
Answer it in “A filter with memory: first-order IIR” - After exactly one time constant, how far has a charging capacitor got towards the supply voltage?
Answer it in “The RC time constant, from measurements”
Engineering applications
Killing mains hum
Averaging over exactly one mains cycle puts a null at the mains frequency and all its harmonics. Precision meters use this to ignore 50 Hz pickup almost completely.
Removing drift
A high-pass filter, built by subtracting a low-pass output from the input, removes slow drift such as a gyroscope's bias, keeping only the fast changes that matter for motion.
Watch (optional)
Streaming video uses a lot of data. Everything you need is in the text.
Summary
- A filter's frequency response shows the gain applied to each sinusoid.
- An N-point moving average has nulls at multiples of fs / N.
- A first-order IIR filter y = y + α(x − y) behaves like an RC low-pass with time constant τ.
- Input minus low-pass output gives a high-pass filter.
Glossary
- Low-pass filter (moving average and EMA)
- A low-pass filter lets slow changes through and attenuates fast ones. In firmware the simplest are the moving average and the exponential moving average. Field Guide
- RC time constant
- When a capacitor charges or discharges through a resistor, the voltage changes exponentially with time constant τ = R × C. Field Guide
- First-order system and time constant
- A first-order system responds to a step by approaching its new value exponentially; its time constant τ is the time to cover 63.2 % of the change. Field Guide
- Exponential growth and decay
- A quantity decays exponentially when its rate of decrease is proportional to how much is left, giving y = y0 × e^(−t/τ). Field Guide
Project: A filter to a written spec
A sensor sampled at 200 Hz carries a useful signal below 5 Hz and interference at 50 Hz. Design a moving average and a first-order IIR filter, compute each one's gain at 5 Hz and 50 Hz, and recommend one. Test your recommendation on synthetic data.
Deliverables
- Both designs with their parameters
- Gains at 5 Hz and 50 Hz, calculated and measured
- A recommendation with its lag stated
Write it up on paper or in your own notes. Lite never stores what you write.
References
- Alan V. Oppenheim et al. (1997). Signals and Systems, 2nd edition. Prentice Hall.
- Alan V. Oppenheim and Ronald W. Schafer (2010). Discrete-Time Signal Processing, 3rd edition. Pearson.
- Steven W. Smith (1997). The Scientist and Engineer's Guide to Digital Signal Processing. California Technical Publishing.