Sub topic 12
Checking Results with a Script and an AC Simulation
This chapter assumes you can derive H(jω) for a first-order RC or RL filter, convert gain to decibels, and read a Bode plot. It also assumes you can run Python with NumPy and a SPICE-family simulator such as ngspice or LTspice. The goal is to confirm your hand results two independent ways. The first is a numerical evaluation of the formula. The second is an AC simulation of the circuit itself.
Why check with two independent methods
A hand derivation can go wrong in several ways: a mis-simplified fraction, hertz mixed up with radians per second, or a forgotten load. Each check catches different mistakes.
- A script evaluates your formula. It catches arithmetic and unit errors, but it cannot tell you that the formula describes the wrong circuit.
- A simulation solves the circuit from its netlist. It catches a wrong formula, but it can hide a wrong netlist, such as an output node on the wrong side of a component.
When the hand calculation, the script, and the simulation agree, an error would have to appear identically in all three. That is unlikely.
Step 1: Evaluate the formula in a script
Take the RC low-pass filter from earlier chapters, with R = 1.59 kΩ and C = 100 nF. Its transfer function is H(jω) = 1 / (1 + jωRC), and the cutoff frequency is fc = 1 / (2πRC), about 1 kHz.
import numpy as np
R = 1.59e3 # ohms
C = 100e-9 # farads
fc = 1 / (2 * np.pi * R * C)
print(f"fc = {fc:.1f} Hz")
f = np.logspace(1, 5, 5) # 10 Hz to 100 kHz, one point per decade
w = 2 * np.pi * f
H = 1 / (1 + 1j * w * R * C) # RC low-pass, output across C
mag_db = 20 * np.log10(np.abs(H))
phase_deg = np.degrees(np.angle(H))
for fi, m, p in zip(f, mag_db, phase_deg):
print(f"{fi:9.1f} Hz {m:7.2f} dB {p:7.2f} deg")The script prints fc = 1001.0 Hz followed by five rows. Your hand results should match these values:
| Frequency | Magnitude (dB) | Phase (deg) |
|---|---|---|
| 10 Hz | 0.00 | -0.57 |
| 100 Hz | -0.04 | -5.71 |
| 1 kHz | -3.01 | -44.97 |
| 10 kHz | -20.03 | -84.28 |
| 100 kHz | -39.99 | -89.43 |
The 1 kHz row is not exactly -45 degrees because 1 kHz is slightly below fc = 1001.0 Hz. At fc itself, the gain is -3.01 dB and the phase is -45.00 degrees.
Three details in this script commonly cause errors:
np.log10multiplied by 20 gives decibels for a voltage ratio. Use 10 only for power ratios.np.anglereturns radians.np.degreesconverts them.w = 2 * np.pi * fconverts hertz to radians per second. Usingfdirectly in1j * f * R * Cshifts the whole response by a factor of 2π.
Step 2: Check the high-pass side
The same pattern works for an RL high-pass filter with the output taken across the inductor. Here H(jω) = jωL / (R + jωL) and fc = R / (2πL). Evaluating at fc/10, fc, and 10·fc shows the pass band, the corner, and the stop band in three rows.
import numpy as np
R = 62.8 # ohms
L = 10e-3 # henries
fc = R / (2 * np.pi * L)
print(f"fc = {fc:.1f} Hz")
f = np.array([fc / 10, fc, fc * 10])
w = 2 * np.pi * f
H = (1j * w * L) / (R + 1j * w * L) # RL high-pass, output across L
for fi, h in zip(f, H):
print(f"{fi:8.1f} Hz {20 * np.log10(abs(h)):7.2f} dB {np.degrees(np.angle(h)):7.2f} deg")The expected output is fc = 999.4 Hz, then roughly -20.04 dB at +84.29 degrees, -3.01 dB at +45.00 degrees, and -0.04 dB at +5.71 degrees. The phase is positive because a high-pass filter leads the input. The two filters are mirror images: the low-pass lags by up to 90 degrees, and the high-pass leads by up to 90 degrees.
Step 3: Test the properties, not only the numbers
Printing a table and eyeballing it works once. Turning your expectations into checks catches regressions when you change a component value. The script below tests three properties of the RC low-pass filter on a dense frequency grid.
import numpy as np
R = 1.59e3
C = 100e-9
fc = 1 / (2 * np.pi * R * C)
def h_rc(f):
return 1 / (1 + 1j * 2 * np.pi * f * R * C)
def db(h):
return 20 * np.log10(np.abs(h))
# Property 1: gain is -3.01 dB and phase is -45 degrees at fc
h = h_rc(fc)
assert abs(db(h) + 3.0103) < 0.001
assert abs(np.degrees(np.angle(h)) + 45.0) < 0.001
# Property 2: roll-off is close to -20 dB per decade well above fc
slope = db(h_rc(100 * fc)) - db(h_rc(10 * fc))
print(f"slope = {slope:.2f} dB/decade")
assert abs(slope + 20) < 0.1
# Property 3: the -3 dB crossing found on a 20-points-per-decade grid
f = np.logspace(1, 5, 81)
g = db(h_rc(f))
target = -10 * np.log10(2) # -3.0103 dB
f_cross = 10 ** np.interp(target, g[::-1], np.log10(f)[::-1])
print(f"-3 dB crossing near {f_cross:.0f} Hz, fc = {fc:.0f} Hz")
assert abs(f_cross - fc) / fc < 0.01
print("all checks passed")The [::-1] reversal is needed because np.interp requires its x values to increase, and the gain decreases with frequency. Interpolating against log frequency matches how the grid is spaced.
Step 4: Simulate the circuit
The netlist below builds the same filter from components. It uses an AC source of magnitude 1, so the node voltage at out equals H(jω) directly.
* RC low-pass filter, AC sweep
V1 in 0 AC 1
R1 in out 1.59k
C1 out 0 100n
.ac dec 20 10 100k
.endEach line does one job:
V1 in 0 AC 1is a source between nodeinand ground (node 0) with AC magnitude 1 and phase 0.R1andC1form the divider. The output node sits between them, across the capacitor..ac dec 20 10 100ksweeps 20 points per decade from 10 Hz to 100 kHz. That grid contains 81 points, and 1 kHz is one of them.
In ngspice you can run it in batch mode with a .print ac vdb(out) vp(out) line before .end. In LTspice you can instead place a probe on out and read the plot. Compare the simulated values at 100 Hz, 1 kHz, and 10 kHz with the table above. They should agree to within a few hundredths of a decibel and a fraction of a degree.
Step 5: Verify the effect of a load
Loading is a good test of a simulation because the hand formula is short. Add a load resistor equal to R across the capacitor by inserting R2 out 0 1.59k before .ac. The capacitor now sees the Thévenin equivalent resistance R || R2 = 795 Ω, and the open-circuit gain is divided by 2:
- Pass-band gain is R2 / (R + R2) = 0.5, which is -6.02 dB.
- Cutoff frequency becomes 1 / (2π · (R || R2) · C), about 2002 Hz.
The script below computes the loaded response directly from impedances, without using the shortcut formulas. It then compares the result with them.
import numpy as np
R, R2, C = 1.59e3, 1.59e3, 100e-9
f = np.logspace(1, 5, 81)
w = 2 * np.pi * f
Zc = 1 / (1j * w * C)
Zl = Zc * R2 / (Zc + R2) # capacitor in parallel with the load
H = Zl / (R + Zl) # voltage divider with the loaded branch
Req = R * R2 / (R + R2)
fc_loaded = 1 / (2 * np.pi * Req * C)
H_formula = (R2 / (R + R2)) / (1 + 1j * w * Req * C)
print(f"fc loaded = {fc_loaded:.0f} Hz")
print(f"DC gain = {20 * np.log10(abs(H[0])):.2f} dB")
print(f"max difference = {np.max(np.abs(H - H_formula)):.2e}")The maximum difference should be near machine precision, around 1e-16. The simulator should show the pass band at about -6 dB and the -3 dB point near 3 dB below that, at about 2 kHz. If the simulation and the script disagree, look for a load resistor connected to the wrong node.
Common errors when comparing results
| Symptom | Likely cause |
|---|---|
| Script and hand result differ by a factor of 6.28 in frequency | Hertz and radians per second mixed up |
| Gain is off by a factor of 2 in decibels | Used 10 log10 on a voltage ratio |
| Simulation shows 0 dB everywhere | Output node connected to the input, or the source has no `AC` magnitude |
| Simulated phase is about 0.785 where you expect -45 | Phase is reported in radians, and the sign convention was misread |
| Cutoff is off by decades | SPICE suffix error, such as `M` for mega |
| Pass-band gain is below 0 dB | A load or source resistance you forgot to include in the hand calculation |
What agreement means
Agreement to a few hundredths of a decibel at three points (a decade below, at, and a decade above fc) is enough for a first-order filter. The shape is fixed by one parameter, so matching at fc and on both sides leaves little room for error. For a cascade or a loaded network, extend the comparison to more points, because the response may no longer be a simple first-order shape.
Exercise
An RL low-pass filter has R = 100 Ω and L = 10 mH, with the output across R. Write the Python expression for H and predict the gain in dB and the phase in degrees at fc. Then state what a simulator should show at that frequency.
Show answer
The output across R gives H = R / (R + jωL) = 1 / (1 + jωL/R), with fc = R / (2πL) = 100 / (2π · 0.01) ≈ 1592 Hz. In Python: H = R / (R + 1j w L), with w = 2 np.pi f. At fc, ωL/R = 1, so |H| = 1/√2, which is -3.01 dB, and the phase is -45 degrees. A simulator with an AC source of 1 should show -3.01 dB and -45 degrees (or -0.785 rad) at node out near 1592 Hz.
Exercise
You simulate the RC low-pass filter with a 10 kΩ load added across the capacitor, and the pass-band gain reads -0.13 dB, but the cutoff moved only slightly. The source resistor is 1.59 kΩ. Is this consistent with the loading formula?
Show answer
Yes. The pass-band gain is R2 / (R + R2) = 10 / (1.59 + 10) = 0.863, which is -1.28 dB, so the reading of -0.13 dB does not match. Check the netlist: it suggests the load is not 10 kΩ, or that it is connected somewhere other than across the capacitor. For example, a load of about 100 kΩ gives 100 / 101.59 = 0.984, which is -0.14 dB. The check shows how a quick pass-band gain calculation catches a netlist error.
Exercise
Why is it not enough to check only the -3 dB frequency when verifying a filter with a script?
Show answer
A wrong formula can still cross -3 dB at the correct frequency. For example, a mistaken filter order, a missing load, or a gain error with a compensating cutoff shift can pass this single check. Checking the phase at fc, the pass-band gain, and the slope a decade or two above fc tests several independent properties of the response.