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

A3 · Lesson 17 · Lab: phasor circuits in Python

A few functions that solve any AC divider

With impedances, every AC circuit becomes Ohm's law with complex numbers, and Python's complex numbers do the arithmetic for you. In this lab you'll write a small toolkit: impedances of R, L and C, series and parallel combinations, a divider, a frequency sweep, and a solver that finds a filter's corner on its own. By the end, a filter you can write as Z's is one you can analyze at any frequency in a line or two. If you haven't started A4 yet, its lessons on functions, loops and lists (lessons 1 to 9) cover the Python used here.

  • The curve is |I| for a series RLC circuit driven by 10 V amplitude, plotted against frequency on a log axis.
  • Move R, L and C. The peak sits at f0=1/(2πLC), where the inductor and capacitor cancel and only R limits the current.
  • Lower R and watch the peak grow taller and narrower.

Predict

At the resonant frequency of a series RLC circuit, how big is the voltage across the capacitor compared with the source?

Name it

Impedances as functions

Each part becomes a one-line function of frequency:

import math

def zR(R, f): return complex(R, 0)
def zL(L, f): return 1j * 2 * math.pi * f * L
def zC(C, f): return 1 / (1j * 2 * math.pi * f * C)

Series impedances add, and parallel ones add as reciprocals:

Zseries=Z1+Z2+⋯Zparallel=11/Z1+1/Z2+⋯

A function can take any number of arguments with *zs, which collects them into a list.

Dividers and transfer functions

The output across Z2 of a divider Z1 over Z2 is H=Z2Z1+Z2. Its size |H| is the gain, 20log10⁡|H| the gain in decibels, and its angle the phase shift. Writing H as a Python function of f lets you evaluate it anywhere.

Finding a corner

A low-pass corner is where |H| has fallen to 1/2 of its low-frequency value, −3.01 dB. For a simple RC it's 1/(2πRC), but for most filters there's no formula. Bisection finds it: |H|−1/2 changes sign across the corner, so halve a bracket of frequencies until it's tight. On a log axis, bisect in the logarithm of f, taking the midpoint flofhi, so a bracket spanning decades closes evenly.

Worked example

Evaluate an RC low-pass with R = 1 kΩ and C = 100 nF at its corner, as the lab's code will.

Show the solution
  1. Corner frequency. fc=12π×1000×10−7Hz=1591.5Hz
  2. The response there. H=11+jωRC=11+j
  3. What Python gives. Python gives (0.5-0.5j): size 0.7071, or −3.01 dB, at an angle of −45°.

Answer at the 1591.5 Hz corner, H is (0.5-0.5j), a size of 0.7071 (−3.01 dB) at −45°.

Worked example

A series circuit of 100 Ω, 10 mH and 2.2 µF is driven by 10 V amplitude at 1 kHz. What current flows?

Show the solution
  1. Impedance. Z=100+j62.83+1j×0.01382=100+j62.83−j72.34=100−j9.51Ω
  2. Current. I=10Z=0.0996∠5.4°A
  3. Compare with resonance. That's almost 0.1 A, since 1 kHz is close to the resonance at 1.07 kHz, where the current would be 10/100 = 0.1 A exactly.

Answer a current of 0.0996∠5.4° A flows, almost 0.1 A.

Worked example

How many bisection steps in log⁡f narrow a corner bracket from [10 Hz, 1 MHz] to within 0.1%?

Show the solution
  1. Span of the bracket. log10⁡(106/10)=5decades
  2. Target width. Within 0.1% means a ratio of 1.001, which in decades is log10⁡1.001=0.000434
  3. Count the halvings. Each step halves the span in decades, so it takes ⌈log2⁡(5/0.000434)⌉=⌈13.49⌉=14steps

Answer 14 steps.

Check

Where should a corner-finding routine land for an RC low-pass with 4.7 kΩ and 10 nF?

Write units the way you normally would: 1.5 Hz, 1500 mHz and 0.0015 kHz are the same answer.

Check

A series RLC circuit (20 Ω, 50 mH, 10 µF) is driven at resonance with 5 V amplitude. What is the amplitude across the capacitor?

Write units the way you normally would: 1.5 V, 1500 mV and 0.0015 kV are the same answer.

Check

Write zR(R, f), zL(L, f) and zC(C, f), returning complex impedances, and series(*zs) and parallel(*zs), which combine any number of them.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write divider(z1, z2), returning H=Z2/(Z1+Z2), and rc_lowpass(R, C, f), built from your impedance functions, returning the low-pass's H at f. Then db(H) returns 20log10⁡|H| and deg(H) its angle in degrees.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write corner(H, f_lo, f_hi). H is a function of frequency for a low-pass whose |H| starts near 1. Bisect in log⁡f (midpoint flofhi) for 60 steps to find where |H|=1/2, and return that frequency. The test uses it on an RC filter and on a two-stage filter that has no tidy formula.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Check

Write rlc_current(V, R, L, C, f), returning the complex current in a series RLC circuit driven by amplitude V at f, and resonance(L, C), returning f0. Use your impedance functions.

Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.

Explain why

Why bisect in log⁡f instead of f when hunting for a filter's corner?

Practice

Keep practicing

4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.