---
title: Amplifier analysis lab
heading: Small-signal parameters, cascades, exact distortion, low-frequency corners and swing, in Python
eyebrow: C5 · Lesson 47 · Amplifier analysis lab
xp: 50
---

::: hook
Lessons 39 to 46 analyzed amplifiers with formulas, each with an approximation behind it: loading worked out stage by stage, distortion from the first terms of a series, a low-frequency corner from a sum of time constants. In this lab you'll turn each into code, then check the approximations against exact calculations: a degenerated stage solved point by point for its true harmonic content, and a cascade of coupling capacitors solved for its true corner.

::: play
- A common-emitter amplifier with an input capacitor, an output capacitor and a bypassed emitter resistor. The Bode plot shows its gain from 1 Hz to 100 kHz.
- Each capacitor adds a corner at the low end. Shrink the bypass capacitor and watch the low corner move up; shrink the coupling capacitors and watch which one matters.
- The midband gain is set by the small unbypassed resistor; slide it and the flat part moves.
:::
:::

```sim circuit
VS s 0 sine 1m V 1k Hz          @ 0,1 down 4
C1 s b 10u                      @ 0,1 right 2
R1 vcc b 47k                    @ 2,-2 down 3
R2 b 0 10k                      @ 2,1 down 4
Q1 c b e npn BF=100             @ 3,1
RC vcc c 4.7k                   @ 4,-2 down 2
RE1 e e2 47                     @ 4,2 down 1
RE2 e2 0 1k                     @ 4,3 down 2
CE e2 0 100u                    @ 6,3 down 2
C2 c out 10u                    @ 4,0 right 4
RL out 0 10k                    @ 8,0 down 5
VCC vcc 0 12                    @ 10,-2 down 7
slider: CE 1u..1m log
slider: C1 1u..100u log
slider: RE1 1..1k log
scope: v(s), v(out)
time: 0..5m s
bode: v(out)/v(s) | f = 1..100k Hz
```

::: predict
## Three coupling capacitors each make a first-order corner at 10 Hz. The usual estimate adds the corners: 30 Hz. Where is the amplifier really 3 dB down?

- [ ] At 10 Hz
- [x] At about 20 Hz
- [ ] At 30 Hz
- [ ] At about 50 Hz

::: reveal
At 19.6 Hz. Each section is only 1 dB down at that frequency, and three of them make 3 dB: the exact corner of n equal sections is $\frac{f_c}{\sqrt{2^{1/n} - 1}}$. The sum of corners is exact only when one capacitor dominates, and otherwise it's conservative: the amplifier reaches lower than it claims.
:::
:::

::: nameit
## The parameters, in one place

At a collector current $I_C$ with gain β and Early voltage $V_A$:

$$g_m = \frac{I_C}{V_T}, \quad r_\pi = \frac{\beta}{g_m}, \quad r_e = \frac{\alpha}{g_m}, \quad r_o = \frac{V_A}{I_C}.$$

## Cascades from stage models

Give each stage as its input resistance, open-circuit gain and output resistance. Working from the source, each junction is a divider:

$$G_v = \frac{R_{i1}}{R_{i1} + R_\text{sig}}A_1\frac{R_{i2}}{R_{i2} + R_{o1}}A_2\cdots A_n\frac{R_L}{R_L + R_{on}}.$$

## Distortion, exactly

For a degenerated stage, the input splits as $v_\text{in} = v_{be} + R_Ei_c$ with $i_c = I_C(e^{v_{be}/V_T} - 1)$. At each sample of a sine, that one equation in $v_{be}$ is monotonic, so bisection solves it; the samples' discrete Fourier transform gives every harmonic. Lesson 46's formula, $\frac{\hat v}{4V_T(1 + g_mR_E)^2}$, is the first term of this calculation.

## Low-frequency corners, exactly

A chain of first-order high-pass sections with corners $f_k$ has $|H|^2 = \prod_k\frac{f^2}{f^2 + f_k^2}$. The −3 dB point is where that equals ½, found by bisection on a log scale; the usual estimate is $\sum_kf_k$.

## Swing

The output can rise by $I_C(R_C \parallel R_L)$ before cutoff and fall by $V_C - V_E - V_{CE,\text{sat}}$ before saturation. The $R_C$ that makes them equal solves a quadratic.
:::

