---
title: "Analog design lab: sizing a two-stage op amp"
heading: From specifications to transconductances, currents, gain and a verified phase margin
eyebrow: S2 · Lesson 51 · Analog design lab
xp: 60
---

::: hook
Lesson 20 gave the two-stage op amp's design equations. In practice designers script them: a short program turns specifications (gain-bandwidth, load, phase margin, gm/ID choices) into transconductances, currents, gain and slew rate, then checks the result's stability numerically instead of trusting the quick estimate. In this lab you'll write that script: the sizing, an exact phase-margin calculation from the poles and zero, and a search for the nulling resistor that meets a phase-margin target.
:::

::: predict
## A two-stage op amp's quick estimate gives 60° of phase margin with the right-half-plane zero ignored. Where does the exact margin, with the zero included, land?

- [ ] Exactly 60°
- [ ] Higher: the zero adds phase
- [x] Lower: a right-half-plane zero adds phase lag
- [ ] It depends only on the DC gain

::: reveal
Lower. A right-half-plane zero raises the gain like an ordinary zero but rotates the phase the wrong way, like a pole, so the true crossover phase is worse than the pole-only estimate. In the design worked in this lab, the zero at $g_{m6}/C_c$ costs about 10°: 67° without it, 57° with it. That's what the nulling resistor buys back.
:::
:::

::: nameit
## The sizing equations

From lesson 20, with $C_c = r_cC_L$ and $p_2 = k\omega_u$:

- $g_{m1} = 2\pi\,\text{GBW}\,C_c$, $g_{m6} = k\,2\pi\,\text{GBW}\,C_L$
- $I_1 = g_{m1}/(g_m/I_D)_1$ per input transistor, $I_6 = g_{m6}/(g_m/I_D)_6$
- $A_0 = \frac{(g_m/I_D)_1}{2\lambda}\cdot\frac{(g_m/I_D)_6}{2\lambda}$, $SR = 2I_1/C_c$

## The open-loop gain

With $\omega_u = 2\pi\,\text{GBW}$, $p_1 = \omega_u/A_0$, $p_2 = g_{m6}/C_L$ and the zero at $z = \frac{1}{C_c(1/g_{m6} - R_z)}$ (positive: right half plane):

$$|A| = A_0\frac{\sqrt{1 + (\omega/z)^2}}{\sqrt{(1 + (\omega/p_1)^2)(1 + (\omega/p_2)^2)}}, \qquad \angle A = -\arctan\frac{\omega}{p_1} - \arctan\frac{\omega}{p_2} - \arctan\frac{\omega}{z}$$

A negative z (left half plane) gives phase lead automatically through the sign of the arctangent. With no zero, use z = ∞.

## Phase margin

Find the crossover $|A(\omega_c)| = 1$ by bisection on $\ln\omega$ between $p_1$ and $100\omega_u$, then $\text{PM} = 180° + \angle A(\omega_c)$.

## What you'll write

1. `size_two_stage(GBW, CL, k=2.2, cc_ratio=0.4, gmid1=15, gmid6=10, lam=0.1)`: return a dict with keys `Cc`, `gm1`, `gm6`, `I1`, `I6`, `A0`, `SR` (SI units).
2. `phase_margin(A0, wu, p2, z=math.inf)`: the exact phase margin in degrees.
3. `rz_for_pm(target, A0, wu, p2, gm6, Cc)`: the smallest $R_z$ (Ω, between 0 and 1 MΩ) whose phase margin reaches the target, by bisection.
:::

::: example
## Size an op amp for 30 MHz into 5 pF with the defaults, and find its phase margin with and without the zero.

::: solution
1. **Compensation and transconductances.** With the defaults, $C_c$ = 2 pF, $g_{m1}$ = 0.377 mS and $g_{m6}$ = 2.07 mS.
2. **Currents, gain and slew rate.** $I_1$ = 25.1 µA, $I_6$ = 207 µA, $A_0$ = 3750 and SR = 25.1 V/µs.
3. **Poles.** $\omega_u/2\pi$ = 30 MHz and $p_2/2\pi$ = 66 MHz.
4. **With $R_z$ = 0.** The zero sits at $g_{m6}/C_c$ (165 MHz), and the exact margin is 57.4°.
5. **With $R_z = 1/g_{m6}$.** That's 482 Ω, and the margin is 67.3°.
6. **Reaching 70°.** It takes $R_z$ = 620 Ω, which moves the zero into the left half plane.

