22. Cyclostationary Processing

PySDR’s Cyclostationary Processing chapter (co-authored by Sam Brown) introduces signals whose statistics repeat periodically: BPSK at its symbol rate, OFDM at its symbol period. The tools are the cyclic autocorrelation function (CAF), \(R_x(\tau, \alpha)\), and its Fourier transform over \(\tau\), the spectral correlation function (SCF). The chapter computes them with explicit loops over the cyclic frequency \(\alpha\) and the lag \(\tau\), with the frequency smoothing and time smoothing methods, and with the FFT accumulation method.

The computations are where C++ has something to add. PySDR’s CAF loop takes 27 seconds on its 100 000-sample test signal. That is not because Python is slow but because the loop recomputes the same complex exponential 10 100 times. Rearranged, the same numbers take 105 ms in C++. Most of that factor comes from the rearrangement, which would help in NumPy as well, and the chapter shows both. It also fills in a section that PySDR marks as “coming soon”: detecting a signal with a known cyclic frequency, here OFDM by its cyclic prefix.

The code is in code/include/cppsdr/csp.hpp and code/tests/test_csp.cpp; figure-generating-scripts/csp_reference.py runs PySDR’s signal, CAF loop and FSM with a fixed seed. The FFT accumulation method is in code/include/cppsdr/fam.hpp and code/tests/test_fam.cpp, with figure-generating-scripts/fam_reference.py as its reference.

The CAF Is an FFT in Disguise

PySDR evaluates the CAF for 100 values of \(\alpha\) and 101 lags:

for j in range(len(alphas)):
    for i in range(len(taus)):
        CAF[j, i] = (np.exp(1j * np.pi * alphas[j] * taus[i]) *
                     np.sum(samples * np.conj(np.roll(samples, taus[i])) *
                            np.exp(-2j * np.pi * alphas[j] * np.arange(N))))

Read the inner expression with \(\tau\) fixed. samples * np.conj(np.roll(samples, tau)) is the lag product \(p_\tau[n] = x[n]\,x^*[n-\tau]\), and the sum over \(n\) with \(e^{-j2\pi\alpha n}\) is its discrete Fourier transform at frequency \(\alpha\). PySDR’s \(\alpha\) grid, \(0, 0.005, \ldots, 0.495\), consists of exact multiples of \(1/N\) for \(N = 100\,000\) (every 500th FFT bin). So for each lag, one FFT of the lag product delivers the CAF at every \(\alpha\) at once:

for (long tau = tau_min; tau <= tau_max; ++tau) {
    for (std::size_t n = 0; n < N; ++n)                          // the lag product, with np.roll's wrap-around
        buf[n] = x[n] * std::conj(x[(n + N - shift) % N]);
    fftwf_execute(plan.get());                                  // every alpha = k/N at once
    for (std::size_t j = 0; j < alphas; ++j)
        caf_[j * cols_ + (tau - tau_min)] = std::polar(1.0, pi * alpha_j * tau) * buf[j * step];
}

101 FFTs of 100 000 points replace 10 100 passes over 100 000 samples, each of which first builds a new array of complex exponentials. The result, an \(\alpha \times \tau\) table returned as an std::mdspan, matches PySDR’s to \(5 \cdot 10^{-8}\) of the peak. It shows the expected features at \(\alpha = 0.05, 0.10, 0.15\): the symbol rate of the 20-samples-per-symbol BPSK and its harmonics. The same rearrangement in NumPy (np.fft.fft of each lag product) would be about as fast; this is the “frequency shift trick” of Correlating Under Frequency Offset once more, an FFT replacing a bank of explicit frequency shifts. The trick needs \(\alpha\) on the FFT grid. For a single off-grid \(\alpha\), the direct sum with a rotator (one complex multiply per sample, see Oscillators and Frequency Shifting) is the right tool, and the detector below is exactly that.

Frequency Smoothing, Computing Less

PySDR’s Frequency Smoothing Method takes one large FFT of the signal and forms, for each \(\alpha\), the cyclic periodogram \(X(f + \alpha/2) X^*(f - \alpha/2)\) by rolling the spectrum. It then smooths that with a 256-point Hann window and keeps every 256th value:

SCF[i, :] = np.convolve(SCF_slice, window, mode='same')[::Nw]   # apply window and decimate by Nw

np.convolve computes all 100 000 smoothed values, 256 multiply-adds each, and the slice then discards 255 of every 256. As with the decimating filter of A Complete Receiver, the C++ version evaluates the convolution only at the 391 kept positions, reproducing mode='same'’s alignment (the output index \(m\) is the full convolution at \(m + 127\)):