::: example
## Lesson 40's cascade (two stages with $A_{vo}$ = 10, $R_i$ = 10 kΩ, $R_o$ = 2 kΩ, between a 1 kΩ source and a 1 kΩ load). Check it from the stage models.

::: solution
1. **Multiply the dividers and gains.**
   $$\begin{aligned} G_v &= \frac{10}{11} \times 10 \times \frac{10}{12} \times 10 \times \frac{1}{3} \\ &= 25.25 \end{aligned}$$
2. **What the code does.** It walks the list of stages, multiplying a divider at each junction and a gain at each stage.
3. **Why it's useful.** The same function handles any number of stages, which is how a designer explores whether a third stage or a follower pays for itself.

**Answer:** the stage models give an overall gain of 25.25, as lesson 40 found.
:::
:::

::: example
## Solve a stage at 1 mA with $R_E$ = 470 Ω exactly for a 380 mV peak input. How does its $HD_2$ compare with lesson 46's formula?

::: solution
1. **Solve the stage exactly.** Bisect
   $$v_\text{in} = v_{be} + R_EI_C(e^{v_{be}/V_T} - 1)$$
   at 4096 samples of the cosine.
2. **Take the harmonics.** The second harmonic over the fundamental gives 1.357%.
3. **Compare with the formula.** The formula gives 0.999%: about a third low, because at this amplitude the junction itself swings from −34 mV to +15 mV, which is no longer small against $V_T$.
4. **When the formula holds.** At 100 mV with $R_E$ = 1 kΩ the two agree within 0.5%, so the formula is fine for small swings and optimistic for large ones.

**Answer:** the exact $HD_2$ is 1.357%, against the formula's 0.999%.
:::
:::

::: example
## Lesson 49's amplifier has corners near 5.4 Hz ($C_1$), 47.8 Hz ($C_E$) and 1.1 Hz ($C_2$). What's the true −3 dB point, against the sum's 54.3 Hz?

::: solution
1. **Bisect for the true corner.** Solving
   $$\prod\frac{f^2}{f^2 + f_k^2} = \frac12$$
   gives 48.4 Hz.
2. **Compare with the sum.** The bypass capacitor's corner dominates, so the sum overestimates by only 12%.
3. **Three equal corners.** There the sum overestimates by 53% (30 Hz against 19.6 Hz).
4. **What it means for a design.** When the estimate meets a specification, the real amplifier meets it with room to spare; when it misses narrowly, the exact calculation may show it passes.

**Answer:** the true −3 dB point is 48.4 Hz; the sum's 54.3 Hz overestimates it by 12%.
:::
:::

::: check
kind: python

## Step 1. Write `small_signal(IC, beta, VA, VT=0.02585)` returning a dict with keys `gm`, `rpi`, `re` and `ro`.

```python starter
def small_signal(IC, beta, VA, VT=0.02585):
    return {"gm": IC / VT, "rpi": 0.0, "re": 0.0, "ro": 0.0}

print(small_signal(1e-3, 100, 100))
```

```python solution
def small_signal(IC, beta, VA, VT=0.02585):
    gm = IC / VT
    return {"gm": gm, "rpi": beta / gm, "re": beta / (beta + 1) / gm, "ro": VA / IC}

print(small_signal(1e-3, 100, 100))
```

```python tests
p = small_signal(1e-3, 100, 100)
assert abs(p["gm"] - 0.0386847) < 1e-6 and abs(p["rpi"] - 2585) < 1e-6, f"38.7 mS and 2.585 kΩ; got {p}"
assert abs(p["re"] - 25.5941) < 1e-3 and abs(p["ro"] - 1e5) < 1e-6, f"25.6 Ω and 100 kΩ; got {p}"
```

::: right
Right. Four numbers from one operating point.
:::
:::

::: check
kind: python

## Step 2. Write `cascade(Rsig, stages, RL)` returning the overall voltage gain, where `stages` is a list of `(Rin, Avo, Ro)` tuples.

```python starter
def cascade(Rsig, stages, RL):
    g = 1.0
    for Rin, Avo, Ro in stages:
        g *= Avo
    return g

print(cascade(1e3, [(1e4, 10, 2e3), (1e4, 10, 2e3)], 1e3))
```

