---
title: "Project: an OFDM link"
heading: Building an OFDM transmitter and receiver with a cyclic prefix, passing it through a multipath channel, equalizing with one tap per subcarrier, and predicting its error rate from the channel's frequency response
eyebrow: S8 · Lesson 53 · Project
xp: 80
---

::: hook
This project builds the core of every Wi-Fi and LTE modem. You'll write the OFDM modulator that turns blocks of QPSK symbols into a time signal with a cyclic prefix, the demodulator that recovers them with an FFT, and the one-tap equalizer that undoes a multipath channel subcarrier by subcarrier. The final test sends 51,200 bits through a three-path channel with noise and checks the bit error rate against a prediction made from nothing but the channel's frequency response: each subcarrier behaves as its own QPSK link at its own SNR.
:::

::: predict
## A three-path channel is normalized to unit energy and the link runs at 10 dB Eb/N0. Compared with a flat channel at the same Eb/N0, what happens to the OFDM link's error rate?

- [ ] Nothing: the equalizer removes the channel completely
- [x] It rises, because the subcarriers in the channel's dips have lower SNR and the zero-forcing equalizer amplifies their noise
- [ ] It falls, because multipath adds energy
- [ ] It becomes 0.5 on every subcarrier

::: reveal
It rises. Equalization divides out each subcarrier's gain $H_k$, restoring the symbols but scaling the noise by $1/|H_k|$, so subcarrier k sees SNR $|H_k|^2E_b/N_0$. The channel's average power gain is 1, but the error rate is dominated by the subcarriers in its spectral dips. That's why OFDM systems code across subcarriers: the strong ones can carry the weak ones' bits.
:::
:::

::: nameit
## The transmitter

For each OFDM symbol, N subcarrier symbols $X[k]$ go through an orthonormal inverse FFT, $x[n] = \frac{1}{\sqrt{N}}\sum_kX[k]e^{j2\pi kn/N}$, which in NumPy is `np.fft.ifft(X)*np.sqrt(N)`. The last $N_{cp}$ samples are copied to the front, and the symbols are sent one after another.

## The channel

