Skip to content

fix: report residuals as observed minus computed - #70

Merged
siebc merged 1 commit into
masterfrom
sc/residual-convention-fix
Aug 5, 2026
Merged

fix: report residuals as observed minus computed#70
siebc merged 1 commit into
masterfrom
sc/residual-convention-fix

Conversation

@giove-a

@giove-a giove-a commented Aug 5, 2026

Copy link
Copy Markdown

What

SatInfo.residual and SatInfo.rate_residual change sign. Both are now measured − modeled, the orientation GNSS software reports observation residuals in.

Neither sign had been chosen. The pseudorange residual was whatever LsqFit hands back (resid = model - data), and #67 wrote the range-rate one to match its wording. Self-consistent, but inverted against every reference implementation — and against any receiver that forms the same difference itself.

Why measured − modeled

residual
RTKLIB pntpos.c: rescode() v[nv] = P - (r + dtr - CLIGHT*dts + dion + dtrp), with H[j] = -e[j]
RTKLIB pntpos.c: resdop() v[nv] = -lam*obs[i].D[band] - (rate + x[3] - CLIGHT*dts)
GNSS-SDR vendors RTKLIB as src/algorithms/libs/rtklib/rtklib_pntpos.cc — both lines verbatim
PocketSDR src/sdr_pvt.c calls RTKLIB's pntpos() and logs ssat[i].resp[0], i.e. that same v, as its per-satellite res

Observed minus computed, in both domains, in all three.

How

The negation is the whole of the change. It is applied to the converged fit and to the solved velocity, so both solves — their iterations, their normal equations, the state they return — are untouched, and calc_pvt passes the vectors straight through.

return ξ_fit_ols.param, -ξ_fit_ols.resid                   # user_position
rate_residuals[j] -= dot(e, velocity) + velocity_and_drift[4]   # velocity solve

No API, type or arity change: field access and keyword construction are unaffected, which is why this is a fix: and not a feat!:.

One asymmetry survives, and is now documented

yⱼ carries -doppler * λ, so rate_residual runs in the geometric range-rate sense — positive while the satellite recedes. That is RTKLIB's resdop sense, but the negative of the λ · carrier_doppler a tracking loop works in. A consumer forming the rate residual from its own loops has to negate one of the two; magnitudes agree either way.

This is invisible to a magnitude check, so it is stated on SatInfo and at the residual loop rather than left to be rediscovered. It is exactly what bit the downstream port below: matching the wording of "modeled − measured" there produced numbers inverted against this package's, and only a numerical comparison caught it.

Tests

Every existing residual assertion in the suite is magnitude- or orthogonality-based, so all of them pass either sign — which is how the inversion went unnoticed in the first place. The new testset pins the orientations directly, in both domains:

  • a satellite measured 100 m long keeps a positive residual, one measured short a negative one, antisymmetric to sub-mm (the model is nonlinear in position, so not exactly);
  • bounded by the perturbation but not pinned to a fraction of it: how much of it the satellite keeps is 1 - Pⱼⱼ, its leverage;
  • a satellite whose Doppler is raised by 50 Hz — closing faster — moves its rate residual negative, by less than λ · 50 Hz.

Full suite passes, including the opt-in L125 real-data fix (PVT_RUN_INTEGRATION_TEST=true, 8/8).

Downstream

GNSSReceiver.jl's vector-tracking loop reports the same two fields from its navigation filter and has been aligned in lockstep (its sc/vector-tracking branch): pseudorange z - h(x), rate h(x) - z, the second because of the observable asymmetry above. Verified numerically against this branch rather than from the docstrings:

PVT (measured − modelled) = -1256.374…
VT   h − z                = -1256.374…   ← match
VT   z − h                =  1256.374…

Anything else thresholding on the sign of residual (rather than abs) inverts; rate_residual is new in 5.0.0 and not yet in General, so it has no released consumers to speak of.

Merges cleanly with #68 and #69 — neither touches these lines.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Aug 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.56%. Comparing base (85b1f63) to head (2066e47).

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #70   +/-   ##
=======================================
  Coverage   98.56%   98.56%           
=======================================
  Files           7        7           
  Lines         488      488           
=======================================
  Hits          481      481           
  Misses          7        7           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Benchmark Results (Julia v1)