```python solution
def cascade(Rsig, stages, RL):
    g, Rprev = 1.0, Rsig
    for Rin, Avo, Ro in stages:
        g *= Rin / (Rin + Rprev) * Avo
        Rprev = Ro
    return g * RL / (RL + Rprev)

print(cascade(1e3, [(1e4, 10, 2e3), (1e4, 10, 2e3)], 1e3))
```

```python tests
assert abs(cascade(1e3, [(1e4, 10, 2e3), (1e4, 10, 2e3)], 1e3) - 25.252525) < 1e-5, "Lesson 40: 25.25."
assert abs(cascade(1e3, [(1e4, 10, 2e3)], 1e9) - 10 / 1.1) < 1e-4, "One stage into an open circuit: 9.09."
g3 = cascade(1e5, [(1.71e5, 0.985, 1e3), (2.6e3, -182, 4.7e3)], 1e9)
assert g3 < -50, "A follower in front of a CE stage keeps most of the gain from a weak source."
```

::: right
Right. A divider at every junction, a gain at every stage.
:::
:::

::: check
kind: python

## Step 3. Write `hd2(vin, IC, RE, VT=0.02585, N=4096)` returning the exact second-harmonic distortion of a degenerated bipolar stage driven by `vin`·cos θ: solve for $v_{be}$ at each of N samples by bisection on [−1, 1] V, then compare the DFT's second and first harmonics.

```python starter
def hd2(vin, IC, RE, VT=0.02585, N=4096):
    return vin / (4 * VT * (1 + IC / VT * RE) ** 2)

print(hd2(0.38, 1e-3, 470))
```

```python solution
import numpy as np

def hd2(vin, IC, RE, VT=0.02585, N=4096):
    v = vin * np.cos(2 * np.pi * np.arange(N) / N)
    lo, hi = -np.ones(N), np.ones(N)
    for _ in range(80):
        m = (lo + hi) / 2
        f = m + RE * IC * (np.exp(m / VT) - 1) - v
        hi = np.where(f > 0, m, hi)
        lo = np.where(f > 0, lo, m)
    ic = IC * (np.exp((lo + hi) / 2 / VT) - 1)
    X = np.abs(np.fft.rfft(ic))
    return float(X[2] / X[1])

print(hd2(0.38, 1e-3, 470))
```

```python tests
assert abs(hd2(0.005, 1e-3, 0) - 4.828070e-2) < 2e-6, f"Undegenerated, 5 mV: 4.83%; got {hd2(0.005, 1e-3, 0)}"
assert abs(hd2(0.1, 1e-3, 1000) - 6.168183e-4) < 2e-8, f"1 kΩ, 100 mV: 0.0617%; got {hd2(0.1, 1e-3, 1000)}"
assert abs(hd2(0.38, 1e-3, 470) - 1.356594e-2) < 2e-6, f"470 Ω, 380 mV: 1.357%; got {hd2(0.38, 1e-3, 470)}"
```

::: right
Right. Vectorized bisection solves every sample at once, and the FFT reads off every harmonic.
:::
:::

::: check
kind: python

## Step 4. Write `f_low(corners)` returning `(estimate, exact)`: the sum of the corners, and the true −3 dB frequency of the chain of first-order high-pass sections, by bisection on a log scale.

```python starter
def f_low(corners):
    s = sum(corners)
    return s, s

print(f_low([10, 10, 10]))
```

```python solution
import math

def f_low(corners):
    def H2(f):
        p = 1.0
        for c in corners:
            p *= f * f / (f * f + c * c)
        return p
    lo, hi = min(corners) * 1e-3, sum(corners) * 10
    for _ in range(200):
        m = math.sqrt(lo * hi)
        if H2(m) > 0.5:
            hi = m
        else:
            lo = m
    return sum(corners), math.sqrt(lo * hi)

print(f_low([10, 10, 10]))
```

```python tests
import math
est, ex = f_low([10, 10, 10])
assert est == 30 and abs(ex - 10 / math.sqrt(2 ** (1 / 3) - 1)) < 1e-6, f"Three equal corners: 19.61 Hz; got {ex}"
assert abs(f_low([5.3, 47.8, 1.1])[1] - 48.401906) < 1e-4, "Lesson 49's amplifier: 48.4 Hz."
assert abs(f_low([20])[1] - 20) < 1e-6, "One section: its own corner."
```