$y = h * x + n$: a short multipath impulse response (fewer taps than the prefix) and complex white noise. With unit-energy QPSK on each subcarrier, the noise variance per complex sample is $1/(2E_b/N_0)$ (ignoring the prefix's energy).

## The receiver

Drop each prefix and take the orthonormal FFT, `np.fft.fft(body)/np.sqrt(N)`. Because the prefix makes the channel's convolution circular, $Y[k] = H[k]X[k] + W[k]$ with $H = \text{FFT}_N(h)$.

## Equalization and performance

$\hat{X}[k] = Y[k]/H[k]$, then QPSK decisions. Subcarrier k has $E_b/N_0$ multiplied by $|H[k]|^2$, so the predicted bit error rate is the average over subcarriers:

$$P_b = \frac{1}{N}\sum_kQ\left(\sqrt{2|H[k]|^2\frac{E_b}{N_0}}\right)$$
:::

::: example
## A 64-subcarrier system uses a 16-sample prefix at 20 MHz. How long a channel can it handle, and what does the prefix cost?

::: solution
1. **Prefix length.**
   $$\begin{aligned} T_\text{cp} &= \frac{16}{20\ \text{MHz}} \\ &= 0.8\ \text{µs} \end{aligned}$$
   the Wi-Fi guard interval.
2. **Longest echo.** It handles echoes up to 240 m longer than the direct path:
   $$3 \times 10^8\ \text{m/s} \times 0.8\ \text{µs} = 240\ \text{m}$$
3. **Cost.** The prefix uses
   $$\frac{16}{80} = 20\%$$
   of the time, 0.97 dB of SNR, as lesson 26 computed.

**Answer:** echoes up to 0.8 µs (240 m of extra path), at a cost of 20% of the time, or 0.97 dB.
:::
:::

::: example
## The project's channel, normalized, has taps proportional to 0.8, 0.5j and −0.3. Where are its weakest subcarriers?

::: solution
1. **Range of the gain.** $|H[k]|^2$ ranges from 0.19 to 2.61 across the band, averaging 1.
2. **The deepest dip.** It runs
   $$10\log_{10}0.19 = -7.2\ \text{dB}$$
   below average, so at 10 dB average those subcarriers see only about 2.8 dB and make most of the errors.
3. **Predicted error rate.** The average is 5.9 × 10⁻³, about 1,500 times the flat-channel value of 3.9 × 10⁻⁶.

**Answer:** the weakest subcarriers sit at the deepest dip, 7.2 dB below average (about 2.8 dB at 10 dB average SNR), and they make most of the errors.
:::
:::

::: check
kind: python

## Step 1. Write `ofdm_mod(X, ncp)`: orthonormal IFFT of each symbol plus a cyclic prefix.

```python starter
import numpy as np

def ofdm_mod(X, ncp):
    # X: array of shape (nsym, N), the subcarrier symbols of each OFDM symbol.
    # Return the serial time signal: for each row, the orthonormal IFFT (np.fft.ifft(row)*sqrt(N))
    # with its last ncp samples copied to the front.
    return np.zeros(1, dtype=complex)

X = np.ones((2, 8), dtype=complex)
print(len(ofdm_mod(X, 2)))
```

```python solution
import numpy as np

def ofdm_mod(X, ncp):
    X = np.atleast_2d(X)
    N = X.shape[1]
    x = np.fft.ifft(X, axis=1)*np.sqrt(N)
    return np.concatenate([x[:, N - ncp:], x], axis=1).reshape(-1)

X = np.ones((2, 8), dtype=complex)
print(len(ofdm_mod(X, 2)))
```

```python tests
import numpy as np
rng = np.random.default_rng(3)
X = (rng.choice([-1, 1], (5, 64)) + 1j*rng.choice([-1, 1], (5, 64)))/np.sqrt(2)
x = ofdm_mod(X, 16)
assert len(x) == 5*80, f"Five symbols of 64 + 16 samples make 400 samples; got {len(x)}."
s = x.reshape(5, 80)
assert np.allclose(s[:, :16], s[:, 64:]), "The cyclic prefix must copy each symbol's last 16 samples to its front."
assert abs(np.mean(np.abs(s[:, 16:])**2) - 1) < 1e-9, "With the orthonormal IFFT, unit-energy subcarrier symbols give unit average sample power."
assert np.allclose(np.fft.fft(s[0, 16:])/8, X[0]), "The body of each symbol must be the IFFT of its row (scaled by √N)."
```

::: right
Right. Unit average power, each prefix a copy of its symbol's tail.
:::
:::

::: check
kind: python

## Step 2. Write `ofdm_demod(y, N, ncp, nsym)`: drop the prefixes and FFT each symbol.

```python starter
import numpy as np

def ofdm_demod(y, N, ncp, nsym):
    # Split y into nsym symbols of N + ncp samples, drop each prefix, and take the orthonormal FFT
    # (np.fft.fft(body)/sqrt(N)). Return an array of shape (nsym, N).
    return np.zeros((nsym, N), dtype=complex)

print(ofdm_demod(np.zeros(20, dtype=complex), 8, 2, 2).shape)
```

```python solution
import numpy as np

def ofdm_demod(y, N, ncp, nsym):
    s = np.asarray(y)[:nsym*(N + ncp)].reshape(nsym, N + ncp)[:, ncp:]
    return np.fft.fft(s, axis=1)/np.sqrt(N)

print(ofdm_demod(np.zeros(20, dtype=complex), 8, 2, 2).shape)
```

```python tests
import numpy as np
def mod(X, ncp):
    N = X.shape[1]; x = np.fft.ifft(X, axis=1)*np.sqrt(N)
    return np.concatenate([x[:, N - ncp:], x], axis=1).reshape(-1)
rng = np.random.default_rng(5)
X = (rng.choice([-1, 1], (6, 64)) + 1j*rng.choice([-1, 1], (6, 64)))/np.sqrt(2)
Y = ofdm_demod(mod(X, 16), 64, 16, 6)
assert Y.shape == (6, 64) and np.allclose(Y, X), "Without a channel the demodulator must return the transmitted symbols."
h = np.array([1.0, 0.5 - 0.3j, 0.2j])
y = np.convolve(mod(X, 16), h)
Y = ofdm_demod(y, 64, 16, 6)
H = np.fft.fft(h, 64)
assert np.allclose(Y, H*X), "With a channel shorter than the prefix, each subcarrier must see exactly one complex gain H[k]."
y2 = np.convolve(mod(X, 2), np.array([1.0, 0.4, 0.3, 0.2, 0.1]))
Y2 = ofdm_demod(y2, 64, 2, 6)
assert not np.allclose(Y2, np.fft.fft(np.array([1.0, 0.4, 0.3, 0.2, 0.1]), 64)*X), "Check that you drop exactly ncp samples per symbol."
```

::: right
Right. Through a channel shorter than the prefix, every subcarrier comes out multiplied by exactly one complex gain.
:::
:::

::: check
kind: python

## Step 3. Write `equalize(Y, h, N)`: the one-tap zero-forcing equalizer.

```python starter
import numpy as np

def equalize(Y, h, N):
    # One-tap zero-forcing equalizer: divide each subcarrier by the channel's frequency response
    # H = FFT of h zero-padded to N.
    return Y

print(equalize(np.ones((1, 4), dtype=complex), np.array([1.0, 0.5]), 4))
```

```python solution
import numpy as np

def equalize(Y, h, N):
    return Y/np.fft.fft(h, N)

print(equalize(np.ones((1, 4), dtype=complex), np.array([1.0, 0.5]), 4))
```

```python tests
import numpy as np
from math import erfc, sqrt
Q = lambda x: 0.5*erfc(x/sqrt(2))
def mod(X, ncp):
    N = X.shape[1]; x = np.fft.ifft(X, axis=1)*np.sqrt(N)
    return np.concatenate([x[:, N - ncp:], x], axis=1).reshape(-1)
def demod(y, N, ncp, nsym):
    s = y[:nsym*(N + ncp)].reshape(nsym, N + ncp)[:, ncp:]
    return np.fft.fft(s, axis=1)/np.sqrt(N)
Z = equalize(np.array([[1 + 1j, 2, -1j, 0.5]]), np.array([1.0, 0.5]), 4)
H = np.fft.fft([1.0, 0.5], 4)
assert np.allclose(Z, np.array([[1 + 1j, 2, -1j, 0.5]])/H), "Divide by the channel's FFT, zero-padded to N."
rng = np.random.default_rng(9)
N, ncp, nsym = 64, 16, 400
h = np.array([0.8, 0.5j, -0.3])
h = h/np.sqrt(np.sum(np.abs(h)**2))
Hk = np.fft.fft(h, N)
e = 10.0
b = rng.integers(0, 2, (nsym, N, 2))
X = ((1 - 2*b[..., 0]) + 1j*(1 - 2*b[..., 1]))/np.sqrt(2)
y = np.convolve(mod(X, ncp), h)
sigma = np.sqrt(1/(2*2*10**(e/10)))
y = y + sigma*(rng.normal(size=len(y)) + 1j*rng.normal(size=len(y)))
Xh = equalize(demod(y, N, ncp, nsym), h, N)
bh = np.stack([(Xh.real < 0), (Xh.imag < 0)], axis=-1).astype(int)
ber = np.mean(bh != b)
th = np.mean([Q(sqrt(2*10**(e/10)*abs(Hk[k])**2)) for k in range(N)])
assert 0.8*th < ber < 1.25*th, f"Each subcarrier has its own SNR, |H[k]|² Eb/N0: the measured {ber:.5f} should match their average error rate, {th:.5f}."
```

::: right
Right. The link's measured error rate matches the average over subcarriers of each one's QPSK error rate at its own SNR.
:::
:::

::: explain
## Why does OFDM need only one complex multiplication per subcarrier to equalize a multipath channel?

::: model
A multipath channel convolves the transmitted signal with its impulse response, which in general mixes every sample with its neighbors. The cyclic prefix makes that linear convolution indistinguishable, over each symbol's body, from a circular convolution of the body with the channel, as long as the channel is shorter than the prefix. The DFT turns circular convolution into multiplication, so after the receiver's FFT each subcarrier's symbol has simply been multiplied by one complex number, the channel's frequency response at that subcarrier. Undoing a single complex gain takes a single division, which is why OFDM receivers can handle long echoes that would need a long time-domain equalizer.
:::
:::

::: practice

::: check
given: N = 64 | 1024; c = 16 | 72; f = 20 | 15.36
answer: c/f
unit: µs
tolerance: 0.5%

## Prefix duration: {c} samples at {f} MHz (N = {N})?

::: misses
- [any] $N_{cp}/f_s$.
:::
:::

::: check
given: g = 0.1 | 0.5 | 2; E = 8 | 12
answer: Q(sqrt(2*g*10^(E/10)))
tolerance: 1%

## Bit error rate of a subcarrier with |H|² = {g} at Eb/N0 {E} dB?

::: misses
- [any] $Q(\sqrt{2|H|^2E_b/N_0})$.
:::
:::

::: check
given: E = 0 | 2 | 4 | 6 | 8 | 10 | 12
answer: 1/(4*10^(E/10))
tolerance: 0.5%

## What noise variance goes into each of the real and imaginary parts at Eb/N0 {E} dB (unit-energy QPSK)?

::: misses
- [1/(2*10^(E/10))] That's the total complex variance; each part gets half.
- [any] Half of $1/(2E_b/N_0)$.
:::
:::
:::
