Skip to content

Python tool for aircraft Doppler audio geolocation using historical ADS-B flight data (Source-available)

Prototype batched measured-ridge feasibility bounds

Cinna Davis0d1009ce3ee5
diff --git a/src/dopplergeist/acoustic_screen.py b/src/dopplergeist/acoustic_screen.py
new file mode 100644
index 0000000..570657c
--- /dev/null
+++ b/src/dopplergeist/acoustic_screen.py
@@ -0,0 +1,89 @@
+"""Vectorized feasibility bounds for measured Doppler ridges."""
+import numpy as np
+
+from .matching import _frequency_scale
+
+
+def ridge_trial_rejections(measurement, event, factors, emissions):
+ """Propose joint-trial rejections from a supported event's loss bound.
+
+ The pseudo-Huber loss has a bounded convex dual. Any dual vector inside
+ its domain gives a lower bound, including when source frequency and drift
+ are constrained to the fitter's declared bounds. A subset of observed
+ samples also gives a lower bound because every omitted loss and the drift
+ penalty are nonnegative. Only bounds strictly above the existing 3-sigma
+ association threshold can reject a trial.
+
+ ``factors`` and ``emissions`` are the existing batched acoustic results,
+ with one row per clock and one column per measured ridge sample. Incomplete
+ support, invalid settings, ill-conditioned fits and uncertain numerical
+ results return False so the ordinary physical fit handles them.
+ """
+ factors = np.asarray(factors, dtype=float)
+ emissions = np.asarray(emissions, dtype=float)
+ trials = factors.shape[0] if factors.ndim else 0
+ rejected = np.zeros(trials, dtype=bool)
+ if factors.ndim != 2 or emissions.shape != factors.shape:
+ return rejected
+ try:
+ sigma = float(_frequency_scale(event))
+ fraction = float(event.get("quality", {}).get("source_drift_fraction", .02))
+ low, high = map(float, event["f0_range_hz"])
+ spec, ridge, weight, _ = measurement
+ times, ridge, weight = (np.asarray(a, dtype=float) for a in (spec.time, ridge, weight))
+ except (ValueError, TypeError, KeyError, AttributeError):
+ return rejected
+ if (not np.isfinite(sigma) or sigma <= 0 or not np.isfinite(fraction) or not 0 <= fraction <= .1
+ or not np.isfinite(low) or not np.isfinite(high) or not 0 < low <= high
+ or times.ndim != 1 or times.shape != ridge.shape or times.shape != weight.shape
+ or not np.all(np.isfinite(weight)) or np.any(weight < 0)):
+ return rejected
+ measured = np.isfinite(times) & np.isfinite(ridge) & (weight > 0)
+ if measured.sum() < 10 or factors.shape[1] != measured.sum():
+ return rejected
+ times, ridge, weight = times[measured], ridge[measured], weight[measured]
+ _, first = np.unique(times, return_index=True)
+ first = np.sort(first)
+ count = min(48, len(first))
+ if count < 3:
+ return rejected
+ selected = first[np.linspace(0, len(first)-1, count, dtype=int)]
+ active = np.flatnonzero(np.all(np.isfinite(factors), axis=1) & np.all(factors > 0, axis=1)
+ & np.all(np.isfinite(emissions), axis=1))
+ if not len(active):
+ return rejected
+ with np.errstate(over="ignore", invalid="ignore", divide="ignore"):
+ x, full_emission = factors[active][:, selected], emissions[active]
+ samples, y, w = full_emission[:, selected], ridge[selected], weight[selected]
+ span = np.ptp(full_emission, axis=1)
+ valid = span > 1e-5
+ span = np.where(valid, span, 1.)
+ u = (samples-(full_emission.min(axis=1)+full_emission.max(axis=1))[:, None]/2)/span[:, None]
+ z = x*u
+ a = np.sum(w*x*x, axis=1)
+ b = np.sum(w*x*z, axis=1)
+ d = np.sum(w*z*z, axis=1)
+ det = a*d-b*b
+ valid &= np.isfinite(det) & (det > 1e-10*a*d)
+ det = np.where(valid, det, 1.)
+ p, q = np.sum(w*x*y, axis=1), np.sum(w*z*y, axis=1)
+ alpha, beta = (d*p-b*q)/det, (a*q-b*p)/det
+ residual = y-alpha[:, None]*x-beta[:, None]*z
+ dual_fraction = residual/np.hypot(sigma, residual)
+ dual = 2*sigma*w*dual_fraction
+ p, q = np.sum(x*dual, axis=1), np.sum(z*dual, axis=1)
+ alpha, beta = (d*p-b*q)/det, (a*q-b*p)/det
+ dual -= w*(alpha[:, None]*x+beta[:, None]*z)
+ dual_fraction = dual/(2*sigma*w)
+ dual_fraction /= np.maximum(1., np.max(np.abs(dual_fraction), axis=1)/(1-1e-10))[:, None]
+ dual = 2*sigma*w*dual_fraction
+ conjugate = 2*sigma*sigma*w*dual_fraction**2/(1+np.sqrt(1-dual_fraction**2))
+ source_box = np.abs(np.sum(x*dual, axis=1))*high
+ drift_box = np.abs(np.sum(z*dual, axis=1))*(fraction*high)
+ value = np.sum(dual*y, axis=1)-np.sum(conjugate, axis=1)-source_box-drift_box
+ error = 1e-7*(1+np.sum(np.abs(dual*y), axis=1)+np.sum(np.abs(conjugate), axis=1)
+ +source_box+drift_box)
+ lower = np.maximum(0., value-error)/np.sum(weight)
+ rejected[active] = (valid & np.isfinite(lower)
+ & (lower > (3*sigma)**2*(1+1e-8)+1e-9))
+ return rejected
diff --git a/tests/test_real_acoustic_screen.py b/tests/test_real_acoustic_screen.py
new file mode 100644
index 0000000..4ea1ced
--- /dev/null
+++ b/tests/test_real_acoustic_screen.py
@@ -0,0 +1,61 @@
+"""Optional feasibility regression against recorded ridges and ADS-B tracks."""
+import json
+import os
+from pathlib import Path
+
+import numpy as np
+import pandas as pd
+import pytest
+
+from dopplergeist.acoustic_screen import ridge_trial_rejections
+from dopplergeist.data_access import FlightIndex
+from dopplergeist.geometry import ecef
+from dopplergeist.matching import (_fit_ridge_uncached, _frequency_scale,
+ batch_acoustic_trials, prepare_events)
+from dopplergeist.search import filter_association_event
+
+
+def test_real_ridge_rejections_have_supported_failing_witnesses():
+ source = os.environ.get("DOPPLERGEIST_REAL_CPA_FIXTURE")
+ if not source:
+ pytest.skip("set DOPPLERGEIST_REAL_CPA_FIXTURE to the real reference diagnostic")
+ fixture = json.loads(Path(source).read_text())
+ events, measurements = prepare_events(fixture["events"])
+ table = pd.DataFrame(fixture["cpa_rows"])
+ tracks = {track.id: track for track in FlightIndex(fixture["real_index"]).lookup(
+ sorted(set(table.flight_id)), max_tracks=2000, max_samples=2000000)}
+ point = fixture["point"]
+ observer = ecef(point["lat"], point["lon"], point["elev_m"])
+ witnessed = tested = unsupported = 0
+ for event, measurement in zip(events, measurements):
+ sigma = _frequency_scale(event)
+ fraction = event.get("quality", {}).get("source_drift_fraction", .02)
+ rows = filter_association_event(table, event, 5., None, 5.)
+ for row in rows.itertuples():
+ track = tracks.get(row.flight_id)
+ if track is None:
+ unsupported += 3
+ continue
+ # These are candidate shared clocks for the same recorded event;
+ # this diagnostic does not supply a location to the automatic run.
+ starts = row.receive_time-event["cpa_receive_s"] + np.array([-2.5, 0., 2.5])
+ batch = batch_acoustic_trials([track], [measurement], observer, starts)[0]
+ if batch is None:
+ unsupported += len(starts)
+ continue
+ factors, emissions = batch
+ rejected = ridge_trial_rejections(measurement, event, factors, emissions)
+ assert rejected.shape == starts.shape
+ incomplete = ~np.all(np.isfinite(factors) & np.isfinite(emissions), axis=1)
+ assert not np.any(rejected & incomplete)
+ tested += int((~incomplete).sum())
+ unsupported += int(incomplete.sum())
+ for i in np.flatnonzero(rejected):
+ fit = _fit_ridge_uncached(track, observer, measurement, starts[i],
+ event["f0_range_hz"], 343., sigma, fraction,
+ acoustic_result=(factors[i], emissions[i]))
+ assert fit["support_fraction"] == 1.
+ assert np.isfinite(fit["score"])
+ assert -fit["score"] > 3*sigma
+ witnessed += 1
+ assert tested > 100 and witnessed > 0 and unsupported > 0