21. OFDM: Transmitter and Receiver

PySDR has no OFDM chapter, but OFDM appears in two places. The Multipath Fading chapter names it as the way 4G and WiFi cope with frequency-selective fading: split the band into many narrow subcarriers, each of which sees a flat channel. The Cyclostationary chapter generates an OFDM signal (64 subcarriers, a 25 % cyclic prefix, QPSK) to show its cyclostationary features. That generator is the transmitter. This chapter adds the receiver: finding the symbol boundaries and the frequency offset from the cyclic prefix, equalizing a multipath channel with one complex division per subcarrier, and keeping the phase in place with pilots. Each step turned out to need a little more care than the textbook block diagram suggests, and the measurements show why.

The code is in code/include/cppsdr/ofdm.hpp and code/tests/test_ofdm.cpp; figure-generating-scripts/ofdm_reference.py runs PySDR’s generator (20 symbols, fixed seed) for comparison.

The Transmitter

PySDR’s generator builds each OFDM symbol in the frequency domain, transforms it with np.fft.ifft, prepends the last 16 samples as the cyclic prefix with np.hstack, and appends the result to the signal with np.concatenate. The C++ modulator keeps one FFTW plan for the life of the object and appends straight into the output, so nothing is reallocated per symbol and the inverse FFT is planned once:

template <std::size_t N, std::size_t CP>
class ofdm_modulator {
public:
    void modulate(std::span<const cf32> subcarriers, std::vector<cf32>& out)
    {
        std::copy_n(subcarriers.begin(), N, buf_.get());
        fftwf_execute(plan_.get());                                       // inverse FFT, planned once
        for (std::size_t i = 0; i < N; ++i) buf_[i] *= 1.0f / float(N);   // numpy's ifft scaling
        out.insert(out.end(), buf_.get() + (N - CP), buf_.get() + N);     // the prefix: a copy of the tail
        out.insert(out.end(), buf_.get(), buf_.get() + N);
    }
...
};

With PySDR’s QPSK mapping, the 1600 samples of 20 symbols match PySDR’s generator to \(3 \cdot 10^{-8}\), and the test checks that every prefix is an exact copy. (PySDR’s generator then interpolates the whole signal by 2 with scipy.signal.resample. An OFDM transmitter usually gets the same effect for free by leaving the outer subcarriers empty and using a larger FFT: zero subcarriers are oversampling.)

Finding Symbols with the Prefix

The cyclic prefix exists so that echoes of one symbol do not spill into the next. It has a second use at the receiver. Because the prefix is a copy, the received signal correlates with itself \(N\) samples later over exactly the prefix’s 16 samples, and nowhere else. Van de Beek, Sandell and Börjesson (“ML estimation of time and frequency offset in OFDM systems”, IEEE Transactions on Signal Processing, 1997) turned this into a maximum-likelihood estimator. For each candidate start \(m\):

\[\gamma(m) = \sum_{k=0}^{CP-1} r[m+k]\, r^*[m+k+N], \qquad \Phi(m) = \tfrac12 \sum_{k=0}^{CP-1} \left(|r[m+k]|^2 + |r[m+k+N]|^2\right),\]

which reads: \(\gamma\) is the correlation of the prefix candidate with the samples \(N\) later, and \(\Phi\) is their energy. The symbol starts where \(|\gamma(m)| - \rho\,\Phi(m)\) peaks (\(\rho\) depends on the SNR; 0.9 is a robust choice). The phase of \(\gamma\) at the peak is the frequency offset: a carrier offset of \(\varepsilon\) subcarrier spacings rotates each sample by \(2\pi\varepsilon/N\), so over \(N\) samples by \(2\pi\varepsilon\), and \(\varepsilon = -\arg\gamma / 2\pi\).

Both sums slide by one sample per sample, so they are running sums, as in the CFAR detector of CFAR and Real-Time Packet Detection. When the candidate advances, one sample pair enters and one leaves, so the cost is constant per sample. The pair leaving the window is \(N + CP\) samples old, so a ring of \(N + CP + 1\) samples holds everything the synchronizer needs:

ring_[n % R] = x;
if (n >= N)      add(n - N, +1);        // the pair (n - N, n) enters the window
if (n >= N + CP) add(n - N - CP, -1);   // the pair (n - N - CP, n - CP) leaves it
const double metric = std::abs(gamma_) - rho_ * phi_;

On a three-path channel (echoes at 2 and 5 samples), 20 dB SNR and a carrier offset of 0.12 subcarrier spacings, the synchronizer finds the start of every one of 39 symbols exactly, and the offset to 0.0003 subcarriers, averaged over the symbols. Fed 7 samples at a time instead of all at once, it gives a bit-identical result.

