13. Digital Modulation, End to End¶
PySDR’s Digital Modulation chapter introduces ASK, PSK, QAM and FSK, Gray coding and differential coding, and closes with a short Python example: QPSK symbols, then the same symbols with noise and with phase noise. This book has met constellations several times already: Gray-coded tables at compile time in Compile-Time DSP: Filters, Tables and Constellations, and soft and hard decisions in Channel Coding. This chapter collects them into a modulator and demodulator that work for any constellation, and then does what PySDR’s example leaves out: it builds a complete coded QPSK link from the blocks of the earlier chapters and checks how close it comes to theory.
The code is in code/include/cppsdr/modem.hpp and code/tests/test_modem.cpp.
A Constellation Is a Type¶
A constellation is a table of symbols indexed by their bit labels, plus a rule for deciding which symbol a received sample is closest to. In C++ both can live in a small type:
template <std::size_t M>
struct qam {
static constexpr auto table = design::qam<M>(); // Gray-coded, unit average power
static unsigned slice(cf32 s); // O(1): round each axis to a level
};
template <std::size_t M>
struct psk {
static constexpr auto table = design::psk<M>(M == 4 ? std::numbers::pi / 4 : 0.0);
static unsigned slice(cf32 s); // O(1): which sector is the angle in
};
using qpsk = psk<4>; // 45, 135, 225, 315 degrees, as in PySDR's example
using qam16 = qam<16>;
and modem<C> provides everything else for any such type: map (bits to symbols), demap_hard, demap_soft (max-log likelihood ratios for the Viterbi decoder), and slice_nearest, an exhaustive search over all \(M\) symbols that serves as the reference. The fast slicers use the geometry: a square QAM decision is two independent roundings, a PSK decision is one angle. The test checks that both agree with the exhaustive search on a 121 × 121 grid of points for QPSK, 8-PSK, 16-QAM and 64-QAM. The grid is offset by half a step so that no point lies exactly on a decision boundary, where any answer is right. static_assert checks that all four tables are Gray coded.
Differential Coding at Compile Time¶
PySDR explains differential coding with a worked example: the input bits 1100111110, with start bit 1, are transmitted as 10111010100. The encoder and decoder are a few lines each, and since they are constexpr, the example itself becomes a test:
constexpr std::array<unsigned, 10> pysdr_in{1, 1, 0, 0, 1, 1, 1, 1, 1, 0};
constexpr std::array<unsigned, 11> pysdr_out{1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0};
static_assert(modem::diff_encode<2>(pysdr_in, 1) == pysdr_out);
static_assert(modem::diff_decode<2>(pysdr_out) == pysdr_in);
So are PySDR’s three claims about it: the start bit does not matter, a 180-degree inversion of the whole stream does not matter, and one channel error becomes two data errors. All three are static_asserts in the test. The template parameter is the PSK order: \(y_i = (y_{i-1} + x_i) \bmod M\) on symbol indices, which for \(M = 2\) is PySDR’s XOR, and for \(M = 4\) is DQPSK, insensitive to 90-degree rotations (also checked).
EVM and Gray Coding, Measured¶
PySDR shows QPSK with additive noise of power 0.01 and with phase noise of 0.1 radians RMS. The usual figure of merit for both is the error vector magnitude, the RMS distance from the ideal symbols relative to the RMS symbol. For small impairments it equals \(\sqrt{P_{noise}}\) for additive noise, and the phase-noise RMS in radians for phase noise. So both of PySDR’s impairments produce the same EVM, 10 % (\(-20\) dB); the test measures 10.1 % for each. The two look very different on a constellation plot (round blobs versus arcs), which is why EVM alone does not describe an impairment.
Gray coding gets one measurement too. 16-QAM at \(E_b/N_0 = 10\) dB, 400 000 symbols: the symbol error rate is \(7.1 \cdot 10^{-3}\), and the bit error rate is \(1.77 \cdot 10^{-3}\) with Gray labels. The textbook approximation \(\frac{3}{8}\operatorname{erfc}\sqrt{0.4\,E_b/N_0}\) gives \(1.75 \cdot 10^{-3}\). With natural binary labels on the same symbols and the same symbol errors, it is \(2.36 \cdot 10^{-3}\), a third more, because a step to a neighbouring symbol can flip two bits.
A Complete Coded QPSK Link¶
Now the end to end part. Every block below comes from an earlier chapter; the test only connects them:
stage |
block |
|---|---|
convolutional code, K = 7, rate 1/2 |
|
QPSK, Gray coded |
|
RRC pulse shaping, 8 samples/symbol, 101 taps |
|
channel: 0.37-sample delay, 1 rad phase, frequency offset, AWGN |
|
matched RRC filter |
convolution with the same taps |
timing recovery |
|
carrier recovery |
|
frame and phase sync |
a 64-symbol preamble, 8 pilot symbols every 248 data symbols |
soft demapping, decoding |
|
Building it turned up two lessons that a block-by-block test cannot show.
The timing detector must not need the carrier. Timing Recovery with a Compile-Time Filter Bank uses PySDR’s Mueller and Muller loop. Its error term uses symbol decisions, so it assumes the constellation is already upright, which holds for PySDR’s BPSK examples. In this link timing recovery runs before the Costas loop, on a constellation rotated by 1 radian and spinning with the frequency offset. Gardner’s detector (F. M. Gardner, IEEE Transactions on Communications, 1986) uses the sample halfway between two symbols instead of decisions,
which reads: if the mid-sample is not zero while the signal changes sign, we are sampling early or late, whatever the carrier phase. It is TED_GARDNER in GNU Radio’s symbol_sync blocks. gardner_timing_recovery reuses the compile-time filter bank and streaming structure of the M&M class; only the error changes. With a gentle loop gain the difference turned out to be modest: at 30 dB the EVM is 2.4 % with Gardner (the noise alone accounts for 2.2 %) and 3.5 % with M&M. Over ten frames at 4 dB, Gardner decoded all ten and M&M nine. With a more aggressive gain of 0.05 and a short lead-in, M&M reached 7.2 % EVM at 30 dB against Gardner’s 2.6 %.
Acquisition time is part of the link budget. The first version sent 200 random symbols before the preamble to let the loops lock. At \(E_b/N_0 = 4\) dB only 4 of 10 frames came through. The per-segment error counts showed why: in the failing frames, the first few hundred data symbols were mostly wrong, and then the errors dropped to the noise level. The Costas loop was still pulling in the frequency offset when the data began. (A feedforward frequency estimate, raising the symbols to the fourth power to strip the QPSK modulation, made things worse at this SNR: it broke frames that decoded without it, because raising noisy samples to the fourth power multiplies the noise far more than the signal.) With 1000 lead-in symbols every frame decodes. The pilots address a related failure: if the loop slips by 90 degrees during a frame, a single preamble would leave everything after the slip rotated, whereas pilots re-resolve the rotation every 256 symbols.
The final result, ten frames of 4000 information bits per setting:
\(E_b/N_0\) |
channel symbol errors (QPSK, \(E_s/N_0 = E_b/N_0\)) |
ideal QPSK, theory |
coded frames error-free |
|---|---|---|---|
4 dB |
0.120 |
0.110 |
10 of 10 |
6 dB |
0.051 |
0.045 |
10 of 10 |
The receiver’s channel symbol error rate is within 10 to 15 % of ideal QPSK, so the synchronization costs only a fraction of a dB; the Viterbi decoder then removes every one of the roughly 480 symbol errors per frame at 4 dB. Uncoded QPSK at the same \(E_b/N_0\) has a bit error rate of \(1.4 \cdot 10^{-2}\).
Key Takeaways¶
Make a constellation a type with a table and an O(1) slicer; one
modem<C>template then maps, slices and soft-demaps any of them, checked against an exhaustive nearest-symbol search.PySDR’s differential coding example, and its three properties, are compile-time checks; \(M\)-ary differential coding is the same code with a template parameter.
EVM is \(\sqrt{P_{noise}}\) for AWGN and the RMS phase error for phase noise: PySDR’s two impairments both give 10 %. Gray labels cut 16-QAM’s bit errors by a third against natural labels.
Use a timing detector that does not need the carrier phase (Gardner) when timing comes before carrier recovery.
Loop acquisition time is a design parameter: with 200 lead-in symbols, 6 of 10 frames failed at 4 dB; with 1000, none did. Pilots limit the damage of a phase slip to one segment.
The complete link, built from the earlier chapters’ blocks, comes within 10 to 15 % of ideal QPSK symbol errors, and its code removes all of them at 4 dB.