TactivergeLite

Lite / Textbooks / Signals and Systems / Chapter 8

8. Filters seen in frequency

Version 1 · Technically checked (automated checks only) · 3 source lessons · Full interactive version

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

  2. That is a geometric series: H = (1/M)·(1 − e^{−jωM})/(1 − e^{−jω}).

  3. Factor out half-angles: (1 − e^{−jθ}) = e^{−jθ/2}·2j·sin(θ/2).

  4. So |H| = |sin(ωM/2) / (M·sin(ω/2))| = |sin(πMf/fs)/(M·sin(πf/fs))|, with a pure delay of (M − 1)/2 samples.

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

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

Common mix-ups

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
  1. Around the loop: V = i·R + v. The capacitor's current is i = C·dv/dt, so R·C·dv/dt = V − v.

  2. Separate variables: dv/(V − v) = dt/(RC).

  3. Integrate from v = 0 at t = 0: −ln(V − v) + ln V = t/(RC), so ln((V − v)/V) = −t/(RC).

  4. Exponentiate: (V − v)/V = e^(−t/RC), so v(t) = V(1 − e^(−t/τ)) with τ = RC.

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

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.

The interactive versions are in the full chapter.

Laboratory

These run in a browser on the full site (they need a faster connection). Simulations are simplified educational models.

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.

  1. 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”
  2. How many samples of delay does a 9-point moving average add?
    Answer it in “The moving average, seen in frequency”
  3. 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”
  4. 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”
  5. For a constant input X, the first-order IIR low-pass settles at…
    Answer it in “A filter with memory: first-order IIR”
  6. 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”
  7. 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”
  8. 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

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

Write it up on paper or in your own notes. Lite never stores what you write.

References

← 7. The DFT: measuring a spectrumContents →