---
title: Receiver lab
heading: Cascade analysis, spurious-response search, complex one-port calibration and bandpass-sampling zones, in Python
eyebrow: S6 · Lesson 49 · Receiver lab
xp: 60
---

::: hook
Lessons 40 to 46 gave the formulas a receiver designer uses one at a time: Friis for noise, the intercept cascade, the (m, n) spur rule, the calibration error model and the bandpass-sampling inequality. Real design work runs them over whole lineups, bands and data sets. This lab writes the four tools: a lineup calculator that returns gain, noise figure and intercept; a search that lists every low-order spurious response a preselector lets through; a short–open–load calibration in complex arithmetic that recovers a device's reflection from raw readings; and a list of every sample rate that can digitize a band.
:::

::: play
- The curve is the cascade noise figure of a 1 dB, 20 dB-gain LNA followed by a 7 dB mixer with 7 dB of loss and a 3 dB IF amplifier, against a passive loss L placed in front of the LNA.
- Every decibel of front-end loss adds a decibel of noise figure, since it multiplies everything behind it.
- The same loss raises the receiver's input-referred intercept by a decibel: noise and linearity pull in opposite directions.
:::

```sim plot
view: graph
x: L = 0..4 dB
y: N = 0..6 dB
plot: L + 10*log(10^0.1 + (10^0.7 - 1)/100 + (10^0.3 - 1)/(100*10^-0.7)) | label: cascade NF
```

::: predict
## A receiver at 2450 MHz uses a 2380 MHz LO and a 70 MHz IF behind a preselector passing 2300 to 2600 MHz. Counting responses with m and n up to 5, how many input frequencies in that band convert to 70 MHz?

- [ ] Two: the signal and its image
- [ ] Four
- [x] Ten: the signal, the image and eight (n, n) responses crowding in between
- [ ] Over fifty

::: reveal
$|mf - nf_{LO}| = f_{IF}$ gives, inside 2300 to 2600 MHz, only the m = n responses: 2450 (1, 1), the image 2310 (1, 1), and pairs at $(nf_{LO} \pm 70)/n$ for n = 2 to 5, from 2345 and 2415 MHz inward to 2366 and 2394 MHz. All sit within 140 MHz of the LO, inside the preselector, so the mixer's balance and the LNA's linearity must suppress them.
:::
:::

::: nameit
## The tools

- **Lineup**: stage by stage, $F = F_1 + \frac{F_2 - 1}{G_1} + \cdots$ and $\frac{1}{\text{IIP3}} = \sum_k\frac{G_{\text{before }k}}{\text{IIP3}_k}$ (milliwatts), total gain the product; then sensitivity and $\text{SFDR} = \frac23(\text{IIP3} - N_\text{floor})$.
- **Spurs**: for every m ≥ 1 and n ≥ 0 up to a limit, the input frequencies $(nf_{LO} \pm f_{IF})/m$, kept if they lie inside the preselector.
- **One-port calibration** with complex numbers: $e_{00} = M_L$, $e_{11} = \frac{a + b}{a - b}$, $e_{10}e_{01} = a(1 - e_{11})$ with $a = M_O - M_L$, $b = M_S - M_L$, and $\Gamma_A = \frac{\Gamma_M - e_{00}}{e_{10}e_{01} + e_{11}(\Gamma_M - e_{00})}$.
- **Bandpass sampling**: for each zone n from 1 to $\lfloor f_H/(f_H - f_L)\rfloor$, the allowed rates $\frac{2f_H}{n} \le f_s \le \frac{2f_L}{n - 1}$ (no upper limit for n = 1).
:::

::: check
kind: python

## Write `lineup(stages)`: stages is a list of (gain dB, NF dB, IIP3 dBm); return (total gain dB, NF dB, input IIP3 dBm). Also `sfdr(iip3, nf, bw_hz)`.

```python starter
def lineup(stages):
    return (0.0, 0.0, 0.0)

def sfdr(iip3, nf, bw_hz):
    return 0.0
```

```python solution
import math

def lineup(stages):
    G, F, inv = 1.0, None, 0.0
    for g, nf, ip in stages:
        f = 10 ** (nf / 10)
        F = f if F is None else F + (f - 1) / G
        inv += G / 10 ** (ip / 10)
        G *= 10 ** (g / 10)
    return (10 * math.log10(G), 10 * math.log10(F), -10 * math.log10(inv))

def sfdr(iip3, nf, bw_hz):
    return 2 / 3 * (iip3 - (-174 + 10 * math.log10(bw_hz) + nf))
```

```python tests
g, nf, ip = lineup([(20, 1, 0), (-7, 7, 15), (30, 3, 20)])
assert abs(g - 43) < 1e-9 and abs(nf - 1.30) < 0.005 and abs(ip + 6.40) < 0.01, "LNA, mixer, IF: 43 dB, 1.30 dB, −6.4 dBm."
g2, nf2, ip2 = lineup([(-2, 2, 100), (20, 1, 0), (-7, 7, 15), (30, 3, 20)])
assert abs(nf2 - nf - 2) < 1e-9 and abs(ip2 - ip - 2) < 0.001, "A 2 dB loss in front adds 2 dB of NF and raises IIP3 by 2 dB."
assert abs(sfdr(-6.4, 1.3, 1e6) - 70.9) < 0.05, "SFDR in 1 MHz: 70.9 dB."
```

