Skip to content

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

[skip ci] Bound spectral frequency sweeps without changing scores (#8)

Cinna Davisf8d609a7e1ca
diff --git a/src/dopplergeist/search.py b/src/dopplergeist/search.py
index 30c2b46..351922f 100644
--- a/src/dopplergeist/search.py
+++ b/src/dopplergeist/search.py
@@ -206,14 +206,8 @@ def event_score(track, observer, spectrum, ridge, weight, start_time, f0_bounds,
f0 = np.clip(np.sum(w*factor[valid]*ridge[valid])/np.sum(w*factor[valid]**2),*f0_bounds)
rmse = np.sqrt(np.average((f0*factor[valid]-ridge[valid])**2,weights=w))
return -float(rmse),float(f0)
- frequencies = np.linspace(*f0_bounds,max(3,int(np.ceil((f0_bounds[1]-f0_bounds[0])/frequency_step))+1))
- predicted = frequencies[:,None]*factor[None,:]
- contrast = spectrum.sample(times[None,:],predicted)
- # Fixed support: candidates do not gain points by leaving the frequency band.
- scores = np.where(np.mean(np.isfinite(contrast),axis=1)>=0.95,
- np.mean(np.nan_to_num(contrast,nan=-4.0),axis=1),-np.inf)
- best = np.argmax(scores)
- return float(scores[best]),float(frequencies[best])
+ from .spectral_grid import best_frequency
+ return best_frequency(spectrum, times, factor, f0_bounds, frequency_step)
def score_candidate(candidate, point, tracks, events, measurements, mode="brightness",
diff --git a/src/dopplergeist/spectral_grid.py b/src/dopplergeist/spectral_grid.py
new file mode 100644
index 0000000..747b2d1
--- /dev/null
+++ b/src/dopplergeist/spectral_grid.py
@@ -0,0 +1,41 @@
+"""Bounded frequency batches for the existing spectrogram-contrast score."""
+
+import numpy as np
+
+
+def best_frequency(spectrum, times, factor, f0_bounds, frequency_step=0.2,
+ *, batch_size=64):
+ """Sweep constant emitted frequencies without a full grid-by-time matrix.
+
+ ``factor`` is the physical Doppler factor at each receiver time. The caller
+ supplies it from the same emission-time model and shared recording clock as
+ the other events. This function does not fit a clock or source drift.
+
+ Keep the original frequency grid, interpolation, 95 percent support rule,
+ missing-bin penalty, per-curve reduction and first-maximum tie rule. At most
+ ``batch_size`` curves are sampled together; temporary matrix storage scales
+ with that batch and the existing number of observed time bins.
+ """
+ if type(batch_size) is not int or batch_size < 1:
+ raise ValueError("Frequency batch size must be a positive integer")
+ times, factor = np.asarray(times), np.asarray(factor)
+ if times.ndim != 1 or factor.shape != times.shape or not len(times):
+ raise ValueError("Need matching nonempty time and Doppler-factor vectors")
+ if (len(f0_bounds) != 2 or not np.isfinite(f0_bounds).all()
+ or not 0 < f0_bounds[0] <= f0_bounds[1]
+ or not np.isfinite(frequency_step) or frequency_step <= 0):
+ raise ValueError("Need positive finite source frequencies and frequency step")
+ frequencies = np.linspace(*f0_bounds,
+ max(3, int(np.ceil((f0_bounds[1] - f0_bounds[0]) / frequency_step)) + 1))
+ best_score, best_source_frequency = -np.inf, frequencies[0]
+ for first in range(0, len(frequencies), batch_size):
+ trial = frequencies[first:first + batch_size]
+ predicted = trial[:, None] * factor[None, :]
+ contrast = spectrum.sample(times[None, :], predicted)
+ scores = np.where(np.mean(np.isfinite(contrast), axis=1) >= 0.95,
+ np.mean(np.nan_to_num(contrast, nan=-4.0), axis=1), -np.inf)
+ winner = np.argmax(scores)
+ # Strict improvement preserves the earliest frequency across batch ties.
+ if scores[winner] > best_score:
+ best_score, best_source_frequency = scores[winner], trial[winner]
+ return float(best_score), float(best_source_frequency)