---
title: Matching lab
heading: Designing L-networks, stub tuners and transformers in Python
eyebrow: C10 · Lesson 24 · Matching lab
xp: 50
---

::: hook
On a Smith chart, matching is a matter of sliding along circles until you reach the center. In code it's a few formulas, and code checks itself: you can design a match, then compute the input impedance of the result and confirm it really is 50 Ω. In this lab you'll write an L-network designer, the single-stub tuner (both solutions), a binomial transformer, and a check of a quarter-wave transformer's bandwidth against its formula.
:::

::: play
- The curve is the reflection seen through a quarter-wave transformer from 50 Ω to a load R, computed exactly by transforming the load through the λ/4 section at each frequency.
- The dashed line is a target reflection; the readout is the fractional band the formula predicts below it.
- The curve crosses the target exactly at the predicted band edges. In the lab you'll compute the same check in code.
:::

```sim plot
view: graph
x: x = 0..2
y: G = 0..0.6
slider: R = 60..400 step 5 = 100
slider: g = 0.02..0.3 step 0.01 = 0.1
plot: 1/sqrt(1 + (2*sqrt(50*R)/(R - 50)/cos(pi*x/2))^2) | samples: 800 | label: |Γ| through the transformer
plot: g | dashed | label: target
readout: predicted band, % = (2 - 4/pi*acos(g/sqrt(1 - g^2)*2*sqrt(50*R)/abs(R - 50)))*100 | digits: 3
```

::: predict
## A single shunt stub can match any load with a nonzero resistance. How many distinct positions along the line (within half a wavelength) work?

- [ ] One
- [x] Two
- [ ] Four
- [ ] Infinitely many

::: reveal
Two. The admittance seen moving along the line traces a circle on the Smith chart, and it crosses the circle of unit conductance twice per half wavelength. At each crossing, a stub of the right length cancels the susceptance. The two solutions differ in stub length and bandwidth; the shorter combination is usually the broader one.
:::
:::

::: nameit
## L-network

To match $R_\text{low}$ to $R_\text{high}$: $Q = \sqrt{R_\text{high}/R_\text{low} - 1}$, a series reactance $X_s = QR_\text{low}$ next to the low resistance, and a shunt reactance $X_p = R_\text{high}/Q$ across the high one, of opposite types.

## Single shunt stub

For a load $Z_L = R_L + jX_L$ on a line of $Z_0$, the distances d where the line's admittance has conductance $1/Z_0$ come from $t = \tan\beta d$:

$$t = \frac{X_L \pm \sqrt{R_L[(Z_0 - R_L)^2 + X_L^2]/Z_0}}{R_L - Z_0}$$

(or $t = -X_L/2Z_0$ when $R_L = Z_0$), with $d/\lambda = \arctan(t)/2\pi$, plus ½ if negative. The susceptance there is

$$B = \frac{R_L^2t - (Z_0 - X_Lt)(X_L + Z_0t)}{Z_0[R_L^2 + (X_L + Z_0t)^2]}$$

and a shorted stub must supply −B: its length is $l/\lambda = \arctan(1/BZ_0)/2\pi$, modulo ½.

## Binomial transformer

$\ln Z_{n+1} = \ln Z_n + 2^{-N}\binom Nn\ln(Z_L/Z_0)$ for n = 0 to N − 1, starting from $Z_0$.

## Checking

Every design here can be checked the same way: transform the load through each element with the input-impedance formula and confirm the result is $Z_0$ at the design frequency.
:::

::: example
## Find both shorted-stub solutions for $Z_L$ = 60 − j80 Ω on a 50 Ω line.

::: solution
1. **The square root in the formula for t.**
   $$\begin{aligned} &\sqrt{\frac{60[(50 - 60)^2 + 80^2]}{50}} \\ &= \sqrt{\frac{60 \times 6500}{50}} \\ &= 88.3 \end{aligned}$$
2. **The two values of t.**
   $$t = \frac{-80 \pm 88.3}{10}$$
   which is 0.832 or −16.8.
