---
title: Feedback lab
heading: Loop gain, phase margin and peaking in NumPy
eyebrow: C6 · Lesson 51 · Feedback lab
xp: 50
---

::: hook
Stability analysis comes down to one function, the loop gain $L(f) = A(f)\beta$. Where its magnitude crosses one, its phase tells you how close the loop is to oscillating, and the closed-loop response's peak shows the same thing from the outside. In this lab you'll compute the loop gain of a multi-pole amplifier, find its crossover and phase margin by bisection, and measure the closed-loop peaking, then use them to see how much margin it takes to get a flat response.
:::

::: play
- The closed-loop response of an amplifier with $A_0 = 10^5$, a dominant pole at 10 Hz and a second pole at $f_2$, with feedback factor β.
- With β = 1 (a follower) and $f_2$ = 1 MHz the response peaks about 1.2 dB before rolling off: 52° of margin.
- Raise $f_2$ or lower β: the peak disappears. Lower $f_2$: it grows into a sharp resonance, a loop about to ring.
:::
:::

```sim plot
view: bode
x: f = 1k..100M Hz log
slider: beta = 0.01..1 = 1 log
slider: f2 = 0.1..10 = 1 MHz log
H: (1e5/((1 + j*f/10)*(1 + j*f/(f2*1e6))))/(1 + beta*1e5/((1 + j*f/10)*(1 + j*f/(f2*1e6))))
readout: phase margin, ° = 90 - atan(sqrt((sqrt(1 + 4*(beta*1e6/(f2*1e6))^2) - 1)/2))*180/pi | digits: 3
```

::: predict
## A follower's loop gain has a dominant pole and a second pole at the follower's GBW. How much does its closed-loop response peak?

- [ ] Not at all
- [x] About 1 dB
- [ ] About 6 dB
- [ ] It's infinite: it oscillates

::: reveal
About 1.2 dB. That loop has about 52° of phase margin; the closed-loop magnitude rises a little above its DC value just before rolling off. Peaking appears when the margin falls below about 65°, and grows quickly below 45°.
:::
:::

::: nameit
## The loop gain

For an amplifier with DC gain $A_0$ and poles $p_k$ (in hertz), with feedback β:

$$L(f) = A_0\beta\prod_k\frac{1}{1 + jf/p_k}$$

In NumPy, start with $A_0\beta$ as a complex array the length of `f` and divide by each factor.

## Crossover and phase margin

$|L|$ falls monotonically, so bisection finds the crossover $f_c$ where $|L| = 1$; search on $\log_{10}f$ so the steps are even across decades. The phase margin is

$$\text{PM} = 180° + \angle L(f_c) = 180° - \sum_k\arctan\frac{f_c}{p_k}$$

computed from the arctangents, since `np.angle` wraps to ±180° and can't tell 190° of lag from −170°.

## Closed-loop peaking

The closed-loop gain is $G = \frac{A}{1 + A\beta} = \frac{L/\beta}{1 + L}$. Sample it densely on a logarithmic grid and compare its largest magnitude with its DC value $\frac{A_0}{1 + A_0\beta}$. For two poles, about 65° of margin gives a maximally flat response, 45° gives 2.3 dB of peaking, and 30° about 6 dB.
:::

::: example
## A follower ($A_0 = 10^5$, poles at 10 Hz and 1 MHz). Find its crossover and phase margin.

::: solution
1. **Crossover.** Bisection gives $f_c$ = 786 kHz.
2. **Phase from each pole.**
   $$\begin{aligned} \arctan(78{,}600) &\approx 90° \\ \arctan(0.786) &= 38.2° \end{aligned}$$
   so PM = 51.8°.
3. **Compare the quick estimate.** With $f_c$ at GBW (1 MHz) it gives 45°; the exact crossover is lower, so the margin is better.

**Answer:** the loop crosses over at 786 kHz with a phase margin of 51.8°.
:::
:::