::: right
Right. A lossless (very high intercept) passive stage moves both numbers by exactly its loss.
:::
:::

::: check
kind: python

## Write `spurs(f_lo_band, f_hi_band, LO, IF, M=5, N=5)`: the sorted list of (m, n, f) with 1 ≤ m ≤ M and 0 ≤ n ≤ N for which an input f inside the band gives $|mf - nf_{LO}| = f_{IF}$.

```python starter
def spurs(f_lo_band, f_hi_band, LO, IF, M=5, N=5):
    return []
```

```python solution
def spurs(f_lo_band, f_hi_band, LO, IF, M=5, N=5):
    out = set()
    for m in range(1, M + 1):
        for n in range(0, N + 1):
            for f in ((n * LO + IF) / m, (n * LO - IF) / m):
                if f_lo_band <= f <= f_hi_band:
                    out.add((m, n, round(f, 3)))
    return sorted(out, key=lambda t: t[2])
```

```python tests
s = spurs(2300, 2600, 2380, 70)
assert len(s) == 10, "Ten responses in the preselector band."
assert (1, 1, 2450.0) in s and (1, 1, 2310.0) in s and (2, 2, 2415.0) in s, "The signal, the image and the half-IF spur."
assert all(m == n for m, n, f in s), "In this band only m = n responses survive."
```

::: right
Right. Responses with m ≠ n land far from the band for this plan, which is why the preselector catches them.
:::
:::

::: check
kind: python

## Write `sol_cal(ML, MO, MS)` returning the error terms (e00, e11, e10e01) and `correct(GM, e00, e11, t)` returning the actual reflection, all complex.

```python starter
def sol_cal(ML, MO, MS):
    return (0j, 0j, 1 + 0j)

def correct(GM, e00, e11, t):
    return GM
```

```python solution
def sol_cal(ML, MO, MS):
    a, b = MO - ML, MS - ML
    e11 = (a + b) / (a - b)
    return (ML, e11, a * (1 - e11))

def correct(GM, e00, e11, t):
    return (GM - e00) / (t + e11 * (GM - e00))
```

```python tests
import cmath
e00, e11, t = 0.05 + 0.02j, 0.1 - 0.05j, 0.9 * cmath.exp(-0.7j)
meas = lambda G: e00 + t * G / (1 - e11 * G)
E00, E11, T = sol_cal(meas(0), meas(1), meas(-1))
assert abs(E00 - e00) < 1e-12 and abs(E11 - e11) < 1e-12 and abs(T - t) < 1e-12, "The calibration recovers all three error terms."
for D in (0.3 + 0.4j, -0.7j, 0.99):
    assert abs(correct(meas(D), E00, E11, T) - D) < 1e-12, "And corrects any device exactly."
```

::: right
Right. With ideal standards the correction is exact; real kits' imperfections are what limit a VNA.
:::
:::

::: check
kind: python

## Write `zones(fL, fH)`: the list of (n, fs_min, fs_max) for every Nyquist zone that can hold the band, with fs_max = infinity for n = 1.

```python starter
def zones(fL, fH):
    return []
```

```python solution
def zones(fL, fH):
    out = []
    for n in range(1, int(fH // (fH - fL)) + 1):
        lo = 2 * fH / n
        hi = 2 * fL / (n - 1) if n > 1 else float('inf')
        if lo <= hi:
            out.append((n, lo, hi))
    return out
```

```python tests
z = zones(60, 80)
assert [n for n, a, b in z] == [1, 2, 3, 4], "60 to 80 MHz fits in zones 1 to 4."
assert z[1][1:] == (80.0, 120.0) and abs(z[2][1] - 53.33) < 0.01 and z[2][2] == 60.0, "Zone 2: 80 to 120 MS/s; zone 3: 53.3 to 60."
assert z[3][1] == z[3][2] == 40.0, "Zone 4 allows exactly 40 MS/s, with no margin."
```

::: right
Right. The highest zone allows a single rate with no tolerance for clock error, so practical designs stay a zone or two lower.
:::
:::

::: explain
## Why does putting even a small loss in front of a receiver's LNA cost so much, and why do designers sometimes accept it anyway?

::: model
A passive loss at the input attenuates the signal before any amplification but adds its own thermal noise, so every decibel of loss raises the whole receiver's noise figure by a decibel; nothing behind it can recover that. Designers still put filters, switches and limiters there, because the same loss lowers the level of strong signals reaching the LNA and mixer, raising the receiver's input-referred intercept by the same decibel and protecting it from overload. The balance between sensitivity and resistance to strong interferers sets how much front-end loss a design can afford.
:::
:::
