25. TDOA: Delays, Peaks and Positions

PySDR’s TDOA chapter locates a transmitter from the differences in the times its signal reaches three or more receivers. Each time difference puts the emitter on a hyperbola, and the hyperbolas cross at the emitter. The chapter’s simulation uses three receivers a few hundred metres apart, 20 MHz of band-limited noise sampled at 50 MS/s, and 10 dB SNR. It estimates the delays by cross-correlation, refines them to a fraction of a sample by zero-padding the cross spectrum 16 times, and turns them into a position with Fang’s closed-form solution and a Gauss-Newton refinement.

At 50 MS/s one sample is 6 metres of range, so everything depends on the fraction of a sample. That is where this chapter spends its time. GNU Radio has a block that does the same job in a flowgraph, gr::digital::corr_est_cc, which finds a known sync word and tags its arrival time to a fraction of a sample. Its estimator is a useful reference, and running it showed both how much the choice of estimator matters and one problem that only a streaming implementation can have. The geometry part then turns out to be the GPS problem with transmitter and receivers swapped, which removes the reference sensor and the pseudo-inverse from PySDR’s solver.

The code is in code/include/cppsdr/tdoa.hpp and code/tests/test_tdoa.cpp. figure-generating-scripts/tdoa_reference.py runs PySDR’s simulation and estimators unchanged, with PySDR’s seed, and saves the received signals and PySDR’s results. The GNU Radio comparison is code/gnuradio_block/test_gr_corr_est.cc.

Three Samples Are Not Enough

After a cross-correlation, the peak sample gives the delay to the nearest sample. The cheapest refinement looks at the peak and its two neighbours. If the true peak lies a little to the right, the right neighbour is larger than the left one, and the difference says by how much. GNU Radio’s corr_est_cc (gr-digital/lib/corr_est_cc_impl.cc) takes the centre of mass of the three squared magnitudes:

// Calculates the center of mass between the three points around the peak.
// Estimate is linear.
nom = d_corr_mag[i - 1] + 2 * d_corr_mag[i] + 3 * d_corr_mag[i + 1];
den = d_corr_mag[i - 1] + d_corr_mag[i] + d_corr_mag[i + 1];
double center = nom / den;
center = (center - 2.0); // adjust for bias in center of mass calculation

Just above it, under #if 0, is the parabola through the same three points, with a reference to Jacovitti and Scarano (“Discrete time techniques for time delay estimation”, IEEE Transactions on Signal Processing, 1993) and the remark that it is biased, “but this method is simple and fast.” Both are a line each in C++:

constexpr double centre_of_mass(double m_1, double m0, double p1) { return (m_1 + 2 * m0 + 3 * p1) / (m_1 + m0 + p1) - 2; }
constexpr double parabolic(double m_1, double m0, double p1) { return (m_1 - p1) / (2 * (m_1 - 2 * m0 + p1)); }

How good are they? The test delays PySDR’s signal (1000 samples of the 20 MHz noise) by an exact fraction of a sample, without noise, applied as a phase ramp in the frequency domain, and compares each estimate with the truth:

true offset

centre of mass of \(|c|^2\) (GNU Radio)

parabola on \(|c|^2\)

parabola on \(|c|\)

zero padding, U = 16 (PySDR)

Newton on the interpolant

0.10

−0.038

−0.020

−0.008

+0.025

\(-8 \cdot 10^{-9}\)

0.30

−0.114

−0.043

−0.016

+0.013

\(-2 \cdot 10^{-9}\)

0.45

−0.173

−0.021

−0.007

−0.013

\(2 \cdot 10^{-9}\)

(errors in samples; at 50 MS/s, 0.1 sample is 60 cm of range). The centre of mass is the worst by far. Its comment is accurate, the estimate is linear, but with a slope of about 0.6 instead of 1: a true offset of 0.45 samples comes out as 0.28. The reason is that three samples of a band-limited correlation peak are not the three masses of a point. The main lobe here is about 2.5 samples wide, and the neighbours carry most of the peak’s weight on both sides. The parabola does better, and better still on \(|c|\) than on \(|c|^2\), because the magnitude is closer to a parabola near its maximum. PySDR’s zero padding is unbiased but can only land on a 1/16 grid, so its error is anything up to half a grid step, 1/32 sample.

