---
title: "Project: matching a 2.4 GHz antenna"
heading: Designing a stub match and checking its bandwidth in Python
eyebrow: C10 · Lesson 52 · Project
xp: 80
---

::: hook
A small 2.4 GHz antenna on a sensor board, measured on a network analyzer, shows 35 − j11.5 Ω at the Wi-Fi band's center instead of 50 Ω: it's resonant a little high and its radiation resistance is low. The radio would reflect about 5% of its power at the center of the band and far more toward its edges, and lose range. The fix is a single shorted stub on the 50 Ω feed line, placed and sized to cancel the mismatch. In this project you'll write the three pieces an RF engineer would: the line's input impedance, a stub designer that works for any load, and a frequency sweep that shows how wide the match is, because a match that's perfect at one frequency and poor across the band isn't good enough.
:::

::: play
- The curve on the Smith chart is the antenna's impedance from 2.2 to 2.7 GHz, modeled as a series RLC: resistance R, quality factor Q, resonant at $f_r$. The point is the impedance at the design frequency $f_0$, with its VSWR circle.
- At resonance the curve crosses the horizontal axis at R. Raise Q and the curve sweeps faster with frequency: the antenna is narrower-band, and any match will be too.
- The project designs a stub match at $f_0$, which moves that point to the center, then sweeps frequency to find where the return loss stays above 10 dB.
:::
:::

```sim plot
view: smith
z0: 50
x: f = 2.2G..2.7G Hz log
slider: R = 10..100 = 35 step 1 Ω
slider: Q = 1..20 = 8 step 0.5
slider: fr = 2.3..2.6 = 2.45 step 0.005 GHz
slider: f0 = 2.3..2.6 = 2.4 step 0.005 GHz
plot: R + j*Q*R*(f/(fr*1e9) - fr*1e9/f) | label: antenna
z: R + j*Q*R*(f0/fr - fr/f0) | label: at f₀ | vswr
readout: |Γ| at f₀ = abs((R + j*Q*R*(f0/fr - fr/f0) - 50)/(R + j*Q*R*(f0/fr - fr/f0) + 50)) | digits: 3
```

::: predict
## The antenna presents 35 − j11.5 Ω at 2.4 GHz on a 50 Ω feed. What return loss does the radio see without a match?

- [ ] 3 dB
- [x] About 13 dB
- [ ] About 30 dB
- [ ] Infinite

::: reveal
$\lvert\Gamma\rvert = \lvert(-15 - j11.5)/(85 - j11.5)\rvert = 18.9/85.8 = 0.22$, a return loss of 13.1 dB: about 5% of the power reflected. Acceptable, but the antenna's own Q means that away from 2.4 GHz it gets much worse; the project's bandwidth sweep makes that precise.
:::
:::

::: nameit
## The pieces

- **Input impedance** of a lossless line of electrical length βl: $Z_\text{in} = Z_0(Z_L + jZ_0\tan\beta l)/(Z_0 + jZ_L\tan\beta l)$.
- **Stub match for a complex load** $Z_L = R_L + jX_L$ (shunt shorted stub): with $t = \tan\beta d$, the positions where the admittance's real part is $Y_0$ satisfy

$$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$ if $R_L = Z_0$), so $d/\lambda = \tan^{-1}(t)/2\pi$, plus 1/2 if negative. At that point the admittance is $Y_0 + jB$; a shorted stub has admittance $-jY_0\cot\beta l_s$, so canceling B needs $l_s/\lambda = \tan^{-1}(Y_0/B)/2\pi$ (plus 1/2 if negative). Any method that lands on the center, such as scanning d for Re Y = $Y_0$, works too.
- **Frequency sweep:** the line lengths are fixed in meters, so their electrical lengths scale with frequency, $d(f) = d_0 f/f_0$. The antenna model is $Z(f) = R + jQR(f/f_r - f_r/f)$.

## Bandwidth

