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

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 H=1/(1+j2πfRC) 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 H=1/(1+j2πfRC) at 200 frequencies from 10 Hz to 1 MHz with np.logspace(1, 6, 200). How are those frequencies spaced?

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))
  • 1j is Python's imaginary unit; arithmetic on arrays is elementwise, so H has 200 complex values.
  • np.abs and np.angle give magnitude and phase (in radians; np.degrees converts).

Finding the corner. The −3 dB point is where the gain crosses −10log10⁡2≈−3.01 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
  1. Count the intervals. Four decades of 50 intervals each make 4×50=200 intervals.
  2. Add the final point. Each interval starts at a point, so adding the final point gives 200+1=201.

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
  1. Gain. |H|=11+9=0.316 or −10.0 dB.
  2. Phase. ϕ=−tan−1⁡3=−71.6°
  3. 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 H 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?

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.