3. **Stub positions.** Those give d = 0.110λ or 0.259λ.
4. **Stub lengths.** The susceptances there need stubs of 0.095λ and 0.405λ.
5. **Check.** These are the textbook answers (Pozar's Example 5.2), and putting either pair back through the formulas gives exactly 1/50 S at the junction.

**Answer:** a 0.095λ stub at d = 0.110λ, or a 0.405λ stub at d = 0.259λ.
:::
:::

::: example
## Design a three-section binomial transformer from 50 Ω to 100 Ω.

::: solution
1. **Weights.** $\ln 2 = 0.693$, and the binomial weights for N = 3 are 1, 3, 3, 1 over 8.
2. **First section.**
   $$\begin{aligned} Z_1 &= 50e^{0.693/8}\ \Omega \\ &= 54.5\ \Omega \end{aligned}$$
3. **Second section.**
   $$\begin{aligned} Z_2 &= 50e^{0.693 \times 4/8}\ \Omega \\ &= 70.7\ \Omega \end{aligned}$$
4. **Third section.**
   $$\begin{aligned} Z_3 &= 50e^{0.693 \times 7/8}\ \Omega \\ &= 91.7\ \Omega \end{aligned}$$
   and the last eighth reaches 100 Ω.
5. **Check the symmetry.** The middle section is exactly $\sqrt{50 \times 100}$, as symmetry demands.

**Answer:** sections of 54.5 Ω, 70.7 Ω and 91.7 Ω.
:::
:::

::: check
given: R = 5 | 12.5 | 25; f = 50 | 433
answer: sqrt(50/R - 1)*R/(2*pi*f*1e6)*1e9
unit: nH
tolerance: 0.5%

## An L-network matches {R} Ω to 50 Ω at {f} MHz with a series inductor next to the load. What inductance?

::: right
Right: {answer} nH.
:::

::: misses
- [any] Not yet. $X = QR_\text{low}$ with $Q = \sqrt{50/R - 1}$, then $L = X/\omega$.
:::
:::

::: check
kind: python

## Write `l_network(Rlow, Rhigh)`: return `(Q, Xs, Xp)`, the Q, the series reactance next to Rlow and the shunt reactance across Rhigh (as positive magnitudes).

```python starter
def l_network(Rlow, Rhigh):
    return (1.0, Rlow, Rhigh)
```

```python solution
import math

def l_network(Rlow, Rhigh):
    Q = math.sqrt(Rhigh / Rlow - 1)
    return (Q, Q * Rlow, Rhigh / Q)
```

```python tests
Q, Xs, Xp = l_network(10, 50)
assert abs(Q - 2) < 1e-12 and abs(Xs - 20) < 1e-12 and abs(Xp - 25) < 1e-12, f"10 to 50 Ω: Q 2, 20 Ω series, 25 Ω shunt; got {(Q, Xs, Xp)}."
Q, Xs, Xp = l_network(5, 50)
Z = 1 / (1 / (5 + 1j * Xs) + 1 / (-1j * Xp))
assert abs(Z - 50) < 1e-9, f"Series +jXs then shunt -jXp should make 5 Ω look like 50 Ω; got {Z}."
```

::: right
Right. The check at the end, computing the network's input impedance, is the habit worth keeping.
:::
:::

::: check
kind: python

## Write `single_stub(ZL, Z0)`: both shunt shorted-stub solutions, as a sorted list of `(d, l)` in wavelengths, each between 0 and ½.

```python starter
def single_stub(ZL, Z0):
    return [(0.0, 0.0), (0.25, 0.25)]
```

```python solution
import math

def single_stub(ZL, Z0):
    RL, XL = ZL.real, ZL.imag
    if abs(RL - Z0) < 1e-12:
        ts = [-XL / (2 * Z0)]
    else:
        r = math.sqrt(RL * ((Z0 - RL) ** 2 + XL ** 2) / Z0)
        ts = [(XL + r) / (RL - Z0), (XL - r) / (RL - Z0)]
    out = []
    for t in ts:
        d = math.atan(t) / (2 * math.pi)
        if d < 0:
            d += 0.5
        B = (RL ** 2 * t - (Z0 - XL * t) * (XL + Z0 * t)) / (Z0 * (RL ** 2 + (XL + Z0 * t) ** 2))
        out.append((d, (math.atan(1 / (B * Z0)) / (2 * math.pi)) % 0.5))
    return sorted(out)
```

```python tests
import math
sol = single_stub(60 - 80j, 50)
assert len(sol) == 2, f"Two solutions expected, got {sol}."
for (d, l), (dw, lw) in zip(sol, [(0.1104, 0.0950), (0.2594, 0.4050)]):
    assert abs(d - dw) < 1e-3 and abs(l - lw) < 1e-3, f"Pozar's answers are (0.110, 0.095) and (0.260, 0.405); got {sol}."
for ZL in (60 - 80j, 20 + 30j, 150 + 0j):
    for d, l in single_stub(ZL, 50):
        t = math.tan(2 * math.pi * d)
        Zin = 50 * (ZL + 1j * 50 * t) / (50 + 1j * ZL * t)
        Y = 1 / Zin + 1 / (1j * 50 * math.tan(2 * math.pi * l))
        assert abs(Y - 1 / 50) < 1e-9, f"For {ZL}, the match at d = {d:.4f}, l = {l:.4f} gives Y = {Y}, not 0.02 S."
```

::: right
Right. Both answers check out by direct computation, which is better than any chart reading.
:::
:::

::: check
kind: python

## Write `binomial(Z0, ZL, N)`: the N section impedances of a binomial transformer, starting next to Z0.

```python starter
import math

def binomial(Z0, ZL, N):
    return [math.sqrt(Z0 * ZL)] * N
```

```python solution
import math

def binomial(Z0, ZL, N):
    out, lz, step = [], math.log(Z0), math.log(ZL / Z0)
    for n in range(N):
        lz += math.comb(N, n) / 2 ** N * step
        out.append(math.exp(lz))
    return out
```

```python tests
import math
assert all(abs(a - b) < 1e-3 for a, b in zip(binomial(50, 100, 2), [59.4604, 84.0896])), f"Two sections, 50 to 100 Ω: 59.46 and 84.09; got {binomial(50, 100, 2)}."
b3 = binomial(50, 100, 3)
assert all(abs(a - b) < 1e-4 for a, b in zip(b3, [54.5254, 70.7107, 91.7004])), f"Three sections: 54.53, 70.71, 91.70; got {b3}."
assert abs(binomial(50, 200, 1)[0] - 100) < 1e-9, "One section is the quarter-wave transformer, √(50 · 200)."
```

::: right
Right. The impedances are symmetric in the logarithm about the midpoint.
:::
:::

::: check
kind: python

## Write `qw_bandwidth(Z0, ZL, gm)`: the fractional band (as a fraction, not a percentage) of a quarter-wave transformer for |Γ| < gm, by the formula. Then write `qw_gamma(Z0, ZL, x)`: the exact |Γ| at x times the design frequency, by transforming ZL through the section.

```python starter
def qw_bandwidth(Z0, ZL, gm):
    return 0.0

def qw_gamma(Z0, ZL, x):
    return 0.0
```

```python solution
import math

def qw_bandwidth(Z0, ZL, gm):
    a = gm / math.sqrt(1 - gm ** 2) * 2 * math.sqrt(Z0 * ZL) / abs(ZL - Z0)
    return 2 - 4 / math.pi * math.acos(a)

def qw_gamma(Z0, ZL, x):
    Z1 = math.sqrt(Z0 * ZL)
    t = math.tan(math.pi / 2 * x)
    Zin = Z1 * (ZL + 1j * Z1 * t) / (Z1 + 1j * ZL * t)
    return abs((Zin - Z0) / (Zin + Z0))
```

```python tests
bw = qw_bandwidth(50, 100, 0.1)
assert abs(bw - 0.36700) < 1e-4, f"50 to 100 Ω, |Γ| < 0.1: 36.7%, got {bw}."
assert abs(qw_gamma(50, 100, 1 - bw / 2) - 0.1) < 1e-9 and abs(qw_gamma(50, 100, 1 + bw / 2) - 0.1) < 1e-9, "At the band edges the exact reflection is exactly 0.1."
assert qw_gamma(50, 100, 1) < 1e-12, "At the design frequency it's a perfect match."
assert abs(qw_gamma(50, 200, 0) - 0.6) < 1e-9, "At zero frequency the section vanishes and the load's own 0.6 remains."
```

::: right
Right. The formula and the exact transformation agree to rounding error.
:::
:::

::: explain
## Why do the two single-stub solutions usually have different bandwidths?

::: model
Both solutions give a perfect match at the design frequency, but they get there through different lengths of line and stub. Away from that frequency, every piece of line changes its electrical length in proportion to frequency, and longer pieces change more, so the admittances drift further from their design values. The solution with the shorter total length, line plus stub, usually stays closer to matched over a wider band. That's why designers compute both and pick the shorter one unless the layout forces the other.
:::
:::