Sweep f, compute the total impedance the radio sees, and find the continuous range around $f_0$ where the return loss stays at or above 10 dB (|Γ| ≤ 0.316). That range is the **matched bandwidth**, and comparing it with the band the radio needs (2.400 to 2.4835 GHz for Wi-Fi) is the pass-or-fail test.
:::

::: example
## Design a stub match for 100 Ω on a 50 Ω line by the formula.

::: solution
1. **The value of t.** With $R_L = 100$ and $X_L = 0$,
   $$\begin{aligned} t &= \frac{\sqrt{100 \times 2500/50}}{50} \\ &= 1.414 \end{aligned}$$
2. **Stub position.**
   $$\begin{aligned} d &= \frac{\tan^{-1}(1.414)}{2\pi} \\ &= 0.152\lambda \end{aligned}$$
3. **Stub length.** There $Y = (1 + j0.707)/50$, so
   $$\begin{aligned} l_s &= \frac{\tan^{-1}(1/0.707)}{2\pi} \\ &= 0.152\lambda \end{aligned}$$
   as in lesson 16.

**Answer:** a 0.152λ shorted stub placed 0.152λ from the load.
:::
:::

::: example
## Why does a higher-Q antenna give a narrower matched bandwidth?

::: solution
1. **How the reactance moves.** Near resonance the antenna's reactance changes by
   $$QR\left(\frac{f}{f_r} - \frac{f_r}{f}\right) \approx 2QR\,\frac{\Delta f}{f_r}$$
   so a higher Q swings the impedance across the Smith chart faster with frequency.
2. **What the stub can fix.** The stub match only corrects one point exactly, so the impedance leaves the 10 dB circle sooner.
3. **The scaling.** Roughly, the fractional bandwidth falls as 1/Q.

**Answer:** a higher Q makes the impedance move away from the single matched point faster, so the matched bandwidth shrinks roughly as 1/Q.
:::
:::

::: check
given: R = 20 | 35 | 60; X = -30 | -11.5 | 25
answer: -20*log(sqrt(((R - 50)^2 + X^2)/((R + 50)^2 + X^2)))
unit: dB
tolerance: 0.5%

## An antenna measures {R} + j({X}) Ω on a 50 Ω feed. What is its return loss before matching?

::: right
Right. {answer} dB.
:::

::: misses
- [any] Not yet. $-20\log_{10}\lvert(Z - Z_0)/(Z + Z_0)\rvert$.
:::
:::

::: check
given: R = 35 | 50; Q = 4 | 8 | 15; fr = 2.45 | 2.5; f = 2.4
answer: Q*R*(f/fr - fr/f)
unit: Ω
tolerance: 0.5%

## A series-RLC antenna model has R = {R} Ω, Q = {Q}, resonant at {fr} GHz. What is its reactance at {f} GHz?

::: right
Right. $QR(f/f_r - f_r/f)$ = {answer} Ω.
:::

::: misses
- [any] Not yet. Evaluate the model's reactance.
:::
:::

::: check
kind: python

## Write `zin(zl, z0, bl)`: the complex input impedance of a lossless line of characteristic impedance z0 and electrical length bl (radians) ending in the complex load zl.

```python starter
def zin(zl, z0, bl):
    return zl
```

```python solution
import math

def zin(zl, z0, bl):
    t = math.tan(bl)
    return z0 * (zl + 1j * z0 * t) / (z0 + 1j * zl * t)
```

```python tests
import math
assert abs(zin(100, 50, math.pi / 2) - 25) < 1e-9, "A quarter-wave 50 ohm line turns 100 ohms into 25."
assert abs(zin(100, 50, math.pi / 4) - (40 - 30j)) < 1e-9, f"An eighth-wave line: 40 - j30 ohms, got {zin(100, 50, math.pi / 4)}."
assert abs(zin(30 + 40j, 50, math.pi) - (30 + 40j)) < 1e-9, "A half-wave line repeats its load."
assert abs(zin(50, 50, 1.234) - 50) < 1e-9, "A matched line looks like Z0 at any length."
assert abs(zin(0, 50, 0.3) - 50j * math.tan(0.3)) < 1e-9, "A shorted line is j Z0 tan(beta l)."
```