The GNU Radio cross-check confirms that the C++ line is the block’s estimator. It runs corr_est_cc in a flowgraph on a band-limited sync word delayed by 0 to 0.45 samples. Every time_est tag equals centre_of_mass applied to the block’s own correlation output (port 1), and the tags show the same slope: 0.058 for a delay of 0.1, 0.174 for 0.3, 0.259 for 0.45. For the block’s main purpose that is fine: it marks where a burst starts, and downstream timing recovery does the fine work. For TDOA it would be an error of up to a metre.

The Curve Between the Samples

A band-limited correlation is fully described by its samples, so the true peak can be computed rather than guessed. PySDR does it by zero-padding the cross spectrum \(G[k] = A^*[k]\,B[k]\) to 16 times the length and taking the inverse FFT. That evaluates the trigonometric interpolant

\[r(\tau) = \frac{1}{n} \sum_{k} G[k]\, e^{j 2\pi k \tau / n}\]

on a grid 16 times finer than the samples, which reads: the correlation at any lag, integer or not, is the inverse Fourier transform of the cross spectrum evaluated at that lag. But we only need \(r(\tau)\) near one peak, not at \(16n\) points. Its derivatives are just as easy, a factor \(j2\pi k/n\) per derivative, so Newton’s method on \(f(\tau) = |r(\tau)|^2\) converges in a few steps from the parabolic estimate:

std::array<cd, 3> interpolant(double tau) const     // r, r', r'' at tau
{
    const cd step = std::polar(1.0, w * tau), wrap = std::polar(1.0, -2 * pi * tau);
    cd rot = 1, r = 0, r1 = 0, r2 = 0;
    for (std::size_t k = 0; k < n_; ++k) {          // one rotator, as in the NCO chapter
        const double kk = k < half ? double(k) : double(k) - double(n_);
        const cd term = g_[k] * (k < half ? rot : rot * wrap);
        r += term;
        r1 += cd(0, w * kk) * term;
        r2 -= (w * kk) * (w * kk) * term;
        rot *= step;
    }
    ...
}
// Newton:  f' = 2 Re(r* r'),  f'' = 2 (|r'|^2 + Re(r* r'')),  tau -= f'/f''

The exponentials come from one complex rotator (Oscillators and Frequency Shifting), and bins above \(n/2\) count as negative frequencies, split where PySDR splits them. So this is exactly the curve that PySDR’s zero padding samples. Four Newton steps cost four passes over the \(n = 1723\) bins, against an inverse FFT of \(16 \times 1723 = 27\,568\) points for PySDR’s method. The result is accurate to \(10^{-8}\) samples, where the 16-times grid gives \(3 \cdot 10^{-2}\).

On PySDR’s own recorded simulation, read from the file the reference script writes, the C++ code reproduces PySDR’s integer lags, its U = 16 range differences (418.5, 34.5 and −384.375 m) and its PHAT results exactly. The Newton peaks agree with PySDR’s method run at U = 1024 to within that method’s grid of 1/1024 sample.

Weighting Is a Policy

PySDR presents the generalized cross-correlation (Knapp and Carter, 1976): weight the cross spectrum by some \(\Psi(f)\) before transforming back. It discusses plain correlation (\(\Psi = 1\)), Roth, SCOT and PHAT (\(\Psi = 1/|G|\)), and shows PHAT as a one-line change. In C++ the weighting is a template parameter. A weighting is anything that maps a bin value and its frequency to a new value, which a concept states:

struct plain { cd operator()(cd g, double) const { return g; } };
struct phat  { double eps = 1e-12; cd operator()(cd g, double) const { return g / (std::abs(g) + eps); } };

template <class W>
concept gcc_weighting = requires(W const w, cd g, double f) { { w(g, f) } -> std::convertible_to<cd>; };

template <gcc_weighting W = plain>
struct in_band {                          // any weighting, restricted to the signal's band
    double f_max;
    W inner{};
    cd operator()(cd g, double f) const { return std::abs(f) <= f_max ? inner(g, f) : cd{}; }
};

template <gcc_weighting W = plain> class cross_correlator { ... };
tdoa::cross_correlator<tdoa::in_band<>> xb(n, {0.2});   // |f| < 10 MHz of 50 MHz

The weighting is called once per bin inside the loop that forms \(G\), and the compiler inlines it, so the choice costs nothing at run time. in_band was not in the original plan. It came from a measurement.