**Answer:** the phase margin is 57.4° with the zero ($R_z$ = 0) and 67.3° without it ($R_z$ = 482 Ω).
:::
:::

::: example
## Why check the phase margin numerically instead of using 90° − arctan($\omega_u/p_2$)?

::: solution
1. **What the estimate assumes.** The quick estimate assumes the crossover is at $\omega_u$ and ignores the zero.
2. **The estimate.** With $p_2 = 2.2\omega_u$ it gives
   $$90° - \arctan\frac{1}{2.2} = 65.6°$$
3. **Exact, without the zero.** The exact crossover, a little below $\omega_u$, gives 67.3°.
4. **Exact, with the zero.** With the right-half-plane zero the exact answer is 57.4°: the estimate would have been 8° optimistic.
5. **The cost of checking.** The numerical check costs a few lines and removes the guesswork.

**Answer:** the quick estimate gives 65.6°, but the exact margin is 67.3° without the zero and 57.4° with it, so with the zero the estimate would have been 8° optimistic.
:::
:::

::: check
kind: python

## Step 1. Write `size_two_stage(...)` returning the dict of design values.

```python starter
import math

def size_two_stage(GBW, CL, k=2.2, cc_ratio=0.4, gmid1=15, gmid6=10, lam=0.1):
    Cc = cc_ratio * CL
    return dict(Cc=Cc, gm1=0, gm6=0, I1=0, I6=0, A0=0, SR=0)

print(size_two_stage(30e6, 5e-12))
```

```python solution
import math

def size_two_stage(GBW, CL, k=2.2, cc_ratio=0.4, gmid1=15, gmid6=10, lam=0.1):
    Cc = cc_ratio * CL
    gm1 = 2 * math.pi * GBW * Cc
    gm6 = k * 2 * math.pi * GBW * CL
    I1 = gm1 / gmid1
    I6 = gm6 / gmid6
    A0 = gmid1 / (2 * lam) * gmid6 / (2 * lam)
    SR = 2 * I1 / Cc
    return dict(Cc=Cc, gm1=gm1, gm6=gm6, I1=I1, I6=I6, A0=A0, SR=SR)

print(size_two_stage(30e6, 5e-12))
```

```python tests
import math
d = size_two_stage(30e6, 5e-12)
assert abs(d['Cc'] - 2e-12) < 1e-18, "Cc should be 0.4 × 5 pF = 2 pF."
assert abs(d['gm1'] / 3.76991e-4 - 1) < 1e-4, f"gm1 should be 0.377 mS; got {d['gm1']:.4e}."
assert abs(d['gm6'] / 2.07345e-3 - 1) < 1e-4, f"gm6 should be 2.07 mS; got {d['gm6']:.4e}."
assert abs(d['I1'] / 2.51327e-5 - 1) < 1e-4 and abs(d['I6'] / 2.07345e-4 - 1) < 1e-4, "Check the currents: gm over gm/ID."
assert abs(d['A0'] - 3750) < 1e-6, f"A0 should be 75 × 50 = 3750; got {d['A0']}."
assert abs(d['SR'] / 2.51327e7 - 1) < 1e-4, f"SR should be 2·I1/Cc = 25.1 V/µs; got {d['SR']:.4e} V/s."
e = size_two_stage(100e6, 2e-12, k=3, cc_ratio=0.5, gmid1=20, gmid6=12, lam=0.05)
assert abs(e['gm6'] / (3 * 2 * math.pi * 100e6 * 2e-12) - 1) < 1e-9 and abs(e['A0'] - 200 * 120) < 1e-6, "The other arguments must be used too."
```

::: right
Right. A few lines turn the specifications into every transconductance and current.
:::
:::

::: check
kind: python

## Step 2. Write `phase_margin(A0, wu, p2, z=math.inf)`.

```python starter
import math

def gain(w, A0, wu, p2, z=math.inf):
    p1 = wu / A0
    mag = A0 * math.sqrt(1 + (w / z)**2) / math.sqrt((1 + (w / p1)**2) * (1 + (w / p2)**2))
    ph = -math.atan(w / p1) - math.atan(w / p2) - math.atan(w / z)
    return mag, ph

def phase_margin(A0, wu, p2, z=math.inf):
    # bisect on ln(w) for |A| = 1, then 180 + phase in degrees
    return 90.0

print(phase_margin(1e5, 1.0, 2.0))
```

