20. Multipath Fading as a Channel Object

PySDR’s Multipath Fading chapter simulates a Rayleigh fading channel with Clarke’s “sum of sinusoids”. Each of \(N\) scattered paths arrives from a random direction \(\alpha_i\) with a random phase \(\phi_i\), so its Doppler shift is \(f_D \cos \alpha_i\); the channel coefficient is the normalized sum of all paths. The Python code evaluates this for a whole one-second time vector at once and plots the magnitude in dB. To use it in a larger simulation, PySDR says, “you would simply multiply the received signal by the complex number z”, which “would then update every time step”.

That last sentence describes a streaming object, and this chapter builds one. The C++ angle is the same one as in Oscillators and Frequency Shifting: a sinusoid that must continue forever is a rotator, and 100 independent rotators can be vectorized. The statistics are then checked against Rayleigh theory, which shows two things: one detail of PySDR’s plot, and one property of the sum-of-sinusoids model that is easy to overlook.

The code is in code/include/cppsdr/fading.hpp and code/tests/test_fading.cpp.

From a Time Vector to Rotators

PySDR’s loop computes, for every path, a cosine and a sine of \(2\pi f_D t \cos\alpha_i + \phi_i\) over the whole time vector:

x = x + np.random.randn() * np.cos(2 * np.pi * fd * t * np.cos(alpha) + phi)
y = y + np.random.randn() * np.sin(2 * np.pi * fd * t * np.cos(alpha) + phi)

A channel that runs for hours has no time vector, and computing \(\cos(2\pi f t)\) from an ever-growing \(t\) loses precision (Oscillators and Frequency Shifting measured how). Each path’s angle advances by a fixed amount per sample, so each path is a rotator: keep \((\cos\theta_i, \sin\theta_i)\) and rotate it by one complex multiplication per sample. With 100 paths, the per-sample work is 100 independent rotations, and independence is what a SIMD unit wants. Stored as a structure of arrays, one array per quantity, the loop is a textbook vectorization candidate:

cf32 next()
{
    double x = 0, y = 0;
#pragma omp simd reduction(+ : x, y)
    for (std::size_t i = 0; i < N; ++i) {
        x += a_[i] * c_[i];                                // PySDR's  randn * cos(...)
        y += b_[i] * s_[i];                                //          randn * sin(...)
        const double c = c_[i] * wc_[i] - s_[i] * ws_[i];  // rotate every path by one sample
        s_[i] = s_[i] * wc_[i] + c_[i] * ws_[i];
        c_[i] = c;
    }
    if (++since_renorm_ == 1024) renormalize();            // keep |rotator| = 1, as in the NCO chapter
    return {float(x), float(y)};
}

alignas(64) std::array<double, N> c_, s_, wc_, ws_, a_, b_;   // one array per quantity

This is the lesson of Feedback Loops: the Costas Loop and IIR Filters and Denormals once more: a recursion cannot be vectorized along time, but many independent recursions can be vectorized across. On the M1 the 100-path fader costs about 36 ns per sample, so one core can fade about 27 million samples per second. Over 10 seconds at 100 kHz (a million samples), its output matches PySDR’s directly evaluated formula, with the same path parameters, to \(10^{-7}\).

The path parameters are drawn as PySDR draws them (uniform angles and phases, Gaussian amplitudes), but from noise::gaussian of Noise, Random Numbers and Statistics, so a seed fixes the channel realization on every standard library.

GNU Radio’s flat_fader (in gr-channels) makes different choices. Per sample and per path it calls fmod on two phase accumulators and looks up sine and cosine in an 8192-entry table. Its arrival angles are not random but evenly spaced, \(\alpha_n = (2\pi n - \pi + \theta)/(4N)\), with a single offset \(\theta\) that performs a slow random walk. Structured angles of this kind are the subject of a body of literature on sum-of-sinusoids simulators with correct statistics (for example Zheng and Xiao, “Simulation models with correct statistical properties for Rayleigh fading channels”, IEEE Transactions on Communications, 2003). The next section shows why such work was needed.

Checking Against Rayleigh Theory

A Rayleigh channel has a few textbook properties that do not depend on how it was simulated, so they make good tests. The test runs 200 seconds of channel at 10 kHz with PySDR’s parameters (60 mph at 200 MHz, so \(f_D = 17.9\) Hz):

property

measured

theory

average power \(E|z|^2\)

1.010

1