::: right
Right. That one function is the heart of every line calculation in this course.
:::
:::

::: check
kind: python

## Write `stub_match(zl, z0)`: return `(d, ls)`, the distance from the load to a shunt shorted stub and the stub's length, both in wavelengths between 0 and 0.5, that match the complex load zl to z0.

```python starter
def stub_match(zl, z0):
    return 0.0, 0.25
```

```python solution
import math

def zin(zl, z0, bl):
    t = math.tan(bl)
    return z0 * (zl + 1j * z0 * t) / (z0 + 1j * zl * t)

def stub_match(zl, z0):
    RL, XL = zl.real, zl.imag
    if abs(RL - z0) < 1e-12:
        t = -XL / (2 * z0)
    else:
        t = (XL + math.sqrt(RL * ((z0 - RL) ** 2 + XL ** 2) / z0)) / (RL - z0)
    d = math.atan(t) / (2 * math.pi)
    if d < 0:
        d += 0.5
    B = (1 / zin(zl, z0, 2 * math.pi * d)).imag
    ls = math.atan(1 / (z0 * B)) / (2 * math.pi)
    if ls < 0:
        ls += 0.5
    return d, ls
```

```python tests
import math
def check_zin(zl, z0, bl):
    t = math.tan(bl)
    return z0 * (zl + 1j * z0 * t) / (z0 + 1j * zl * t)
def total(zl, z0, d, ls):
    z1 = check_zin(zl, z0, 2 * math.pi * d)
    ys = 1 / (1j * z0 * math.tan(2 * math.pi * ls))
    return 1 / (1 / z1 + ys)
for zl in (100 + 0j, 25 + 0j, 30 + 40j, 80 - 60j, 50 + 30j, 35 - 11.5476j, 10 + 5j):
    d, ls = stub_match(complex(zl), 50.0)
    assert 0 <= d <= 0.5 and 0 < ls <= 0.5, f"For {zl} the lengths should be between 0 and 0.5 wavelengths; got d = {d}, ls = {ls}."
    z = total(complex(zl), 50.0, d, ls)
    g = abs((z - 50) / (z + 50))
    assert g < 1e-6, f"For a load of {zl} ohms, your stub at d = {d:.4f} with ls = {ls:.4f} leaves |Gamma| = {g:.4f}; it should match."
```

::: right
Right. Any load with a positive resistance can be matched this way, from anywhere on the chart.
:::
:::

::: check
kind: python

## Write `matched_band(R, Q, fr, f0, z0=50.0)`: model the antenna as $Z(f) = R + jQR(f/f_r - f_r/f)$, design a stub match at f0 (use your `stub_match`, or the solution's method), then sweep f from 0.8 f0 to 1.2 f0 in 1 MHz steps, scaling the electrical lengths by f/f0, and return `(f_low, f_high)`: the edges of the continuous range around f0 where the return loss is at least 10 dB.

```python starter
def matched_band(R, Q, fr, f0, z0=50.0):
    return f0, f0
```

