Sub topic 12
Checking results with a script and an AC simulation
This chapter assumes you can derive H(jω) for a first-order filter, convert gain to decibels, and read the cutoff frequency from component values. Here you check those hand results two ways. One is a short numerical script. The other is a SPICE AC sweep.
Why check twice
A hand calculation can hide slips: a wrong unit prefix, a swapped R and C, or a phase sign error. A script evaluates your formula at many points. A simulator solves the circuit itself, without using your formula. If both agree with your algebra, the result is probably right. If they disagree, the mismatch tells you where to look.
Step 1: Check the RC low-pass with a script
The script uses R = 1.59 kΩ and C = 100 nF. It computes the cutoff frequency, then evaluates H(jω) = 1 / (1 + jωRC) at one point per decade.
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")Expect fc of about 1000.9 Hz. Check these sanity points:
- At 10 Hz the gain is close to 0 dB and the phase is close to 0°.
- At 1 kHz, near fc, the gain is about -3 dB and the phase is about -45°.
- At 100 kHz the gain is about -40 dB and the phase approaches -90°.
The one-per-decade grid does not land exactly on fc, so the 1 kHz row is a little off -3.01 dB. To test the cutoff exactly, evaluate the script at f = fc.
Step 2: Check an RL high-pass the same way
For the RL high-pass, the output is taken across the inductor. The transfer function is H(jω) = jωL / (R + jωL). The script below tests one decade below, at, and one decade above fc.
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")At fc the output should show about -3 dB and +45°. One decade below, the gain is about -20 dB. One decade above, it is close to 0 dB. A high-pass filter has positive phase, so a negative sign here points to an error in your formula.
Step 3: Check the circuit with an AC simulation
SPICE runs a small-signal AC analysis. It replaces each component with its impedance at each frequency and solves the network. The netlist below sweeps the same RC low-pass filter.
* 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 with a 1 V AC amplitude. Because the input is 1, the output at nodeoutequals H directly.R1andC1form the divider, with the output taken across the capacitor..ac dec 20 10 100ksweeps from 10 Hz to 100 kHz with 20 points per decade.
Plot the magnitude of out in dB and its phase in degrees. Then compare them with the script.
Comparing the results
| Quantity | Hand and script | Simulation | Action if they differ |
|---|---|---|---|
| fc | about 1.0 kHz | -3 dB point on the plot | Recheck units and prefixes |
| Gain at fc | about -3 dB | about -3 dB | Recheck the divider formula |
| Phase at fc | -45° (low-pass) | -45° | Recheck the sign of the phase |
| Slope past fc | -20 dB/decade | -20 dB/decade | Confirm the filter is first order |
Exercise
You run the RL high-pass script and see +45° and -3 dB at fc, but the simulation shows -45° at the same frequency. Name one likely cause in the simulation.
Show answer
The simulated output node is probably across the resistor instead of the inductor, or the netlist builds a low-pass circuit. Across R the circuit acts as a low-pass filter with a negative phase, so the output must be taken across L to match the high-pass formula.
Exercise
The script gives fc of about 1 kHz for R = 1.59 kΩ and C = 100 nF, but your netlist uses 100m for the capacitor. What happens to the simulated cutoff?
Show answer
In SPICE, m means milli, so 100m is 0.1 F instead of 100 nF. That is 10^6 times too large, so the simulated cutoff drops by a factor of 10^6 to about 1 mHz, far below the 10 Hz start of the sweep. The response would look like a flat roll-off across the whole sweep.