C3 · Lesson 24 · Code lab
Computing and plotting a filter's response
A Bode plot is just a transfer function evaluated at many frequencies. With NumPy, which handles complex numbers natively, that's a couple of lines: build a logarithmic list of frequencies, compute for all of them at once, and take the magnitude in dB and the angle in degrees. Then you can find the corner frequency numerically and check it against the formula.
Play
- Below is the response you'll compute, drawn by the course's own plotter. The code labs reproduce it with NumPy and then dig numbers out of it.
- Change R and C and watch the corner move.
Predict
You evaluate at 200 frequencies from 10 Hz to 1 MHz with np.logspace(1, 6, 200). How are those frequencies spaced?
Equally on a log scale. np.logspace(1, 6, 200) gives to with the exponents equally spaced, so each frequency is the same ratio, about 1.06 times, above the last. That matches a Bode plot's log axis: every decade gets the same number of points.
Name it
Frequency response in NumPy
import numpy as np
R, C = 1e3, 100e-9
f = np.logspace(1, 6, 200) # 10 Hz to 1 MHz
H = 1 / (1 + 1j * 2 * np.pi * f * R * C) # complex, one value per frequency
gain_db = 20 * np.log10(np.abs(H))
phase_deg = np.degrees(np.angle(H))
1jis Python's imaginary unit; arithmetic on arrays is elementwise, soHhas 200 complex values.np.absandnp.anglegive magnitude and phase (in radians;np.degreesconverts).
Finding the corner. The −3 dB point is where the gain crosses dB. With a sampled curve, the closest sample is only as accurate as the spacing; interpolating between the two samples either side (in log frequency) does much better, and np.interp does it in one line, as long as its x values increase.
Plotting (shown inline when you run the lab):
import matplotlib.pyplot as plt
plt.semilogx(f, gain_db)
plt.xlabel("frequency (Hz)"); plt.ylabel("gain (dB)"); plt.grid(True)
plt.show()Worked example
A sweep runs from 10 Hz to 100 kHz with 50 points per decade, both ends included. How many frequencies is that?
Show the solution
- Count the intervals. Four decades of 50 intervals each make intervals.
- Add the final point. Each interval starts at a point, so adding the final point gives .
Answer 201 frequencies.
Worked example
What do the gain and phase of an RC low-pass come out to at three times its corner?
Show the solution
- Gain. or −10.0 dB.
- Phase.
- What the code must do. A correct code lab must reproduce both.
Answer a gain of 0.316 (−10.0 dB) and a phase of −71.6°.
Check
Compute the gain in dB of an RC low-pass (R = 2200 Ω, C = 2.2 × 10⁻⁸ F) at 400 log-spaced frequencies from 10 Hz to 1 MHz, and set f_3db to the −3 dB frequency, found by interpolating the computed curve.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
For the same kind of filter (R = 1000 Ω, C = 4.7 × 10⁻⁸ F), set phase_at to the phase in degrees at 100 times the corner frequency, and gain_at to the gain in dB there. Compute them from directly at that one frequency.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why is the −3 dB frequency found more accurately by interpolating between samples than by taking the nearest sample?
The computed curve only exists at the sampled frequencies, which are spaced by a few percent. The nearest sample to −3 dB can therefore be off by up to half that spacing. Between neighboring samples the gain curve is smooth and nearly straight (especially against log frequency), so drawing a line between the two samples either side of −3 dB and finding where it crosses gives the frequency to a small fraction of the spacing.
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.