Skip to content

Correlation and Detection

Correlator demo: maps and detectors

What you're seeing

Every panel is one class, doppler.correlation.Correlator, built as a different flavor (the Correlator guide says which knobs pick which), and each panel's title names its flavor.

Left — the 1-D map, coherent integration. BPSK PN reference, lag=17, SNR ≈ −6 dB. With a single frame the peak/mean ratio is ~4.0 — barely distinguishable from noise. After 8 coherent dwells (dwell=8) it rises to ~7.0, pulling the lag-17 peak cleanly above the noise floor. Coherent integration improves SNR by 10 log₁₀(M) dB.

Centre — the 2-D map, template match. An 8×8 complex template shifted by (row=3, col=5) is recovered in a single FFT2 call. The surface peak lands exactly on the injected shift.

Right — the 1-D detector's push() stream. Four signal dwells fire above threshold=5; noise-only dwells stay below it. Each dot is one dwell's test statistic: peak magnitude divided by local noise estimate.

How it works

execute() accumulates frames and returns output only on the dwell-th call; all other calls return None.

import numpy as np

from doppler.correlation import Correlator

N, LAG = 64, 17  # 1-D frame length and injected lag
NY, NX = 8, 8  # 2-D frame dimensions
ROW, COL = 3, 5  # 2-D shift (row, col)
SIGMA = 2.0  # noise amplitude; power = SIGMA² = 4  →  SNR ≈ −6 dB
DWELL = 8  # coherent integration depth
THRESHOLD = 5.0  # detection gate

rng = np.random.default_rng(42)

# Unit-magnitude BPSK PN references — fully deterministic.
ref1d = rng.choice(np.array([-1.0, 1.0], dtype=np.float32), size=N).astype(
    np.complex64
)
ref2d = rng.choice(
    np.array([-1.0, 1.0], dtype=np.float32), size=(NY, NX)
).astype(np.complex64)

noise_scale = np.float32(SIGMA / np.sqrt(2))


def noisy_frame() -> np.ndarray:
    """Return one N-sample CF32 frame: ref1d shifted by LAG, plus noise."""
    signal = np.roll(ref1d, LAG)
    noise = (rng.standard_normal(N) + 1j * rng.standard_normal(N)).astype(
        np.complex64
    ) * noise_scale
    return signal + noise


def noise_block(n_frames: int) -> np.ndarray:
    """Return n_frames*N samples of CF32 noise (no signal)."""
    total = N * n_frames
    return (
        rng.standard_normal(total) + 1j * rng.standard_normal(total)
    ).astype(np.complex64) * noise_scale



# 1-D map: dwell=1 vs dwell=8 — execute() accumulates frames and returns
# output only on the dwell-th call; all other calls return None.
with Correlator(ref1d, dwell=1) as c:
    mag_d1 = np.abs(c.execute(noisy_frame()))

with Correlator(ref1d, dwell=DWELL) as c:
    for _ in range(DWELL - 1):
        c.execute(noisy_frame())
    mag_d8 = np.abs(c.execute(noisy_frame()))

snr_d1 = mag_d1[LAG] / np.mean(np.delete(mag_d1, LAG))
snr_d8 = mag_d8[LAG] / np.mean(np.delete(mag_d8, LAG))
print(
    f"[1-D map]      dwell=1  peak/mean={snr_d1:.1f}"
    f"  |  dwell={DWELL}  peak/mean={snr_d8:.1f}"
)

# The dwell=8 coherent sum must place the global peak exactly at the
# injected lag, and integrating 8 frames must sharpen the peak/mean
# ratio over a single frame (coherent integration gain).
assert int(np.argmax(mag_d8)) == LAG, "dwell=8 peak not at injected lag"
assert snr_d8 > snr_d1, "no coherent integration gain at dwell=8"

The detector flavor (detect=1) wraps this loop and applies a CFAR threshold, so det.push(block) hands you one record per detection without your managing the dwell counter.

The 2-D map accumulates the dwell integration in the frequency domain — it sums the per-frame cross-spectra and inverts once on the dump rather than once per frame. The result is identical by linearity of the inverse FFT, but the single inverse amortizes over the dwell (~1.7× cheaper per frame at dwell=8, bench_correlator.py). This holds only for coherent (complex-sum) integration; see the 2-D Acquisition gallery for the details.

The detector — streaming CFAR

push(block) returns a structured array, one record per dwell that fires above the threshold: col is the lag, then peak_mag, noise_est and test_stat. The noise estimate is taken from lags [noise_lo, noise_hi]. A block may be any length, since a partial frame waits for the next push, and the record array is sized for the whole block, so a push never drops a detection.

# One coherent-dwell block of the same shifted-PN + noise frames.
block = np.concatenate([noisy_frame() for _ in range(DWELL)])

det = Correlator(ref1d, detect=1, dwell=DWELL, noise_lo=LAG + 4,
                 noise_hi=N - 1, threshold=THRESHOLD)
for hit in det.push(block):
    print(f"detection  lag={hit['col']}  stat={hit['test_stat']:.2f}")
# detection  lag=17  stat=7.34   (stat varies with noise; lag is
#                                 deterministic)

The 2-D map — standalone match

# 2-D map: recover a (row, col) circular shift in one FFT2 call.
x2d = np.roll(np.roll(ref2d, ROW, axis=0), COL, axis=1)
with Correlator(ref2d, dwell=1) as c:
    surf2d = np.abs(c.execute(x2d)).reshape(NY, NX)

peak_row, peak_col = np.unravel_index(surf2d.argmax(), (NY, NX))
print(
    f"[2-D map]    peak at (row={peak_row}, col={peak_col})"
    f"  (expected ({ROW}, {COL}))"
)

# 2-D template match: the correlation surface must peak exactly at the
# injected (row, col) circular shift.
assert (peak_row, peak_col) == (ROW, COL), "2-D peak not at true shift"

The 2-D detector (a 2-D ref with detect=1) wraps the 2-D map with the same CFAR gating for a full 2-D acquisition search — see the 2-D Acquisition gallery.

pip install "doppler-dsp[examples]"   # matplotlib + scipy, once
python -m doppler.examples.corr_demo   # → corr_demo.png