```python solution
import math

def gain(w, A0, wu, p2, z=math.inf):
    p1 = wu / A0
    mag = A0 * math.sqrt(1 + (w / z)**2) / math.sqrt((1 + (w / p1)**2) * (1 + (w / p2)**2))
    ph = -math.atan(w / p1) - math.atan(w / p2) - math.atan(w / z)
    return mag, ph

def phase_margin(A0, wu, p2, z=math.inf):
    lo, hi = math.log(wu / A0), math.log(100 * wu)
    for _ in range(200):
        m = 0.5 * (lo + hi)
        if gain(math.exp(m), A0, wu, p2, z)[0] > 1:
            lo = m
        else:
            hi = m
    return 180 + math.degrees(gain(math.exp(lo), A0, wu, p2, z)[1])

print(phase_margin(1e5, 1.0, 2.0))
```

```python tests
import math
wu = 2 * math.pi * 30e6
for r, want in ((1, 51.827), (2, 65.530)):
    pm = phase_margin(1e5, wu, r * wu)
    assert abs(pm - want) < 0.05, f"With p2 = {r}·ωu and no zero the margin should be {want}°; got {pm:.3f}."
pz = phase_margin(1e5, wu, 2 * wu, 5 * wu)
pl = phase_margin(1e5, wu, 2 * wu, -5 * wu)
assert pz < 65.53 - 5 and pl > 65.53 + 5, "A right-half-plane zero should cost margin; a left-half-plane one add it."
```

::: right
Right. The exact margin matches lesson 15's closed form, and the sign of the zero does what it should.
:::
:::

::: check
kind: python

## Step 3. Write `rz_for_pm(target, A0, wu, p2, gm6, Cc)`.

```python starter
import math

def gain(w, A0, wu, p2, z=math.inf):
    p1 = wu / A0
    mag = A0 * math.sqrt(1 + (w / z)**2) / math.sqrt((1 + (w / p1)**2) * (1 + (w / p2)**2))
    ph = -math.atan(w / p1) - math.atan(w / p2) - math.atan(w / z)
    return mag, ph

def phase_margin(A0, wu, p2, z=math.inf):
    lo, hi = math.log(wu / A0), math.log(100 * wu)
    for _ in range(200):
        m = 0.5 * (lo + hi)
        if gain(math.exp(m), A0, wu, p2, z)[0] > 1:
            lo = m
        else:
            hi = m
    return 180 + math.degrees(gain(math.exp(lo), A0, wu, p2, z)[1])

def rz_for_pm(target, A0, wu, p2, gm6, Cc):
    # z = 1/(Cc*(1/gm6 - Rz)); bisect Rz in [0, 1e6] for PM = target
    return 0.0

print(rz_for_pm(70, 3750, 2 * math.pi * 30e6, 2.0734511513692635e-3 / 5e-12, 2.0734511513692635e-3, 2e-12))
```

```python solution
import math

def gain(w, A0, wu, p2, z=math.inf):
    p1 = wu / A0
    mag = A0 * math.sqrt(1 + (w / z)**2) / math.sqrt((1 + (w / p1)**2) * (1 + (w / p2)**2))
    ph = -math.atan(w / p1) - math.atan(w / p2) - math.atan(w / z)
    return mag, ph

def phase_margin(A0, wu, p2, z=math.inf):
    lo, hi = math.log(wu / A0), math.log(100 * wu)
    for _ in range(200):
        m = 0.5 * (lo + hi)
        if gain(math.exp(m), A0, wu, p2, z)[0] > 1:
            lo = m
        else:
            hi = m
    return 180 + math.degrees(gain(math.exp(lo), A0, wu, p2, z)[1])

def rz_for_pm(target, A0, wu, p2, gm6, Cc):
    def pm(R):
        d = 1 / gm6 - R
        z = math.inf if d == 0 else 1 / (Cc * d)
        return phase_margin(A0, wu, p2, z)
    if pm(0.0) >= target:
        return 0.0
    lo, hi = 0.0, 1e6
    for _ in range(100):
        m = 0.5 * (lo + hi)
        if pm(m) < target:
            lo = m
        else:
            hi = m
    return hi

print(rz_for_pm(70, 3750, 2 * math.pi * 30e6, 2.0734511513692635e-3 / 5e-12, 2.0734511513692635e-3, 2e-12))
```