for (std::size_t c = 0; c < cols; ++c) {
    const std::size_t m = c * Nw;                     // kept output: full[m + off] = sum_k w[k] slice[m + off - k]
    cd acc = 0;
    for (std::size_t k = 0; k < Nw; ++k) { ... acc += window[k] * slice(m + off - k); }
    scf[i * cols + c] = std::abs(acc);
}

One more detail is needed to reproduce PySDR’s output exactly: the spectral shift is int(alphas[i] * N/2), and int truncates. For some \(\alpha\) the floating-point product lands just below an integer, so the shift is one less than intended. For 7 of the 300 values this happens: for \(\alpha\) index 73, for example, the product is 3649.9999999999995 and the shift becomes 3649 instead of 3650. The effect is tiny (the row is evaluated at a cyclic frequency \(2/N\) too low, a fiftieth of the grid step), and round would be the intended operation. Computing it with the same operations in the same order (std::size_t(alphas[i] * double(N) / 2), with the \(\alpha\) grid built as np.arange builds it) gives the same truncations. The 300 × 391 SCF then matches PySDR’s to \(6 \cdot 10^{-8}\) of the peak, computed in 61 ms.

Detecting a Known Cyclic Feature

PySDR’s chapter ends with a section, “Signal Detection With Known Cyclic Frequency”, that promises an example. The idea: if you know the cyclic frequency and the lag at which a signal type has a feature, you need only one value of the CAF, not a whole surface. For OFDM with \(N\) subcarriers and a cyclic prefix of \(CP\) samples, the feature is the one OFDM: Transmitter and Receiver used for synchronization. The prefix makes the signal correlate with itself at lag \(\tau = N\), and that correlation repeats every \(N + CP\) samples, so it appears at \(\alpha = 1/(N+CP)\). A streaming detector accumulates one CAF value and the signal power:

\[d = \frac{\left| \sum_n x[n]\, x^*[n-N]\, e^{-j2\pi n/(N+CP)} \right|}{\sum_n |x[n]|^2},\]

which reads: correlate the signal with itself \(N\) samples earlier, demodulate that product at the symbol rate, and normalize by the power. The cyclic_detector class does this with a ring of \(N+1\) samples and a wrapped phase accumulator. Three properties make it useful, and the test checks each on 500 OFDM symbols (64 subcarriers, 16-sample prefix, 40 000 samples):

  • It works below the noise. At \(-5\) dB SNR the statistic is 0.048; for noise alone it is 0.0008.

  • It needs no synchronization. Shifting the signal by 37 samples and adding a frequency offset of 0.01 cycles per sample changes the statistic by less than 3 %. A time shift only rotates the phase of the sum, and in the conjugate product \(x[n]x^*[n-N]\) a frequency offset becomes a constant phase.

  • It is specific. At \(\alpha = 1/96\), the cyclic frequency of an OFDM signal with a 32-sample prefix, the statistic drops to 0.006.

This is the reason cyclostationary features are popular for spectrum sensing in cognitive radio, as in the Sutton, Nolan and Doyle paper PySDR cites. The detector also needs only one complex multiply-add per sample and a few dozen bytes of state, which makes it practical in hardware.

The FFT Accumulation Method

The frequency smoothing method computes the SCF one cyclic frequency at a time. PySDR’s last method, the FFT accumulation method (FAM; Roberts, Brown and Loomis, “Computationally efficient algorithms for cyclic spectral analysis”, IEEE Signal Processing Magazine, 1991), computes every cyclic frequency the record can resolve at once. It runs two stages of FFTs:

  • split the record into \(P\) overlapping blocks of \(N_p\) samples with hop \(L\), apply a Hann window, and transform each block;

  • rotate each channel to absolute time, then for every pair of channels \((k, l)\) take the FFT over the \(P\) blocks of \(X_k \, X_l^*\).

PySDR’s example uses \(2^{14}\) samples, \(N_p = 512\), \(L = 128\) and \(P = 64\). That is \(512^2 = 262\,144\) second-stage FFTs of 64 points, written as a double loop in Python. The result is a \(16\,384 \times 512\) matrix: cyclic frequency \(\alpha\) from −1 to 1 in steps of \(1/N\), by 512 frequencies. csp::fam (code/include/cppsdr/fam.hpp) keeps PySDR’s parameters and output layout, and figure-generating-scripts/fam_reference.py runs PySDR’s code unchanged on this chapter’s BPSK signal. The full matrix is 67 MB, so the script saves every row’s maximum, nine whole rows, and the sum and sum of squares. The C++ result matches all of them to \(3 \times 10^{-7}\) (relative to the peak, single-precision FFTs against NumPy’s double). It takes 52 ms; PySDR’s loop takes 4.3 s. Three things in the C++ version are worth a look.