How close to the bound? The test repeats PySDR’s simulation 200 times with new noise (600 delay estimates) and compares the RMS delay error with the Cramér-Rao bound for two noisy copies of a signal (see G. C. Carter, “Coherence and time delay estimation”, Proceedings of the IEEE, 1987). Written for complex samples, the bound sums over the frequency bins:

\[\frac{1}{\operatorname{var}(\hat\tau)} = \sum_k \left(\frac{2\pi k}{n}\right)^2 \frac{2\rho_k^2}{1 + 2\rho_k},\]

where \(\rho_k\) is the SNR in bin \(k\). This reads: every bin contributes its frequency squared, because high frequencies change phase fastest with delay, times how cleanly that bin is seen in both receivers. For PySDR’s settings the bound is 0.0143 samples.

estimator

RMS delay error [samples]

relative to the bound

zero padding, U = 16 (PySDR)

0.0264

1.85

plain correlation + Newton

0.0168

1.17

in_band<plain> + Newton

0.0138

0.97

PySDR’s 1/16 grid alone contributes a quantization error of \((1/16)/\sqrt{12} = 0.018\) samples, more than the bound. At these settings U = 16 is not enough, and the Newton refinement removes that limit at lower cost. The second line is more interesting. The signal occupies 20 MHz of the 50 MHz band, so 60 % of the bins contain only noise. Plain correlation adds them in with full weight, and they add variance without information. The maximum-likelihood weighting of Knapp and Carter gives them zero weight, and so does in_band. Restricting the band brings the estimate to the bound. (Slightly below it, 0.97, because 600 estimates determine an RMS to about 3 %.)

PHAT and echoes. PySDR recommends PHAT for multipath. The test adds an echo of amplitude 0.6 to one receiver, \(d\) samples after the direct path, at 20 dB, and averages the error over 100 noise realizations:

echo delay \(d\) [samples]

plain correlation

PHAT

1

0.365

0.350

2

0.644

0.526

3

0.461

0.149

5

0.262

0.147

8

0.179

0.093

12

0.032

0.109

(mean absolute error in samples). PySDR’s claim holds in the middle of the table: for echoes 3 to 8 samples late, PHAT’s error is two to three times smaller. At the ends it does not. An echo within about one resolution cell (\(1/B = 2.5\) samples) merges with the direct path, and no weighting separates two copies that the bandwidth cannot resolve. An echo far away, 12 samples here, no longer touches the main peak, so plain correlation is nearly unbiased. PHAT then pays the price PySDR mentions, because whitening raises the noise-only bins to the same weight as the signal. Which weighting to use depends on the channel. With the weighting as a template parameter, the choice is one type in the code.

A Peak at the Edge of a Buffer

Back to corr_est_cc, for a problem that has nothing to do with estimation theory. Look again at the three samples it reads: d_corr_mag[i - 1], d_corr_mag[i], d_corr_mag[i + 1]. d_corr_mag holds the correlation of the current work() call. What if the peak is the first output of the call, \(i = 0\)? The parabolic branch under #if 0 checked for that. The centre of mass that replaced it does not, so it reads one element before the vector. And if the correlation is still rising at the last output, the search stops at \(i = n - 1\) and reads one element past this call’s data.

Whether this happens depends on where the scheduler cuts the stream. The cross-check controls that. With a 64-sample sync word, the block’s FFT filter processes 65 samples at a time, so every work() call is a multiple of 65 long. Capping the call size at 65 with set_max_noutput_items(65) puts call boundaries at known places, and the test places six sync words so that their peaks fall exactly there:

offset

time_est, large calls

time_est, calls of 65

centre of mass of the output

324

(no tag)

−0.103

(extra detection)

325

0.000

0.353

0.000

520

0.058

0.379

0.058

715

0.116

0.405

0.116

Same input, same block, different results. With large calls every tag is correct. With calls of 65, every peak at a boundary gets a wrong time estimate. The values fit d_corr_mag[-1] reading as 0, but that is luck, since the read is undefined behaviour. The first sync word also gets a second, spurious tag at the last output of the previous call. In a normal flowgraph the calls are thousands of samples long, so only a peak that lands on the first or last output of a call is affected: two positions per call. That is rare enough to go unnoticed and common enough to matter for a measurement system. The same code is on GNU Radio’s main branch as of November 2026; the report is drafted in issue_drafts/gnuradio.md.

The general lesson is one that earlier chapters turned into a habit: a streaming block must give the same output however its input is split. The OFDM synchronizer of OFDM: Transmitter and Receiver, the timing recovery of Timing Recovery with a Compile-Time Filter Bank, the streaming Welch estimator of The FFT and the Spectrogram and the complete receiver of A Complete Receiver are all tested by feeding them the same data in different chunk sizes and comparing the results bit for bit. A peak search needs one sample of look-behind and one of look-ahead, which means two samples of state carried between calls, or a history() in GNU Radio terms. The batch cross_correlator of this chapter has no such issue because it sees the whole record. A streaming version would carry the last two magnitudes.

Positions: GPS Backwards

PySDR turns range differences into a position in two steps. Fang’s closed form uses the two pairs that include sensor 0. The Gauss-Newton refinement then uses all three pairs, weighted with the pseudo-inverse of their covariance \(\mathbf{C} = \mathbf{D}\mathbf{D}^\top\), because pairs that share a sensor share its error. tdoa::fang is a direct port of the first, constexpr and with no allocation. Since it is constexpr, the claim that it recovers the emitter from exact data is a static_assert:

constexpr tdoa::sensors<3> rx{vec2{65, 229}, vec2{676, 123}, vec2{153, 543}};
constexpr vec2 tx{153, 355};
static_assert([] {
    const auto u = tdoa::fang(rx, r(1) - r(0), r(2) - r(0));
    return u && near(u->x, tx.x, 1e-6) && near(u->y, tx.y, 1e-6);
}());

For the second step there is a simpler formulation. Each sensor \(i\) measures the arrival time of the signal, which in metres is its range \(|\mathbf{u} - \mathbf{s}_i|\) to the emitter plus an unknown common offset \(b\), the emission time:

\[p_i = |\mathbf{u} - \mathbf{s}_i| + b + \varepsilon_i ,\]

which reads: the measured value is distance plus an offset that is the same for every sensor. This is the GPS pseudorange equation of Spreading Codes: GPS C/A Codes at Compile Time, with the satellite’s role taken by the sensors and the receiver clock bias replaced by the emission time. Pair differences cancel \(b\), which is all TDOA measures. So we can work with per-sensor values \(p_i\) plus one extra unknown, instead of pairs and their correlated covariance. The unknown \(b\) drops out of the least-squares problem in closed form, since its best value is the mean residual. What remains in each Gauss-Newton step is a 2 × 2 system:

\[\Big(\sum_i (\mathbf{e}_i - \bar{\mathbf{e}})(\mathbf{e}_i - \bar{\mathbf{e}})^\top\Big)\,\boldsymbol\delta = \sum_i (\mathbf{e}_i - \bar{\mathbf{e}})\,\rho_i ,\]

with \(\mathbf{e}_i\) the unit vector from sensor \(i\) to the current estimate, \(\bar{\mathbf{e}}\) their mean, and \(\rho_i\) the residual. This reads: the matrix is the scatter of the unit vectors pointing from the sensors to the emitter, and the step moves the estimate along the directions in which the residuals disagree. There is no reference sensor and no pseudo-inverse. The two forms are the same thing. PySDR’s \(\mathbf{J}^\top\mathbf{C}^{+}\mathbf{J}\) contains the projector \(\mathbf{D}^\top(\mathbf{D}\mathbf{D}^\top)^{+}\mathbf{D}\), which for any complete or reference set of pairs is the centring matrix \(\mathbf{I} - \mathbf{1}\mathbf{1}^\top/N\), and centring the unit vectors is exactly what the scatter matrix does. Three range differences become per-sensor values with \(p = \mathbf{D}^\top m / N\), the least-squares solution, because \(\mathbf{D}^\top\mathbf{D} = N\mathbf{I} - \mathbf{1}\mathbf{1}^\top\):

const auto p = tdoa::pseudoranges_from_pairs<3>(range_diff);          // D^T m / N
const vec2 u = tdoa::gauss_newton(rx, p, *tdoa::fang(rx, range_diff[0], range_diff[1]));

On PySDR’s data this gives (152.888690547, 355.000409161), PySDR’s pinv-weighted result to all nine printed decimals. The test also checks the property PySDR describes for four sensors, that the correctly weighted solution does not depend on which sensor is the reference. Here it holds by construction: reordering the sensors and changing the common offset changes the result by less than \(10^{-9}\) m. Everything is fixed size (std::array<vec2, N>, a sym2 with a closed-form solve), so the solver allocates nothing and runs at compile time as well. A second static_assert checks that it converges to the emitter from (300, 300).

GDOP Is the Same Matrix

The scatter matrix in the Gauss-Newton step is also the Fisher information for the position, per unit variance of each sensor’s range error: \(\mathbf{F} = \sum_i (\mathbf{e}_i - \bar{\mathbf{e}})(\mathbf{e}_i - \bar{\mathbf{e}})^\top\). So the geometric dilution of precision, \(\mathrm{GDOP} = \sqrt{\operatorname{tr}\mathbf{F}^{-1}}\), needs no new code. It also explains PySDR’s numbers without a matrix inverse. At the centre of a regular \(N\)-gon the unit vectors are spread evenly, \(\bar{\mathbf{e}} = 0\), and \(\mathbf{F} = \tfrac{N}{2}\mathbf{I}\), so \(\mathrm{GDOP} = 2/\sqrt{N}\). That is \(2/\sqrt{3} = 1.15\) for PySDR’s equilateral triangle and \(\sqrt{2/3} = 0.82\) for its ring of six. Both are static_asserts, and so is PySDR’s warning that the shortcut \(\sqrt{\operatorname{tr}(\mathbf{J}^\top\mathbf{J})^{-1}}\) without the covariance gives a value below 1 for three sensors:

static_assert(near(tdoa::gdop(triangle, vec2{0, 0}), 2 / cx::sqrt(3), 1e-9));
static_assert(near(tdoa::gdop(hexagon, vec2{0, 0}), cx::sqrt(2.0 / 3), 1e-9));
static_assert(gdop_shortcut(triangle, vec2{0, 0}) < 1);

The noise trials close the loop. With the in_band delay estimates, the per-sensor range error is \(0.0138/\sqrt 2\) samples, or 0.059 m. PySDR’s geometry has a GDOP of 1.204 at the emitter, which predicts a position error of 0.071 m. The measured RMS position error over the 200 trials is 0.071 m.

The same trials check the bound itself, and here PySDR’s chapter has a problem in its figure rather than its text. The script behind the “Cramér-Rao lower bound” plot computes var_tau = 1 / (8 pi^2 beta^2 T gamma) with \(T\) in seconds. That has units of seconds, not seconds squared, because the time-bandwidth product \(TB\) is missing. For the chapter’s own simulation the script’s formula gives a ranging error of 413 m. The measured error per pair is 0.083 m, and the formula with \(TB = 400\) and the two-sensor effective SNR gives 0.0835 m. The plotted curves, 10 m to 10 km, are high by a factor of \(\sqrt{B \cdot 1\,\mathrm{s}}\), about 1000 at 1 MHz. This is item 11 of the PySDR draft in issue_drafts/pysdr.md.

Key Takeaways

  • At 50 MS/s a sample is 6 m, so the sub-sample estimator decides the accuracy. GNU Radio’s three-point centre of mass returns about 0.6 times the true offset on a band-limited signal. The parabola on \(|c|\) is within 0.016 samples; PySDR’s 16-fold zero padding within 1/32.

  • The correlation between samples is the inverse DFT of the cross spectrum. Newton’s method on it, with one rotator for the exponentials, finds the peak to \(10^{-8}\) samples for four passes over the bins instead of a 16-times-longer inverse FFT.

  • Make the GCC weighting a policy. in_band<plain> reaches the Cramér-Rao bound where plain correlation is 17 % above it. PHAT helps against echoes 3 to 8 samples late and hurts against a well-separated one.

  • Test a streaming block by splitting its input differently. corr_est_cc mis-tags peaks that land on a work() boundary, because its three-point refinement reads outside the current call’s buffer.

  • TDOA is GPS backwards: per-sensor pseudo-ranges plus an unknown emission time. That form needs no reference sensor and no pseudo-inverse, and it gives PySDR’s weighted Gauss-Newton result to nine decimals. Its normal matrix, the scatter of the unit vectors, is the Fisher information, and GDOP is \(2/\sqrt{N}\) at the centre of a regular array.

  • Measured position error equals GDOP times the sensor range error: 0.071 m against a predicted 0.071 m. PySDR’s CRLB figure omits the time-bandwidth product and is high by about \(10^3\) to \(10^4\).