```python tests
import math
gm6 = 2.0734511513692635e-3; Cc = 2e-12; wu = 2 * math.pi * 30e6; p2 = gm6 / 5e-12
R = rz_for_pm(70, 3750, wu, p2, gm6, Cc)
assert abs(R / 620.18 - 1) < 0.01, f"Reaching 70° should take about 620 Ω; got {R:.1f}."
assert abs(phase_margin(3750, wu, p2, 1 / (Cc * (1 / gm6 - R))) - 70) < 0.05, "The margin at that R should be 70°."
assert rz_for_pm(50, 3750, wu, p2, gm6, Cc) == 0.0, "If R = 0 already meets the target, return 0."
```

::: right
Right. The search lands on 620 Ω, a little above $1/g_{m6}$ = 482 Ω: the zero moves just into the left half plane and buys the last few degrees.
:::
:::

::: check
given: g = 10 | 50 | 200; C = 1 | 5 | 20; r = 0.2 | 0.5
answer: 2*pi*g*1e6*r*C*1e-12*1000
unit: mS
tolerance: 1%

## For GBW = {g} MHz into {C} pF with $C_c$ = {r}·$C_L$, what $g_{m1}$ does the input pair need?

::: right
Right. $2\pi\,\text{GBW}\,C_c$ = {answer} mS.
:::

::: misses
- [2*pi*g*1e6*C*1e-12*1000] Use $C_c$, not $C_L$.
- [any] Not yet. $\omega_u = g_{m1}/C_c$.
:::
:::

::: check
given: g = 0.3 | 1 | 3; C = 1 | 3; R = 0 | 0.2
answer: 1/(C*1e-12*(1/(g*1e-3) - R*1e3))/(2*pi)/1e6
unit: MHz
tolerance: 1%

## With $g_{m6}$ = {g} mS, $C_c$ = {C} pF and $R_z$ = {R} kΩ, where's the zero (positive means right half plane)?

::: right
Right. $\frac{1}{2\pi C_c(1/g_{m6} - R_z)}$ = {answer} MHz.
:::

::: misses
- [any] Not yet. Include $R_z$ in the zero's formula.
:::
:::

::: check
given: r = 1.5 | 2.2 | 3
answer: 90 - atan(1/r)*180/pi
unit: °
tolerance: 1%

## What's the quick-estimate phase margin for $p_2$ = {r}$\omega_u$ (no zero)?

::: right
Right. $90° - \arctan(\omega_u/p_2)$ = {answer}°, a little below the exact value.
:::

::: misses
- [any] Not yet. The second pole's lag at $\omega_u$.
:::
:::

::: explain
## Why is a numerical phase-margin check worth writing, when the design equations already target a margin?

::: model
The design equations use approximations: they place the crossover exactly at the unity-gain frequency, treat the second pole as $g_{m6}/C_L$, and often leave out the right-half-plane zero and any higher poles. Each approximation shifts the margin a few degrees, and they don't all point the same way. The zero alone can cost ten degrees. A numerical check evaluates the actual gain and phase of the modeled transfer function at its true crossover, so it catches those errors before a simulation or a test chip does, and it turns choices like the nulling resistor into a quick search instead of a guess.
:::
:::

::: practice

::: check
given: g = 20 | 80; C = 2 | 10; r = 0.3 | 0.6
answer: 2*pi*g*1e6*r*C*1e-12*1000
unit: mS
tolerance: 1%

## $g_{m1}$ for {g} MHz, {C} pF, $C_c$ = {r}·$C_L$?

::: misses
- [any] $2\pi\,\text{GBW}\,C_c$.
:::
:::

::: check
given: k = 2 | 3; g = 10 | 50; C = 3 | 8
answer: k*2*pi*g*1e6*C*1e-12*1000
unit: mS
tolerance: 1%

## $g_{m6}$ for $p_2$ = {k}·$\omega_u$, GBW {g} MHz, {C} pF load?

::: misses
- [any] $k\,2\pi\,\text{GBW}\,C_L$.
:::
:::

::: check
given: r = 1 | 1.5 | 1.8 | 2 | 2.5 | 3 | 4
answer: 90 - atan(1/r)*180/pi
unit: °
tolerance: 1%

## Quick PM for $p_2$ = {r}$\omega_u$?

::: misses
- [any] $90° - \arctan(1/r)$.
:::
:::
:::