Both stages are batched FFTs with one plan each. The first stage is one fftwf_plan_many_dft over the 64 blocks, the way PySDR’s np.fft.fft(xw) transforms a 2-D array in one call. The second stage is a batch of 64-point transforms over contiguous columns, so the first stage’s output is stored transposed: one column of \(P\) values per channel.

The “frequency shift down” needs no trigonometry. PySDR multiplies by np.exp(-2j*np.pi*f*t) with \(f = c/N_p - 1/2\) and \(t = mL\). Its phase \(f t = (c - N_p/2)\, m L / N_p\) is an integer multiple of \(1/N_p\), so the factor is an \(N_p\)-th root of unity, found by an integer index into a table. With PySDR’s \(L = N_p/4\), only four entries are ever used: \(1, -j, -1, j\). The table stores those four exactly.

Half of the second stage is a mirror image. The product for the pair \((l, k)\) is the complex conjugate of the product for \((k, l)\), and the FFT of a conjugated sequence is the conjugated FFT reversed. So \(|X\!F_2|^2\) of \((l, k)\) at bin \(q\) is that of \((k, l)\) at \(-q\), and only pairs with \(l \le k\) need a transform. Channel \(k\) has \(k + 1\) such pairs and channel \(N_p - 1 - k\) has \(N_p - k\), so the two together fill exactly one batch of \(N_p + 1\) transforms:

for (std::size_t k = 0; k < np_ / 2; ++k) {
    const std::size_t k2 = np_ - 1 - k;
    std::size_t slot = 0;
    for (std::size_t l = 0; l <= k; ++l) product(k, l, slot++);
    for (std::size_t l = 0; l <= k2; ++l) product(k2, l, slot++);
    fftwf_execute(plan2_sym_.get());               // Np + 1 transforms of P points
    ...                                            // store (k, l) at q and (l, k) at -q
}

The mirrored matrix equals the one from all 262 144 transforms to \(10^{-8}\) of the peak, and the time drops from 87 to 52 ms. A first version that padded each batch with unused zero transforms saved only a third.

What does the result show? PySDR finds “a clear hot spot at 0.05 Hz” in the 2-D image, and its 1-D plot averages each row over frequency. On the C++ result, that mean puts the symbol rate, \(\alpha = 0.05005\), 30 dB above the median row, and twice the symbol rate 26 dB above it. The row maximum would not show this. With only 64 blocks averaged, each cell is a noisy estimate, and the largest of a row’s 512 cells is mostly noise. By that statistic the strongest row is \(\alpha = 0.00586\). White noise, which has no cyclic features at all, run through the same code has a row whose maximum is only 7 dB below that of \(\alpha = 0\). PySDR’s choice of the mean is the right one. One more detail: the FAM image shows \(|X\!F_2|^2\), the squared magnitude, while the FSM and TSM images show the magnitude, so its features look about twice as strong in dB.

Key Takeaways

  • For a fixed lag, the CAF over \(\alpha\) is the DFT of the lag product: on the FFT grid, one FFT per lag replaces PySDR’s \(\alpha \times \tau\) loop (105 ms instead of 27 s, same numbers to \(5 \cdot 10^{-8}\)).

  • Compute only what you keep: PySDR’s FSM smooths 100 000 values per \(\alpha\) and keeps 391.

  • To reproduce Python output bit for bit, reproduce its rounding too: int() truncation of a floating-point shift is part of the algorithm.

  • A known cyclic feature needs one CAF value: the OFDM prefix feature detects OFDM at \(-5\) dB SNR with no timing or frequency synchronization, in constant memory.

  • The FAM is two batched FFT stages. Its phase correction is a root of unity looked up by an integer index (four values with \(L = N_p/4\)), and the conjugate symmetry of the channel pairs halves the second stage: 52 ms against PySDR’s 4.3 s, equal to \(3 \times 10^{-7}\).

  • Summarize an SCF row by its mean over frequency, as PySDR does. The maximum of thousands of noisy cells reports the estimator’s variance, not the signal.