12. Timing Recovery with a Compile-Time Filter Bank

PySDR’s Synchronization chapter recovers symbol timing with the Mueller and Muller algorithm: a feedback loop that decides, symbol after symbol, where within each symbol period to take its sample, tracking a fractional position mu. To sample between the received samples, PySDR first interpolates the whole signal by 16:

samples_interpolated = signal.resample_poly(samples, 16, 1)
...
out[i_out] = samples_interpolated[i_in*16 + int(mu*16)]

and then picks one of the 16 interpolated values per symbol. With 8 samples per symbol, that computes 128 interpolated samples per symbol and uses one. It also turns the whole recording into a 16 times larger array before the loop starts, which is impossible for a stream that never ends. PySDR says as much at the end of the section: in practice the loop runs on a few samples at a time, and “you have to remember the value of mu in between calls to the sync function, as well as the last couple values of out and out_rail.”

This chapter builds that practical version, and the C++ idea at its heart is simple: compute only the one interpolated value the loop needs, with a filter chosen from a small bank that the compiler designs.

A Bank of Fractional Delays

Evaluating a band-limited signal at a fractional position \(i + \mu\) is a convolution with a shifted sinc, which is exactly PySDR’s fractional delay filter from the same chapter, np.sinc(n - delay) with a Hamming window. If we allow \(P\) different values of \(\mu\) (PySDR’s 16), we need \(P\) such filters. They depend only on \(P\) and the filter length, so the compiler can design all of them, using the fractional_delay<N> design from Compile-Time DSP: Filters, Tables and Constellations (code/include/cppsdr/timing.hpp):

template <std::size_t P, std::size_t N>
constexpr std::array<std::array<float, N>, P> fractional_delay_bank()
{
    std::array<std::array<float, N>, P> bank{};
    for (std::size_t p = 0; p < P; ++p) bank[p] = design::fractional_delay<N>(double(p) / double(P));
    return bank;
}

For \(P = 16\) and 21 taps this is 336 numbers, stored as read-only data, instead of a copy of the signal 16 times its size. And because the bank is a compile-time constant, we can state its defining properties as checks:

static constexpr auto bank = fractional_delay_bank<P, N>();
static_assert(bank[0][N / 2] == 1.0f, "phase 0 is the identity: a single 1 at the centre ...");
static_assert(design::dc_gain(bank[P / 2]) > 0.999999 && design::dc_gain(bank[P / 2]) < 1.000001,
              "... and every phase has unity gain");

A delay of zero must reproduce the input exactly; the windowed sinc at integer offsets is zero everywhere except the centre, and the compiler confirms it.

This structure has a name: a polyphase filter bank. The 16 filters are the 16 phases of the interpolation filter that resample_poly uses internally; we just never compute the phases we do not need. The same idea underlies GNU Radio’s polyphase clock synchronizer (pfb_clock_sync) and its arbitrary resampler.

The Loop, with Its State

The loop itself is PySDR’s, line for line; what changes is where its state lives. Everything PySDR says must be remembered between calls (mu, the last two outputs and decisions, and the input samples not yet consumed) becomes a member of an object, so the caller can feed blocks of any size:

template <std::size_t P = 16, std::size_t N = 21>
class mm_timing_recovery {
public:
    explicit mm_timing_recovery(double sps, double gain = 0.3);
    void process(std::span<const cf32> in, std::vector<cf32>& out);
private:
    double sps_, gain_;
    double mu_ = 0;
    std::vector<cf32> buf_;          // input not yet consumed, plus N/2 samples of history
    std::size_t i_;
    cf32 y1_{}, y2_{}, rail1_{}, rail2_{};   // PySDR's out[i-1], out[i-2], out_rail[...]
};

and the body of process reads like PySDR’s while loop:

while (i_ + half < buf_.size()) {
    const auto& h = bank[std::size_t(mu_ * P) % P];   // the phase at or below mu, like PySDR's int(mu*16)
    cf32 y{};
    for (std::size_t k = 0; k < N; ++k) y += h[k] * buf_[i_ + k - half];
    out.push_back(y);

    // Mueller and Muller error, exactly PySDR's lines
    const cf32 rail(y.real() > 0 ? 1.0f : 0.0f, y.imag() > 0 ? 1.0f : 0.0f);
    const cf32 x = (rail - rail2_) * std::conj(y1_);
    const cf32 z = (y - y2_) * std::conj(rail1_);
    const double mm = (z - x).real();
    y2_ = y1_, y1_ = y, rail2_ = rail1_, rail1_ = rail;

    mu_ += sps_ + gain_ * mm;
    const double whole = std::floor(mu_);
    i_ += std::size_t(whole);
    mu_ -= whole;
}

The interpolation needs \(N/2\) samples on either side of the sampling point, so a symbol near the end of a block is simply computed in the next call; after each call the input buffer is trimmed to the samples that can still be needed. buf_ grows and shrinks with insert and erase, but a std::vector keeps its capacity, so once the stream is running and the caller has reserved space in out, no memory is allocated anymore.

The bank size and the filter length are template parameters with PySDR’s values as defaults. That makes the trade-off between resolution, filter length and memory a compile-time decision you can change in one place, and every instance gets its own checked bank.

Against PySDR

figure-generating-scripts/timing_reference.py builds the test signal with PySDR’s own code: 2000 random BPSK symbols, raised-cosine pulse shaping at 8 samples per symbol, the 0.4-sample fractional delay, and a little noise. It then runs PySDR’s interpolating Mueller and Muller loop on it, verbatim. code/tests/test_timing.cpp checks:

  • the C++ loop decodes 1500 symbols after lock without a single bit error;

  • PySDR’s resample_poly version decodes the same bits with the same alignment, so the two implementations agree on the result;

  • feeding the input in random block sizes gives bit-identical output to one big call, which is the real test of the state handling;

  • a 64-phase bank (mm_timing_recovery<64, 21>) decodes just as well. On this signal it does not reduce the residual error (0.0036 in both cases): with 16 phases the sampling instant is off by at most 1/16 of a sample, and the noise dominates. Whether finer phases pay off depends on the SNR and the modulation; the template parameter makes it cheap to find out.

The Same Idea in Hardware

An FPGA timing-recovery loop is built exactly like this: the fractional part of mu addresses a coefficient ROM (the bank), a small FIR computes one interpolated sample per symbol, and the integer part of mu decides how many input samples to advance. Usually mu is a fixed-point number, and its top bits select the phase, just as the top bits of the NCO’s phase accumulator select a sine table entry in Oscillators and Frequency Shifting. With fixed<High, Low> from Fixed Point and CIC Filters: the Bridge to VHDL the C++ loop can be made bit-true to such a design, and the bank can be exported as the ROM contents.

When the Carrier Is Not Locked Yet

PySDR’s Mueller and Muller detector is decision-directed: its error uses the decided symbols, which presumes the constellation is already upright. That is true in PySDR’s BPSK examples and in the RDS receiver. When timing recovery must run before carrier recovery on a rotating QPSK constellation, a detector that does not need the carrier phase is the safer choice. gardner_timing_recovery in the same header uses Gardner’s detector, the sample halfway between symbols times the change from one symbol to the next, with the same compile-time filter bank. Digital Modulation, End to End uses it in a complete QPSK link.

Key Takeaways

  • When a loop needs one interpolated value per symbol, compute one, from a polyphase bank of fractional-delay filters, instead of interpolating the whole signal.

  • Designing the bank at compile time costs nothing at run time, keeps it in read-only data, and lets static_assert check its properties.

  • State that must survive between blocks belongs in the object; test it by feeding random block sizes and demanding bit-identical output.

  • Make resolution and filter length template parameters, so the trade-off is a one-line change.