Time benchmarks
master 2066e47... master / 2066e47...
calc_pvt/GPSL1/4sats/cold 0.0375 ± 0.0023 ms 0.0384 ± 0.0074 ms 0.977 ± 0.2
calc_pvt/GPSL1/4sats/warm 25.9 ± 6 μs 26.4 ± 6.3 μs 0.98 ± 0.32
calc_pvt/GPSL1/9sats/cold 0.0738 ± 0.0015 ms 0.0744 ± 0.0015 ms 0.992 ± 0.028
calc_pvt/GPSL1/9sats/warm 0.0412 ± 0.00079 ms 0.042 ± 0.0068 ms 0.983 ± 0.16
calc_pvt/GalileoE1B/4sats/cold 0.0342 ± 0.007 ms 0.035 ± 0.0067 ms 0.977 ± 0.27
calc_pvt/GalileoE1B/4sats/warm 20.2 ± 4.7 μs 21.1 ± 5 μs 0.958 ± 0.32
calc_pvt/GalileoE1B/5sats/cold 0.0502 ± 0.0077 ms 0.0505 ± 0.0009 ms 0.993 ± 0.15
calc_pvt/GalileoE1B/5sats/warm 23.6 ± 5.9 μs 24.1 ± 1.4 μs 0.978 ± 0.25
time_to_load 2.3 ± 0.019 s 2.38 ± 0.077 s 0.966 ± 0.032
Memory benchmarks
master 2066e47... master / 2066e47...
calc_pvt/GPSL1/4sats/cold 0.52 k allocs: 23.4 kB 0.524 k allocs: 23.6 kB 0.992
calc_pvt/GPSL1/4sats/warm 0.417 k allocs: 17.2 kB 0.419 k allocs: 17.3 kB 0.995
calc_pvt/GPSL1/9sats/cold 0.881 k allocs: 0.0366 MB 0.885 k allocs: 0.0368 MB 0.993
calc_pvt/GPSL1/9sats/warm 0.646 k allocs: 23.2 kB 0.648 k allocs: 23.3 kB 0.995
calc_pvt/GalileoE1B/4sats/cold 0.52 k allocs: 23.4 kB 0.524 k allocs: 23.6 kB 0.992
calc_pvt/GalileoE1B/4sats/warm 0.395 k allocs: 16 kB 0.397 k allocs: 16.1 kB 0.994
calc_pvt/GalileoE1B/5sats/cold 0.637 k allocs: 28.6 kB 0.641 k allocs: 28.8 kB 0.993
calc_pvt/GalileoE1B/5sats/warm 0.446 k allocs: 17.4 kB 0.448 k allocs: 17.5 kB 0.995
time_to_load 0.149 k allocs: 11.2 kB 0.149 k allocs: 11.2 kB 1

@giove-a
giove-a force-pushed the sc/residual-convention-fix branch from 9c11a68 to 0d8a789 Compare August 5, 2026 10:39
`SatInfo.residual` and `SatInfo.rate_residual` change sign. Both are now
measured − modeled, the orientation GNSS software reports observation residuals
in: RTKLIB's `rescode` (`v = P - (r + dtr - c·dts + dion + dtrp)`) and `resdop`
(`v = -λ·D - (rate + ċ - c·dts)`), and GNSS-SDR and PocketSDR, which take both
from RTKLIB — GNSS-SDR vendors it as `rtklib_pntpos.cc` and PocketSDR calls
`pntpos()` and logs `ssat[i].resp[0]` as its per-satellite residual.

Neither sign was chosen before. The pseudorange residual was whatever `LsqFit`
returns (`resid = model - data`), and the range-rate one was written to match its
wording. So the values were self-consistent but inverted against every reference
implementation, and against a receiver that forms the same difference itself.

The negation is the whole of the change: it is applied to the converged fit and
to the solved velocity, so both solves — their iterations, their normal
equations, the state they return — are untouched. `calc_pvt` passes the vectors
straight through. No API, type or arity changes.

One asymmetry is worth stating because it survives the fix and is invisible in
magnitudes: `yⱼ` carries `-doppler * λ`, so `rate_residual` runs in the geometric
range-rate sense — positive while the satellite *recedes* — which is RTKLIB's
`resdop` sense but the negative of the `λ · carrier_doppler` a tracking loop
works in. A consumer forming the rate residual from its own loops has to negate
one of the two. Documented on `SatInfo` and at the residual loop.

Tests pin both orientations directly (`test/pvt.jl`): a satellite measured 100 m
long keeps a positive residual and one measured short a negative one, antisym-
metric to sub-mm; and a satellite whose Doppler is raised by 50 Hz — closing
faster — moves its rate residual negative by less than `λ · 50 Hz`. Every other
residual assertion in the suite is magnitude- or orthogonality-based and so was
blind to the sign, which is why a flip needed pinning of its own. Full suite
passes, including the opt-in L125 real-data fix.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@giove-a
giove-a force-pushed the sc/residual-convention-fix branch from 0d8a789 to 2066e47 Compare August 5, 2026 10:48
@siebc
siebc merged commit 1bb2fe8 into master Aug 5, 2026
8 checks passed
@siebc
siebc deleted the sc/residual-convention-fix branch August 5, 2026 10:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants