4. Decibels with a Dimension: a Link Budget the Compiler Checks¶
PySDR’s Link Budgets chapter has no code at all, and it does not need any: a link budget is a column of numbers in decibels that you add up. Its ADS-B example (an aircraft 30 km from the receiver, transmitting at 1090 MHz) goes like this: 20 dBW of transmit power, plus 3 dB and 0 dB of antenna gain, minus 122.7 dB of free-space path loss and 6 dB of miscellaneous losses gives a received power of \(-105.7\) dBW. The noise power is \(k + T + B = -228.6 + 24.8 + 63 = -140.8\) dBW, and the SNR is the difference, 35.1 dB.
Every number in that calculation carries a unit, and the units are where link budgets go wrong in practice: a value in dBm added as if it were dBW (a 30 dB mistake), two powers in dB added together (which does not add the powers), a bandwidth entered in Hz instead of dB-Hz. Spreadsheets do not complain about any of these. In this chapter we write PySDR’s ADS-B budget in C++ so that each of those mistakes is a compile error, and so that the whole budget, including the check “does the link close?”, is evaluated by the compiler. The language features involved are strong types, user-defined literals, operator overloading on templates and constexpr, and none of them costs anything at run time.
What a dB Value Really Is¶
A value in decibels is \(10\log_{10}\) of a linear quantity divided by a reference. 0 dBW is 1 W, 24.8 dBK is 300 K, 63 dB-Hz is 2 MHz, and a plain dB value is a ratio with no unit at all. Adding logarithms multiplies the linear quantities:
Read it from left to right: the units of the linear quantities multiply, watts per kelvin per hertz times kelvin times hertz leaves watts. So when we add dB values, their dimensions multiply, which means the exponents of the base units add up. That gives us a type design. We describe the dimension of a dB value by three integer exponents, for watts, kelvins and hertz, and make them template parameters (code/include/cppsdr/units.hpp):
template <int W, int K, int Hz>
struct decibel {
double value; // in dB relative to 1 W^W K^K Hz^Hz
};
using dB = decibel<0, 0, 0>; // a pure ratio: gain, loss, SNR
using dBW = decibel<1, 0, 0>; // power relative to 1 W
using dBK = decibel<0, 1, 0>; // temperature relative to 1 K
using dBHz = decibel<0, 0, 1>; // bandwidth relative to 1 Hz
using dBW_per_K_Hz = decibel<1, -1, -1>; // Boltzmann's constant
Adding two dB values adds the exponents; subtracting subtracts them. That is one template for each operator:
template <int W1, int K1, int H1, int W2, int K2, int H2>
constexpr decibel<W1 + W2, K1 + K2, H1 + H2> operator+(decibel<W1, K1, H1> a, decibel<W2, K2, H2> b)
{
return {a.value + b.value};
}
At run time this is a single addition of two doubles; the struct is exactly one double, and all the bookkeeping happens in the type system while compiling. Now look at what falls out of these few lines without any extra rules:
dBW + dBhas exponents \((1,0,0) + (0,0,0) = (1,0,0)\): a power plus a gain is a power.dBW - dBWgives \((0,0,0)\), a ratio: the SNR is a plain dB value, as it should be.k + T + Bgives \((1,-1,-1) + (0,1,0) + (0,0,1) = (1,0,0)\): thermal noise comes out in dBW by itself.dBW + dBWgives \((2,0,0)\), “dB relative to one square watt”. That is not an error by itself (it is just the logarithm of a product of two powers), but it cannot be stored in adBWvariable or added to the received power, and that is where the compiler stops you.
The last point is the classic mistake. Two signals of 20 dBW and 17 dBW do not add up to 37 dBW; they add up to about 21.8 dBW, because powers add in watts. If you try:
constexpr dBW total = 20.0_dBW + 17.0_dBW;
Clang answers:
error: no viable conversion from 'decibel<1 + 1 aka 2, [2 * ...]>' to 'const decibel<1, [2 * ...]>'
which, translated, says: you made something with watts squared and wanted something with watts. The correct operation has to go through linear units, and the library provides it under a name that says what it does, power_sum(a, b).
Literals That Carry Their Unit¶
Writing dBW{20} everywhere would be correct but clumsy. C++11 user-defined literals let us write units the way they appear in PySDR’s text:
namespace literals {
constexpr dB operator""_dB(long double v) { return {double(v)}; }
constexpr dBW operator""_dBW(long double v) { return {double(v)}; }
constexpr dBW operator""_dBm(long double v) { return {double(v) - 30}; } // 0 dBm = -30 dBW
constexpr hertz operator""_MHz(long double v) { return {double(v) * 1e6}; }
constexpr meter operator""_km(long double v) { return {double(v) * 1e3}; }
constexpr kelvin operator""_K(long double v) { return {double(v)}; }
}
Notice _dBm. dBm is not a different dimension, only a different reference point, so it is simply converted to dBW when the literal is read. 30.0_dBm == 0.0_dBW is true, and the 30 dB mix-up becomes impossible. The linear quantities (hertz, kelvin, meter, watt) are small strong types too, so a frequency cannot be passed where a distance is expected. The standard library uses exactly this technique for time: std::chrono durations, with literals such as 10ms.
The ADS-B Budget, Compiled¶
The physics are constexpr functions, written in their textbook form rather than with PySDR’s precomputed constant \(-147.55\):
inline constexpr double speed_of_light = 299'792'458.0; // m/s, exact by definition
inline constexpr double boltzmann = 1.380649e-23; // J/K, exact by definition (SI 2019)
inline constexpr dBW_per_K_Hz k_B{10 * cx::log10(boltzmann)}; // -228.6 dBW/K/Hz
// Free-space path loss, (4 pi d f / c)^2, as a positive number of dB.
constexpr dB fspl(meter d, hertz f)
{
return {20 * cx::log10(4 * cx::pi * d.value * f.value / speed_of_light)};
}
constexpr dBW thermal_noise(kelvin T, hertz B) { return k_B + to_dB(T) + to_dB(B); }
(cx::log10 is the constexpr logarithm from Compile-Time DSP: Filters, Tables and Constellations.) And here is the budget itself, line for line PySDR’s table (code/examples/link_budget.cpp):
constexpr dBW Pt = 20.0_dBW; // ~100 W transmitter
constexpr dB Gt = 3.0_dB; // aircraft antenna, pointed down
constexpr dB Gr = 0.0_dB; // omnidirectional receive antenna
constexpr dB Lp = fspl(30.0_km, 1090.0_MHz); // University of Maryland to BWI
constexpr dB Lmisc = 6.0_dB; // mismatch, cables, connectors
constexpr dBW Pr = Pt + Gt + Gr - Lp - Lmisc; // received signal power
constexpr dBW N = thermal_noise(300.0_K, 2.0_MHz); // k T B
constexpr dB snr = Pr - N; // dBW - dBW: a plain ratio
static_assert(snr > 20.0_dB, "the link must close with margin");
The types on the left are not decoration. If any expression on the right had the wrong dimension (a gain forgotten, a loss added as a power, Hz where dB-Hz belongs), the line would not compile. The static_assert at the end makes the system requirement part of the build: change the distance to 3000 km and the program refuses to compile, with the message “the link must close with margin”. The test file also pins PySDR’s intermediate numbers (122.7 dB, \(-105.7\) dBW, \(-140.8\) dBW), and the program prints the table:
Pt 20.0 dBW
Gt 3.0 dB
Gr 0.0 dB
Lp -122.7 dB
Lmisc -6.0 dB
Pr -105.7 dBW (-75.7 dBm)
Noise -140.8 dBW (k = -228.6 dBW/K/Hz)
SNR 35.1 dB
Range for 10 dB SNR: 538 km (free space, before the horizon gets in the way)
PySDR suggests leaving the distance as a variable to find out how far away the receiver can hear aircraft. Since free-space loss grows by 20 dB per decade of distance, the range for a required SNR is \(d = 30\,\text{km} \cdot 10^{(\text{SNR} - \text{SNR}_\text{req})/20}\), also a constexpr function. For a required 10 dB it gives 538 km, beyond the radio horizon of an aircraft at cruising altitude seen from the ground (roughly 400 km), which is why real ADS-B range is limited by geometry rather than by power.
Type-level tests are possible too, and they document the rules better than a comment:
static_assert(!std::is_assignable_v<dBW&, decltype(Pt + Pt)>, "dBW + dBW is not a power");
static_assert(!std::is_assignable_v<dB&, decltype(Pr)>, "a power is not a ratio");
static_assert(std::is_same_v<decltype(k_B + to_dB(300.0_K) + to_dB(2.0_MHz)), dBW>,
"k T B comes out in dBW by itself");
How Far to Take This¶
Our decibel template is about forty lines and covers exactly what a link budget needs. For linear physical units in full generality (metres per second squared, newtons, conversions between unit systems) there are mature libraries: mp-units, which has been proposed for standardization, Boost.Units, and Aurora’s Au. The idea is always the one shown here: encode the dimension in the type, let template arithmetic track it, and pay nothing at run time.
In DSP code, the most useful places for strong types are usually not physical units but quantities that are easy to confuse because they are all double: sample rate versus frequency, radians per sample versus hertz, a gain in dB versus a linear gain. A function declared as nco(radians_per_sample w) cannot be called with a frequency in hertz by accident, and a std::chrono::duration cannot be confused with a sample count. The cost is a struct with one member; the benefit is that a whole class of bugs moves from the lab to the compiler.
Key Takeaways¶
A dB value is the logarithm of a linear quantity, so adding dB values multiplies dimensions. Encoding the dimension exponents as template parameters lets the compiler check every line of a link budget.
User-defined literals (
20.0_dBW,1090.0_MHz,-75.7_dBm) make the units visible in the source and convert references such as dBm on entry.With
constexpr, the whole budget is evaluated while compiling, andstatic_assert(snr > required)turns the requirement into a build check.Strong types cost nothing at run time: each is one
double, and the type arithmetic disappears after compilation.