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

C9 · Lesson 52 · Signals lab

Convolution, difference equations and frequency response in Python

Every signal-processing library has three functions at its core: one that convolves, one that runs a difference equation over a signal (called lfilter in SciPy), and one that evaluates a filter's frequency response. They're short, and writing them yourself is the surest way to know exactly what they compute, including the conventions that trip people up, like the sign of the feedback coefficients. In this lab you'll write all three and use them on the 60 Hz notch filter from lesson 39.

  • The input stems are two tones, a slow one at ω = 0.2 and a fast one at ω = 2.5 (half the size). The output stems are what an M-point moving average makes of them, once it has filled (after M − 1 samples).
  • At M = 1 nothing changes. Raise M: the fast tone shrinks quickly while the slow one barely changes. At M = 5 the fast tone is nearly gone: 2.5 is almost exactly the 5-point average's second null, 2 × 2π/5 = 2.51.
  • Your lfilter will produce exactly these output samples, and freq_response the gains in the readouts.

Predict

lfilter(b=[1], a=[1, -0.5], x=[1, 0, 0, 0]) runs y[n]=0.5y[n−1]+x[n]. What does it return?

Name it

The conventions

A filter is two coefficient lists, b (numerator) and a (denominator, with a[0] usually 1):

H(z)=b0+b1z−1+⋯+bMz−Ma0+a1z−1+⋯+aNz−N

which is the difference equation

a0y[n]=∑k=0Mbkx[n−k]−∑k=1Naky[n−k]

starting at rest (samples before n = 0 are zero). An FIR filter has a = [1].

The three functions

  • convolve(x, h): output length len(x) + len(h) − 1, with y[i + j] += x[i]·h[j].
  • lfilter(b, a, x): compute each y[n] from the formula above, in order, using outputs already computed.
  • freq_response(b, a, w): with z−1=e−jω, add up bkz−k and akz−k and divide.

Worked example

Run lfilter([0.25, 0.25, 0.25, 0.25], [1], [1, 1, 1, 1, 1, 1]).

Show the solution
  1. What it computes. A 4-point moving average of a step.
  2. The output. 0.25,0.5,0.75,1,1,1
  3. Filling up. It takes M − 1 = 3 samples to fill, then outputs the input's average.

Answer 0.25, 0.5, 0.75, 1, 1, 1.

Worked example

What is abs(freq_response([1], [1, -0.5], pi))?

Show the solution
  1. At ω = π. z−1=e−jπ=−1
  2. Evaluate. H=1/(1+0.5)=0.667
  3. Compare DC. At DC it would be 1/(1 − 0.5) = 2.

Answer 0.667.

Check

lfilter([1], [1, -(0.7)], impulse) gives h. What is h[3]?

Check

lfilter([1/4]*4, [1], step) averages a unit step over 4 points. What is the output at n = 1?

Check

Write convolve(x, h): the linear convolution of two lists, of length len(x) + len(h) − 1.

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

Check

Write lfilter(b, a, x): run the difference equation with coefficient lists b and a (a[0] divides, a[1:] are feedback with the sign convention above), starting at rest, and return the output list.

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

Check

Write freq_response(b, a, w): the complex value H(ejω) of the filter at frequency w (radians per sample).

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 lfilter have to compute the outputs in order, while convolve could compute them in any order?

Practice

Keep practicing

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