::: example
## The same amplifier with β = 0.1 (a gain of 10). Find its margin.

::: solution
1. **Crossover.** The loop gain is ten times smaller, so it crosses one at 99.5 kHz, a decade below the second pole.
2. **Phase margin.** PM = 84.3°, and the closed-loop response doesn't peak at all.
3. **The pattern.** Higher closed-loop gain means less loop gain and more margin.

**Answer:** the phase margin is 84.3°, with crossover at 99.5 kHz.
:::
:::

::: check
given: A0 = 1e4 | 1e5; beta = 0.01 | 0.5; p1 = 10 | 100; f = 1 | 50
answer: A0*beta/sqrt(1 + (f*1000/p1)^2)
tolerance: 0.5%

## A single-pole amplifier ($A_0$ = {A0}, pole at {p1} Hz) has β = {beta}. What is $|L|$ at {f} kHz?

::: right
Right. $\frac{A_0\beta}{\sqrt{1 + (f/p_1)^2}}$ = {answer}.
:::

::: misses
- [any] Not yet. Divide $A_0\beta$ by $|1 + jf/p_1|$.
:::
:::

::: check
given: fc = 0.5 | 2; p2 = 1 | 5; p3 = 10 | 40
answer: 180 - 90 - atan(fc/p2)*180/pi - atan(fc/p3)*180/pi
unit: °
tolerance: 0.5%

## A loop crosses over at {fc} MHz, far above its dominant pole, with further poles at {p2} MHz and {p3} MHz. What is its phase margin?

::: right
Right. $180° - 90° - \arctan(f_c/p_2) - \arctan(f_c/p_3)$ = {answer}.
:::

::: misses
- [180 - atan(fc/p2)*180/pi - atan(fc/p3)*180/pi] The dominant pole contributes its full 90° at crossover.
- [any] Not yet. Subtract every pole's lag from 180°.
:::
:::

::: check
kind: python

## Write `loop_gain(f, A0, beta, poles)` (complex $L$ at the frequencies `f`) and `phase_margin(A0, beta, poles)` returning `(fc, pm)`: the crossover by bisection on $\log_{10}f$ between 1 Hz and 10¹² Hz, and the margin from the arctangents.

```python starter
import numpy as np

def loop_gain(f, A0, beta, poles):
    f = np.asarray(f, dtype=float)
    return A0 * beta / (1 + 1j * f / poles[0])

def phase_margin(A0, beta, poles):
    fc = A0 * beta * poles[0]
    return fc, 90.0
```

```python solution
import numpy as np

def loop_gain(f, A0, beta, poles):
    f = np.asarray(f, dtype=float)
    L = A0 * beta * np.ones_like(f, dtype=complex)
    for p in poles:
        L = L / (1 + 1j * f / p)
    return L

def phase_margin(A0, beta, poles):
    lo, hi = 0.0, 12.0
    for _ in range(200):
        m = (lo + hi) / 2
        if abs(loop_gain([10 ** m], A0, beta, poles)[0]) > 1:
            lo = m
        else:
            hi = m
    fc = 10 ** ((lo + hi) / 2)
    return fc, 180 - sum(np.degrees(np.arctan(fc / p)) for p in poles)
```

```python tests
import numpy as np
L = loop_gain([1e3, 1e6], 1e5, 1.0, [10, 1e6])
assert np.allclose(L, [8.99909109 - 999.909009j, -0.499995 - 0.500005j], rtol=1e-6), f"loop_gain should include every pole; got {L}."
cases = [((1e5, 1.0, [10, 1e6]), 786151.3777114018, 51.82802118819117),
         ((1e5, 1.0, [10, 3e6]), 953061.8621602578, 72.37621029577365),
         ((1e5, 0.1, [10, 1e6]), 99508.54867924369, 84.3230453855965),
         ((2e4, 1.0, [100, 2e6, 2e7]), 1568815.8272178683, 47.407591844321985)]
for args, wfc, wpm in cases:
    fc, pm = phase_margin(*args)
    assert abs(fc / wfc - 1) < 1e-6, f"For {args}: crossover should be {wfc:.0f} Hz, got {fc:.0f} Hz."
    assert abs(pm - wpm) < 1e-4, f"For {args}: phase margin should be {wpm:.3f}°, got {pm:.3f}°."
```

