15. Correlating Under Frequency Offset¶
The packet detector of CFAR and Real-Time Packet Detection assumes the received preamble arrives at exactly the expected frequency. PySDR’s Detection chapter explains what happens when it does not. A frequency offset makes the received samples rotate in phase relative to the template, the correlation’s terms stop adding up in phase, and the peak shrinks following a Dirichlet (“periodic sinc”) function. The chapter gives the formula, the 3.9 dB lost at half a bin, and three remedies: coherent segments with phase compensation, non-coherent segments, and a brute-force search over frequencies, made cheap with a “frequency shift trick” in the FFT domain. It describes them in prose, without code.
This chapter implements the non-coherent segmented correlator and the FFT frequency search and checks the formula numerically. Along the way it finds something PySDR’s text does not mention and its own packet detector depends on: the Zadoff-Chu preamble that PySDR chooses for that detector reacts to a frequency offset in a completely different way from an ordinary preamble.
The code is in code/include/cppsdr/freq_search.hpp and code/tests/test_freq_search.cpp. All results here are noise-free and deterministic, so each number can be checked against a formula.
The Loss Formula, Checked¶
PySDR’s loss formula is
which reads: the \(N\) terms of the correlation each turn by an extra \(2\pi \Delta f T_s\), and the magnitude of their sum, relative to \(N\) aligned terms, is this ratio of sines. As a constexpr function it can state PySDR’s two numbers as compile-time checks: half a bin (\(\Delta f = 1/(2NT_s)\)) costs 3.92 dB, and one full bin is a null. The formula is exact, not approximate, whenever every preamble sample has the same magnitude. Correlating PySDR’s 63-sample Zadoff-Chu preamble with frequency-shifted copies of itself at the true lag reproduces it to \(10^{-6}\) dB at every offset from 0.05 to 0.95 bins.
Two Preambles, Two Behaviours¶
The formula describes the correlation at the true lag. A detector does not know the true lag; it takes the best lag. The figure shows the noise-free correlation peak, maximized over all lags, for two 63-sample preambles and for two segmented correlators, as the offset grows to 8 bins (a bin being \(1/(N T_s)\), the inverse preamble duration):
For a random BPSK preamble (blue), the peak follows PySDR’s formula down to about \(-14\) dB. Beyond that the “peak” is no longer the preamble at all: the best lag is now a sidelobe, a chance alignment of the sequence with a shifted copy of itself. This matches PySDR’s statement that beyond about half a bin, detection becomes impossible.
The Zadoff-Chu preamble (yellow) never drops below \(-5.4\) dB anywhere in the range. At integer offsets the reason is visible in the test output:
offset (bins) ZC: peak, dB @ lag random BPSK: peak, dB @ lag
1 -4.4 @ -25 -14.2 @ 8
2 -2.0 @ 13 -11.5 @ 6
3 -1.8 @ -12 -13.7 @ 6
5 -0.1 @ 1 -15.4 @ -42
The Zadoff-Chu peak does not shrink, it moves. A ZC sequence of root \(u\) has the algebraic property that multiplying it by a complex exponential of \(k\) bins gives (up to a constant phase) the same sequence cyclically shifted by \(k u^{-1} \bmod N\) samples, where \(u^{-1}\) is the inverse of \(u\) modulo \(N\). For \(u = 5\), \(N = 63\), the inverse is 38 (\(5 \cdot 38 = 190 = 3 \cdot 63 + 1\)), so one bin of offset moves the peak by 38, which is \(-25\) modulo 63. The test predicts the lag for every row from this formula and checks it. The peak magnitude still drops a little because the preamble is not periodic: shifted by 25 samples, only 38 of its 63 samples overlap the template.
For a detector this is good news and bad news. A ZC packet detector keeps working at frequency offsets that would blind a BPSK preamble, but it reports the packet at the wrong time, off by \(k u^{-1}\) samples, and nothing in the correlation peak says so. PySDR’s packet detector in the same chapter uses exactly this preamble, so a frequency offset there turns into a timing error rather than a missed packet. LTE meets the same property in its random-access channel, whose preambles are Zadoff-Chu sequences: for high-speed cells, where Doppler is large, 3GPP TS 36.211 (section 5.7.2) defines “restricted sets” of cyclic shifts so that the shifted peaks cannot be confused with other users’ preambles.
Segmented Correlators¶
PySDR’s non-coherent remedy splits the \(N\)-sample preamble into \(M\) segments of \(S = N/M\) samples, correlates each segment coherently, and adds the segments’ powers:
which reads: within a segment the phase rotation is small, and between segments it no longer matters, because only magnitudes are added. segmented_correlator<M> is the streaming correlator from CFAR and Real-Time Packet Detection with the inner sum split into \(M\) parts; the number of segments is a template parameter, so the split costs nothing at run time. It emits one power cell per sample, indexed by the alignment start, so it plugs directly into the CFAR stage there (packet_detector::process_cells).
For a constant-modulus preamble, the noise-free peak relative to the ideal \(N^2\) follows directly from the formula above: each segment contributes \(S^2 D_S(\Delta f)^2\), where \(D_S\) is the Dirichlet ratio for \(S\) samples, so
The test checks this to 0.05 dB for 7 and 9 segments. The figure shows the trade: at zero offset the segmented correlators start \(10\log_{10} M\) below the coherent one (8.5 and 9.5 dB), the non-coherent integration loss PySDR mentions; in exchange, their first null moves out to \(M\) bins. Beyond about one bin they beat the coherent correlator, which by then is only finding sidelobes. (The figure’s axis is peak power relative to an equally noisy coherent correlator. The real detection loss of non-coherent combining is smaller than \(10\log_{10} M\), because summing \(M\) powers also reduces the noise’s variance, which is what the threshold of a CFAR detector depends on.)
The Frequency Shift Trick¶
The brute-force remedy correlates against many frequency-shifted copies of the preamble. Done naively, that is one full correlation per frequency bin. PySDR describes the shortcut: correlate in the frequency domain, \(\mathrm{IFFT}(R \cdot P^*)\), and shift the preamble’s spectrum instead of re-computing anything. Multiplying the preamble by a complex exponential of \(k\) FFT bins moves its spectrum by \(k\) positions, so \(P_k[i] = P[(i - k) \bmod N_{FFT}]\). The received block is transformed once, the preamble once (in the constructor), and each frequency costs one complex multiply per bin and one inverse FFT:
fftwf_execute(fwd_r_.get()); // the received block, transformed once
for (std::size_t j = 0; j <= 2 * K_; ++j) {
const std::ptrdiff_t k = std::ptrdiff_t(j) - std::ptrdiff_t(K_);
for (std::size_t i = 0; i < n_; ++i) {
const std::size_t src = std::size_t((std::ptrdiff_t(i) - k + std::ptrdiff_t(n_)) % std::ptrdiff_t(n_));
work_[i] = R_[i] * std::conj(P_[src]); // the preamble shifted by k bins: an index, not a copy
}
fftwf_execute(inv_.get());
... // row j of the (frequency x lag) grid
}
return grid_view(grid_.data(), 2 * K_ + 1, n_); // std::mdspan: grid[k + K, lag]
The result is a frequency-by-lag grid, returned as an std::mdspan view as in The FFT and the Spectrogram. The bin spacing is \(f_s / N_{FFT}\), much finer than the \(1/(N T_s)\) that matters for the preamble, so the worst-case loss between grid points is tiny. In the test, a BPSK preamble hidden at lag 300 of a 1024-sample block with an offset of 7.3 FFT bins is found at bin 7, lag 300, with a loss below 0.01 dB. The test also checks the trick itself: the grid row for \(k = 7\) equals a brute-force correlation with the preamble multiplied by an NCO at 7 bins, to \(1.5 \cdot 10^{-7}\) relative.
This is the same search the GPS acquisition in Spreading Codes: GPS C/A Codes at Compile Time performs over Doppler. There we multiplied the received samples by an NCO for every Doppler hypothesis and repeated the forward FFTs each time: 21 hypotheses (\(-5\) to \(+5\) kHz in 500 Hz steps) times ten 1 ms blocks, 210 NCO passes and forward FFTs. With Doppler steps that are whole FFT bins, the trick needs ten forward FFTs in total, one per block, and an index offset per hypothesis (the phase difference between blocks does not matter, because their powers are added). The GPS search uses 4000-point FFTs at 4 MHz, so its bins are 1 kHz wide, and the 500 Hz steps fall halfway between bins every other time. The trick covers the whole-kilohertz hypotheses directly; for the others, a zero-padded 8000-point FFT halves the bin spacing.
For streams longer than one block, the FFT correlation becomes overlap-save, as PySDR notes: keep lags 0 to \(N_{FFT} - N\) of each block, which are free of wrap-around, and advance by that much (FIR Filters in C++ has the machinery).
Key Takeaways¶
PySDR’s Dirichlet loss formula is exact for constant-modulus preambles at the true lag; as a
constexprfunction it can guard a design.Beyond about one bin of offset, a coherent correlator’s best lag is a sidelobe: the preamble is effectively undetectable.
A Zadoff-Chu preamble is different: an offset of \(k\) bins moves its peak by \(k u^{-1} \bmod N\) samples with little loss. Detection survives; timing does not. PySDR’s packet detector uses such a preamble.
Non-coherent segments trade \(10 \log_{10} M\) of peak power for a null \(M\) times further out; the noise-free peak is exactly \(D_S(\Delta f)^2 / M\).
The frequency shift trick is an index rotation of a spectrum computed once: one forward FFT for the whole frequency search, verified against brute force.