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
lfilterwill produce exactly these output samples, andfreq_responsethe gains in the readouts.
Predict
lfilter(b=[1], a=[1, -0.5], x=[1, 0, 0, 0]) runs . What does it return?
[1, 0.5, 0.25, 0.125]. In the b, a convention the denominator is , so a = [1, −0.5] means : the feedback coefficient enters with its sign flipped. Getting that sign wrong gives the first option.
Name it
The conventions
A filter is two coefficient lists, b (numerator) and a (denominator, with a[0] usually 1):
which is the difference equation
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 , add up and and divide.
Worked example
Run lfilter([0.25, 0.25, 0.25, 0.25], [1], [1, 1, 1, 1, 1, 1]).
Show the solution
- What it computes. A 4-point moving average of a step.
- The output.
- 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
- At ω = π.
- Evaluate.
- 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 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?
Convolution's output samples each depend only on the input, which is all known in advance, so every output can be computed independently. A recursive filter's output depends on its own earlier outputs through the feedback terms, so y[n] can't be computed until y[n − 1], y[n − 2], … exist. That chain is what lets a short recursion produce an infinitely long impulse response, and it's also why IIR filters are harder to run in parallel than FIR filters.
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.