::: right
Right. The starter used only the dominant pole, which always reports 90° of margin: a single-pole loop can't ring, so it hides exactly the poles that matter.
:::
:::

::: check
kind: python

## Write `peaking_db(A0, beta, poles)`: the closed-loop gain's maximum over `np.logspace(0, 12, 200001)` Hz, in dB relative to its DC value $A_0/(1 + A_0\beta)$.

```python starter
import numpy as np

def loop_gain(f, A0, beta, poles):
    f = np.asarray(f, dtype=float)
    L = A0 * beta * np.ones_like(f, dtype=complex)
    for p in poles:
        L = L / (1 + 1j * f / p)
    return L

def peaking_db(A0, beta, poles):
    f = np.logspace(0, 12, 200001)
    L = loop_gain(f, A0, beta, poles)
    return float(20 * np.log10(np.max(np.abs(L))))
```

```python solution
import numpy as np

def loop_gain(f, A0, beta, poles):
    f = np.asarray(f, dtype=float)
    L = A0 * beta * np.ones_like(f, dtype=complex)
    for p in poles:
        L = L / (1 + 1j * f / p)
    return L

def peaking_db(A0, beta, poles):
    f = np.logspace(0, 12, 200001)
    L = loop_gain(f, A0, beta, poles)
    G = L / beta / (1 + L)
    G0 = A0 / (1 + A0 * beta)
    return float(20 * np.log10(np.max(np.abs(G)) / G0))
```

```python tests
cases = [((1e5, 1.0, [10, 1e6]), 1.2493583941700295), ((1e5, 1.0, [10, 3e6]), 0.0), ((1e5, 0.1, [10, 1e6]), 0.0), ((2e4, 1.0, [100, 2e6, 2e7]), 1.8999402416091817)]
for args, want in cases:
    got = peaking_db(*args)
    assert abs(got - want) < 0.01, f"For {args}: peaking should be {want:.3f} dB, got {got:.3f} dB."
```

::: right
Right. The 52° follower peaks by 1.25 dB and the 72° one not at all, so a margin of about 60° or more gives a flat closed-loop response.
:::
:::

::: explain
## Why does a smaller phase margin show up as peaking in the closed-loop response?

::: model
The closed-loop gain is $A/(1 + A\beta)$, and near crossover the loop gain has a magnitude close to one. If its phase there is close to −180°, then $1 + A\beta$ is the sum of 1 and a vector pointing nearly the opposite way, so it becomes small, and dividing by a small number makes the closed-loop gain rise above its low-frequency value. With a large phase margin the loop gain's vector points well away from −1 and $1 + A\beta$ never gets small. Peaking in frequency is the same thing as ringing in time: both come from closed-loop poles close to the imaginary axis.
:::
:::

::: practice

::: check
given: A0 = 1e4 | 1e6; beta = 0.001 | 1
answer: A0/(1 + A0*beta)
tolerance: 0.5%

## What is the DC closed-loop gain for $A_0$ = {A0} and β = {beta}?

::: misses
- [any] $A_0/(1 + A_0\beta)$.
:::
:::

::: check
given: x = 0.2 | 1 | 3
answer: atan(x)*180/pi
unit: °
tolerance: 0.5%

## How much phase does a pole contribute at {x} times its frequency?

::: misses
- [any] $\arctan(f/p)$.
:::
:::

::: check
given: dB = 1 | 3 | 6
answer: 10^(dB/20)
tolerance: 0.5%

## A peak of {dB} dB is what ratio above the DC gain?

::: misses
- [any] $10^{dB/20}$.
:::
:::
:::