```python solution
import math

def zin(zl, z0, bl):
    t = math.tan(bl)
    return z0 * (zl + 1j * z0 * t) / (z0 + 1j * zl * t)

def stub_match(zl, z0):
    RL, XL = zl.real, zl.imag
    if abs(RL - z0) < 1e-12:
        t = -XL / (2 * z0)
    else:
        t = (XL + math.sqrt(RL * ((z0 - RL) ** 2 + XL ** 2) / z0)) / (RL - z0)
    d = math.atan(t) / (2 * math.pi)
    if d < 0:
        d += 0.5
    B = (1 / zin(zl, z0, 2 * math.pi * d)).imag
    ls = math.atan(1 / (z0 * B)) / (2 * math.pi)
    if ls < 0:
        ls += 0.5
    return d, ls

def matched_band(R, Q, fr, f0, z0=50.0):
    ant = lambda f: R + 1j * Q * R * (f / fr - fr / f)
    d, ls = stub_match(ant(f0), z0)
    ok = []
    f = 0.8 * f0
    while f <= 1.2 * f0 + 1:
        s = f / f0
        z1 = zin(ant(f), z0, 2 * math.pi * d * s)
        z = 1 / (1 / z1 + 1 / (1j * z0 * math.tan(2 * math.pi * ls * s)))
        g = abs((z - z0) / (z + z0))
        ok.append((f, g <= 10 ** (-10 / 20)))
        f += 1e6
    i0 = min(range(len(ok)), key=lambda i: abs(ok[i][0] - f0))
    lo = hi = i0
    while lo > 0 and ok[lo - 1][1]:
        lo -= 1
    while hi < len(ok) - 1 and ok[hi + 1][1]:
        hi += 1
    return ok[lo][0], ok[hi][0]
```

```python tests
lo, hi = matched_band(35, 8, 2.45e9, 2.4e9)
assert abs(lo - 2.318e9) < 3e6 and abs(hi - 2.496e9) < 3e6, f"The R = 35, Q = 8 antenna matched at 2.4 GHz should stay above 10 dB return loss from about 2.318 to 2.496 GHz; got {lo / 1e9:.3f} to {hi / 1e9:.3f} GHz."
lo2, hi2 = matched_band(70, 4, 2.4e9, 2.4e9)
assert abs(lo2 - 2.165e9) < 3e6 and abs(hi2 - 2.605e9) < 3e6, f"A lower-Q antenna (Q = 4) should give a wider band, about 2.165 to 2.605 GHz; got {lo2 / 1e9:.3f} to {hi2 / 1e9:.3f} GHz."
assert lo <= 2.4e9 <= hi and hi - lo > 83.5e6, "The design should cover the whole 2.4 GHz Wi-Fi band, 83.5 MHz wide."
```

::: right
Right. 178 MHz of bandwidth comfortably covers Wi-Fi's 83.5 MHz; a higher-Q antenna would need a broader matching network.
:::
:::

::: explain
## Why is a single-stub match narrowband, even when it's perfect at the design frequency?

::: model
The match depends on two electrical lengths, the stub's position and its length, which are fixed in meters, so their electrical lengths change in proportion to frequency. Away from the design frequency, the line no longer rotates the load to exactly the g = 1 circle, and the stub no longer supplies exactly the right susceptance. On top of that, the antenna's own impedance moves with frequency, faster for higher Q. The errors grow on both sides of the design point, so the region of good return loss is a band whose width shrinks as the antenna's Q grows and as the stub and line get electrically longer.
:::
:::

::: practice

::: check
given: R = 40 | 75; X = -20 | 20
answer: -20*log(sqrt(((R - 50)^2 + X^2)/((R + 50)^2 + X^2)))
unit: dB
tolerance: 0.5%

## Return loss of {R} + j({X}) Ω on 50 Ω?

::: misses
- [any] $-20\log_{10}\lvert\Gamma\rvert$.
:::
:::

::: check
given: g = 0.1 | 0.316 | 0.5
answer: -20*log(g)
unit: dB
tolerance: 0.5%

## Return loss for |Γ| = {g}?

::: misses
- [any] $-20\log_{10}\lvert\Gamma\rvert$.
:::
:::

::: check
given: f0 = 2.4 | 5.5; lo = 2.3 | 5.2; hi = 2.5 | 5.9
answer: (hi - lo)/f0*100
unit: %
tolerance: 0.5%

## A match centered at {f0} GHz holds from {lo} to {hi} GHz. What is its fractional bandwidth?

::: misses
- [any] Width over center.
:::
:::
:::