time below \(-10\) dB, \(1 - e^{-0.1}\)

9.48 %

9.52 %

time below \(-20\) dB

1.02 %

1.00 %

downward crossings of \(-10\) dB, \(\sqrt{2\pi} f_D \rho e^{-\rho^2}\)

12.1 /s

12.8 /s

average fade duration below \(-10\) dB

7.8 ms

7.4 ms

with \(\rho = 10^{-10/20}\) the threshold relative to the RMS level. The level-crossing rate and fade duration formulas are Jakes’s (Microwave Mobile Communications, 1974). For a single realization of 200 seconds these agree well.

The last textbook property is the autocorrelation: Clarke’s model predicts \(R(\tau) = J_0(2\pi f_D \tau)\), a Bessel function. A C++ detail first: C++17 added Bessel functions to <cmath> (std::cyl_bessel_j), but libc++, the standard library on macOS, does not implement the special math functions; with GCC’s libstdc++ the same line compiles. So fading.hpp carries a 10-line constexpr power series for \(J_0\), which the test checks against the known first zero, 2.4048.

The comparison is instructive:

tau       J0      one realization   average of 20 realizations
 5.0 ms  +0.923       +0.931              +0.923
10.0 ms  +0.708       +0.741              +0.711
21.4 ms  +0.000       +0.102              +0.012
30.0 ms  -0.359       -0.239              -0.336
40.0 ms  -0.322       -0.243              -0.289

The average over 20 realizations follows \(J_0\), but a single realization does not: its time-averaged autocorrelation is off by up to 0.12. The reason is that a single realization has only 100 fixed arrival angles. Its time average is a weighted average of 100 cosines, a sample of the integral that produces \(J_0\), not the integral itself. In statistical terms, the model with random parameters is not ergodic in its autocorrelation: averages over time and averages over realizations differ. This is the defect that the structured-angle simulators in the literature address. In practice: run several channel realizations (independent seeds through std::seed_seq, as in Noise, Random Numbers and Statistics) rather than one long one when the result depends on the correlation in time, as it does for an interleaver or a tracking loop.

How Deep Are the Fades?

PySDR’s plotting code converts the magnitude with z_mag_dB = 10*np.log10(z_mag). Since z_mag is \(|z|\), an amplitude, its value in dB is \(20 \log_{10}|z|\); the factor 10 halves every fade in the plot. The figure shows both scales for one second of the C++ fader with PySDR’s parameters:

One second of Rayleigh fading in dB on the correct 20 log10 scale and on PySDR's 10 log10 scale

The difference matters for the chapter’s message. A real Rayleigh channel spends 9.5 % of its time more than 10 dB below its average. On the plotted scale, only 1 % of the time looks that deep (the test counts it: 1.02 %). The deep fades that make fading channels hard are twice as deep as PySDR’s figure suggests.

Frequency-Selective Fading

PySDR stops at flat fading, noting that frequency-selective fading “requires a multi-tap channel impulse response which we will not get into”. With a fader object, that impulse response is a few lines: a delay line whose taps are independent faders, each with its own average power,

\[y[n] = \sum_t g_t[n] \, x[n - d_t],\]

which reads: each delayed echo of the signal is scaled by its own, independently fading coefficient. tapped_delay_line<Taps> implements it; each tap’s fader gets its own seed, derived from the channel seed and the tap index through std::seed_seq, so the taps are independent but the whole channel is reproducible. Like every block in this book, it produces identical output whether fed in one call or in random pieces, and the test checks that. Two equal taps 5 samples apart give the textbook picture of frequency-selective fading: notches every \(f_s/5\) that move as the two coefficients change.

Key Takeaways

  • A fading channel that “updates every time step” is a streaming object; each of its sinusoids is a rotator, and 100 independent rotators vectorize (structure of arrays, #pragma omp simd): about 36 ns per sample.

  • The streaming fader matches PySDR’s formula to \(10^{-7}\) over a million samples, and matches Rayleigh theory in power distribution, level-crossing rate and fade duration.

  • A single sum-of-sinusoids realization does not have Clarke’s \(J_0\) autocorrelation; only the ensemble does. Use several independently seeded realizations.

  • std::cyl_bessel_j is standard C++17 but missing from libc++; a small constexpr series does the job.

  • Amplitudes in dB are \(20 \log_{10}\). PySDR’s fading plot uses \(10 \log_{10}|z|\) and shows fades half as deep as they are.