::: right
Right. The sum is exact for one section and conservative for several.
:::
:::

::: check
kind: python

## Step 5. Write `best_rc(VCC, IC, RL, VE, vsat=0.2)` returning `(RC, swing)`: the collector resistor that equalizes the two swing limits, and that swing. Work in ohms, amperes and volts.

```python starter
def best_rc(VCC, IC, RL, VE, vsat=0.2):
    RC = (VCC - VE) / (2 * IC)
    return RC, IC * RC

print(best_rc(12, 1e-3, 1e4, 1.2))
```

```python solution
import math

def best_rc(VCC, IC, RL, VE, vsat=0.2):
    A = VCC - VE - vsat
    # (A - IC*RC)(RC + RL) = IC*RC*RL is a quadratic in RC
    a = IC
    b = 2 * IC * RL - A
    c = -A * RL
    RC = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a)
    return RC, A - IC * RC

print(best_rc(12, 1e-3, 1e4, 1.2))
```

```python tests
RC, sw = best_rc(12, 1e-3, 1e4, 1.2)
assert abs(RC - 6617.6853) < 1e-3 and abs(sw - 3.982315) < 1e-6, f"Expected 6.62 kΩ and 3.98 V; got {RC}, {sw}"
RC2, sw2 = best_rc(15, 2e-3, 4.7e3, 1.5)
assert abs(sw2 - 2e-3 * RC2 * 4.7e3 / (RC2 + 4.7e3)) < 1e-9, "Both limits must be equal."
```

::: right
Right. Setting the two limits equal centers the operating point on the signal's load line.
:::
:::

::: check
given: n = 2 | 3 | 4; f = 5 | 20 | 50
answer: f/sqrt(2^(1/n) - 1)
unit: Hz
tolerance: 0.5%

## {n} identical coupling capacitors each make a corner at {f} Hz. Where is the amplifier 3 dB down?

::: right
Right. $\frac{f_c}{\sqrt{2^{1/n} - 1}}$ = {answer}.
:::

::: misses
- [n*f] The sum of the corners overestimates when they're equal.
- [any] Solve $\left(1 + \frac{f_c^2}{f^2}\right)^n = 2$.
:::
:::

::: check
kind: parts
given: Rs = 0.6 | 1 | 10; A1 = 5 | 20; R1 = 10 | 47; O1 = 1 | 4.7; RL = 1 | 10

## A {Rs} kΩ source drives a stage with $R_i$ = {R1} kΩ, $A_{vo}$ = {A1} and $R_o$ = {O1} kΩ, which drives {RL} kΩ.

::: part
answer: R1/(R1 + Rs)
tolerance: 0.5%

### What fraction of the source reaches the input?

::: misses
- [any] $\frac{R_i}{R_i + R_\text{sig}}$.
:::
:::

::: part
answer: R1/(R1 + Rs)*A1*RL/(RL + O1)
tolerance: 0.5%

### What's the overall gain?

::: misses
- [A1*RL/(RL + O1)] Include the input divider.
- [R1/(R1 + Rs)*A1] Include the output divider.
- [any] Both dividers times $A_{vo}$.
:::
:::
:::

::: explain
## Why does adding up the corner frequencies of several coupling capacitors overestimate an amplifier's low-frequency corner?

::: model
Each first-order high-pass section is only slightly attenuating at frequencies a little above its own corner, and the sum-of-corners estimate effectively assumes the worst case at every frequency. When several sections have similar corners, their small losses combine, and the overall response reaches 3 dB down at a frequency well below the sum but above any single corner: for three equal sections, about twice one corner, not three times. When one section's corner is far above the others, it does nearly all the attenuating and the sum is close to the truth. The sum is therefore a safe upper bound for design.
:::
:::

::: practice

::: check
given: n = 2 | 5; f = 8 | 30
answer: f/sqrt(2^(1/n) - 1)
unit: Hz
tolerance: 0.5%

## {n} equal corners at {f} Hz. True −3 dB point?

::: misses
- [any] $\frac{f_c}{\sqrt{2^{1/n} - 1}}$.
:::
:::
:::