One Division per Subcarrier

After the offset is corrected, the receiver drops the prefix and takes an FFT of each symbol. As long as all echoes fit inside the prefix, the channel acts on each subcarrier as a single complex gain \(H[k]\): the prefix turns the channel’s linear convolution into a circular one, which the FFT diagonalizes. So equalization is one complex division per subcarrier, with \(H\) measured from a known training symbol at the start of the burst:

void train(std::span<const cf32> symbol, std::span<const cf32> known)
{
    std::fill(H_.begin(), H_.end(), cf32{1, 0});
    const auto y = demodulate(symbol);
    for (std::size_t k = 0; k < N; ++k) H_[k] = y[k] / known[k];   // H = Y / X
}

With this the test decodes all 2560 QPSK subcarrier symbols of the burst without error. The receiver also deliberately starts the FFT window 3 samples early, inside the prefix. An early window turns into a linear phase across the subcarriers, which the training estimate absorbs; a late one would pull in the next symbol.

Choosing the test channel taught a lesson of its own. The first channel had echoes of amplitude 0.5 and 0.3 next to a direct path of 0.8, and its frequency response came close to zero at some subcarriers. The one-tap equalizer then divides by a near-zero \(H[k]\) and amplifies the noise on that subcarrier enormously: the EVM was 25 % even at 40 dB SNR. This is PySDR’s frequency-selective fading seen from the receiver, and it is why OFDM systems always combine the subcarriers with channel coding (Channel Coding), so that a few faded subcarriers cost a few correctable bits. The tests use a milder channel (echoes of 0.4 and 0.2) so that each test measures one effect at a time.

Pilots Against Drift

Even with the milder channel, the first version reached only 17 % EVM at 40 dB, with the errors growing from symbol to symbol. The cause was the frequency estimate. Even with no frequency offset applied, the per-symbol estimates came out around 0.002 subcarrier spacings instead of 0, because the first samples of each prefix carry the previous symbol’s echoes and do not quite match. But a frequency error turns into a phase that grows without bound. In the test run the last of the 40 data symbols arrived rotated by 17 degrees. The training symbol fixed the phase only at the start.

Real OFDM systems handle this the way PySDR’s digital modulation chapter suggests in general: with pilots. IEEE 802.11a reserves 4 of its 64 subcarriers (at \(\pm 7\) and \(\pm 21\)) for known symbols in every OFDM symbol. The receiver measures their common rotation and removes it from all subcarriers:

template <std::size_t P>
double correct_common_phase(std::span<cf32> y, std::array<std::size_t, P> const& pilots,
                            std::span<const cf32> known)
{
    std::complex<double> acc = 0;
    for (std::size_t i = 0; i < P; ++i)
        acc += std::complex<double>(y[pilots[i]]) * std::conj(std::complex<double>(known[i]));
    const cf32 r(std::polar(1.0, -std::arg(acc)));
    for (auto& v : y) v *= r;
    return std::arg(acc);
}

With the four pilots, the EVM at 40 dB drops from 16.9 % to 1.8 %.

When the Echo Outlasts the Prefix

Finally, the prefix’s first purpose. The test moves the last echo from 5 samples to 24, beyond the 16-sample prefix, and keeps everything else equal at 40 dB. The EVM rises from 1.8 % to 15.8 %. The part of the echo that reaches past the prefix carries the previous symbol into the FFT window, which is inter-symbol interference. It also breaks the circular structure, so the subcarriers interfere with each other. Neither effect depends on the noise, so no SNR removes them: they form an error floor. This is why the prefix length is chosen from the longest expected delay spread, and why it costs throughput: here 16 of every 80 samples, 20 %.

The cyclic prefix also makes OFDM cyclostationary: the correlation at lag \(N\) repeats every \(N + CP\) samples. Cyclostationary Processing turns that into a detector that needs no synchronization at all.

Key Takeaways

  • The OFDM transmitter is one reused inverse-FFT plan and a copy for the prefix; it matches PySDR’s generator to \(3 \cdot 10^{-8}\).

  • The prefix is also a synchronization signal: its correlation with the samples \(N\) later finds the symbol start and the frequency offset, with running sums at constant cost per sample.

  • Within the prefix, multipath is one complex gain per subcarrier, removed by one division, but a near-zero \(H[k]\) amplifies noise without limit. OFDM needs coding.

  • A frequency estimate that is off by a few thousandths of a subcarrier still rotates the constellation visibly over a burst; pilot subcarriers in every symbol (as in 802.11a) correct it: 16.9 % EVM becomes 1.8 %.

  • An echo longer than the prefix creates an error floor (here 15.8 % EVM at 40 dB) that no SNR removes.