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.

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.