You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
743 lines
30 KiB
743 lines
30 KiB
"""Reference receiver (sparse end, K = 1). |
|
|
|
Pipeline |
|
1. decimate to the chip rate (anti-alias) |
|
2. detect the repeated up-chirps (non-coherent FFT combining) -> bin b_up = f - tau |
|
3. find the down-chirp -> bin b_dn = f + tau; separate CFO f and timing tau |
|
4. refine at the exact frame alignment: fractional CFO from the phase advance |
|
between consecutive preamble symbols, sub-bin peaks with a Jacobsen estimator |
|
5. derotate CFO, resample the *original* samples on the TX chip grid |
|
(sample-clock error eps is derived from the CFO: shared-crystal model) |
|
6. dechirp + FFT every payload symbol; pilots give the bin offset, carrier |
|
phase and sub-chip timing; hard decisions on index (Gray) and QPSK |
|
""" |
|
from dataclasses import dataclass |
|
import numpy as np |
|
from dsp import resample_at, wrap, jacobsen |
|
from phy import (PhyParams, base_chirp_chips, inv_gray, qpsk_demap, bits_to_symbols, |
|
payload_layout, n_symbols_for_bits) |
|
|
|
|
|
@dataclass |
|
class Sync: |
|
T: int # integer frame start in chip-rate buffer |
|
tau0: float # fractional frame start (chips, chip-rate buffer) |
|
f_bins: float # CFO in FFT bins (bin = bw/M Hz) |
|
f_hz: float |
|
eps: float # sample-clock error implied by CFO |
|
metric: float # detection metric |
|
|
|
|
|
def excise(x, p, thresh_db=8.0, nfft=1024, dilate=2): |
|
"""Notch strong narrowband interference before demodulation. |
|
|
|
A chirp's spectrum is flat across the signal band, so any spectral region |
|
standing well above the in-band median is something else: a carrier, an FM |
|
voice signal, a spur. Average the spectrum over the whole buffer (Welch, |
|
Hann, 50% overlap), mark bins more than ``thresh_db`` above the in-band |
|
median (plus ``dilate`` bins either side for window leakage), and zero |
|
those frequencies in one FFT of the buffer. Notching a fraction q of the |
|
band costs the signal roughly 20*log10(1-q) dB; an unexcised interferer |
|
costs it everything once the interference exceeds the processing gain. |
|
|
|
Returns (x_clean, notched_fraction_of_signal_band). |
|
""" |
|
x = np.asarray(x, np.complex128) |
|
x = x - x.mean() # DC is handled separately; don't notch it |
|
N = len(x) |
|
if N < 2 * nfft: |
|
return x, 0.0 |
|
hop = nfft // 2 |
|
win = np.hanning(nfft) |
|
nseg = (N - nfft) // hop + 1 |
|
idx = np.arange(nseg)[:, None] * hop + np.arange(nfft)[None, :] |
|
psd = np.mean(np.abs(np.fft.fft(x[idx] * win, axis=1)) ** 2, axis=0) |
|
f = np.fft.fftfreq(nfft, 1 / p.fs) |
|
inband = np.abs(f) < p.bw / 2 |
|
mask = psd > np.median(psd[inband]) * 10 ** (thresh_db / 10) |
|
if not mask.any(): |
|
return x, 0.0 |
|
mask = np.convolve(mask.astype(int), np.ones(2 * dilate + 1, int), "same") > 0 |
|
q = float(mask[inband].mean()) |
|
X = np.fft.fft(x) |
|
fb = np.fft.fftfreq(N, 1 / p.fs) |
|
kbin = np.round(fb / (p.fs / nfft)).astype(int) % nfft # full bin -> Welch bin |
|
X[mask[kbin]] = 0 |
|
return np.fft.ifft(X), q |
|
|
|
|
|
def decimate(x, p): |
|
if p.osr == 1: |
|
return np.asarray(x, complex) |
|
pos = np.arange(0, len(x), p.osr, dtype=np.float64) |
|
return resample_at(x, pos, cutoff=1.0 / p.osr) |
|
|
|
|
|
def _win_fft(r, start, M, ref): |
|
return np.fft.fft(r[start:start + M] * ref) |
|
|
|
|
|
P_DET = 2 # zero-padding factor of the detection FFT |
|
|
|
|
|
def preamble_metric(r, p, block=4096): |
|
"""Detection metric for a preamble starting in each M-chip window of r. |
|
|
|
2x zero-padded FFT: a tone between bins loses up to 3.9 dB in a plain |
|
M-point FFT, but at most ~0.9 dB on the half-bin grid. Consistency metric: |
|
at the bin that wins the combined spectrum of L consecutive windows, every |
|
one of them must show a peak; geometric mean of the per-window peak/mean |
|
ratios (a lone strong window, e.g. a pilot next to quiet windows, scores |
|
low). Returns (metric[k], winning bin B[k] on the half-bin grid). |
|
""" |
|
M, L = p.M, p.n_preamble - 1 |
|
cu = np.conj(base_chirp_chips(M)) |
|
nw = len(r) // M |
|
if nw < L: |
|
return np.zeros(0), np.zeros(0, int) |
|
W = r[:nw * M].reshape(nw, M) |
|
Eu = np.empty((nw, P_DET * M)) |
|
for a in range(0, nw, block): # bounded memory on long captures |
|
Eu[a:a + block] = np.abs(np.fft.fft(W[a:a + block] * cu, n=P_DET * M, axis=1)) ** 2 |
|
cs = np.vstack([np.zeros((1, P_DET * M)), np.cumsum(Eu, axis=0)]) |
|
S = cs[L:] - cs[:-L] # S[k] = sum of windows k..k+L-1 |
|
nk = S.shape[0] |
|
B = np.argmax(S, axis=1) |
|
ratio = Eu / Eu.mean(axis=1, keepdims=True) |
|
lg = np.zeros(nk) |
|
for i in range(L): |
|
lg += np.log(ratio[np.arange(nk) + i, B] + 1e-12) |
|
return np.exp(lg / L), B |
|
|
|
|
|
def detect_all(r, p, det_thresh=4.0): |
|
"""All preamble candidates in a long chip-rate buffer, in time order. |
|
|
|
Returns window indices k (preamble starts near chip k*M), strongest first |
|
within each cluster: one candidate per preamble (non-maximum suppression |
|
over n_preamble windows). |
|
""" |
|
metric, _ = preamble_metric(r - r.mean(), p) |
|
cand = np.where(metric >= det_thresh)[0] |
|
keep = [] |
|
for k in cand[np.argsort(-metric[cand])]: |
|
if all(abs(k - j) > p.n_preamble for j in keep): |
|
keep.append(int(k)) |
|
return sorted(keep), metric |
|
|
|
|
|
def acquire(r, p, det_thresh=4.0): |
|
M, n_pre = p.M, p.n_preamble |
|
s1, s2 = p.sync_shifts |
|
r = r - r.mean() |
|
up = base_chirp_chips(M) |
|
cu = np.conj(up) |
|
nw = len(r) // M |
|
L = n_pre - 1 |
|
if nw < n_pre + 6: |
|
return None |
|
W = r[:nw * M].reshape(nw, M) |
|
P = P_DET |
|
metric, B = preamble_metric(r, p) |
|
k = int(np.argmax(metric)) |
|
if metric[k] < det_thresh: |
|
return None |
|
bu = B[k] / P # up-chirp bin = f - tau (mod M) |
|
|
|
# Align windows to the symbol boundaries implied by the up-chirps, assuming |
|
# f = 0: frame start T_m = (-bu mod M) + m*M. The true start is T_m + f, so |
|
# on these windows the sync up-chirps dechirp to exactly their shifts |
|
# (independent of f), and both full down-chirps dechirp to bin 2f, which |
|
# can be combined. Score every whole-symbol hypothesis m with all four. |
|
def pk(X, c, tol=1): |
|
return max(abs(X[(c + d) % len(X)]) ** 2 for d in range(-tol, tol + 1)) |
|
|
|
tau_g = (-bu) % M |
|
best = None |
|
for m in range(k - 3, k + 2): |
|
Tm = int(round(tau_g + m * M)) |
|
if Tm < 0 or Tm + int((n_pre + 4.25) * M) + M > len(r): |
|
continue |
|
Xs1 = np.fft.fft(r[Tm + n_pre * M:Tm + (n_pre + 1) * M] * cu) |
|
Xs2 = np.fft.fft(r[Tm + (n_pre + 1) * M:Tm + (n_pre + 2) * M] * cu) |
|
D = sum(np.abs(np.fft.fft(r[Tm + (n_pre + 2 + j) * M:Tm + (n_pre + 3 + j) * M] * up, |
|
n=P * M)) ** 2 for j in range(2)) |
|
bd = int(np.argmax(D)) |
|
E = D[bd] / P + pk(Xs1, s1) + pk(Xs2, s2) |
|
if best is None or E > best[0]: |
|
best = (E, Tm, bd / P) |
|
if best is None: |
|
return None |
|
_, Tm, bd = best |
|
f_c = wrap(bd, M) / 2.0 # |f| < M/4 |
|
T = int(round(Tm + f_c)) |
|
if T < 0 or T + int((n_pre + 4.25) * M) > len(r): |
|
return None |
|
|
|
# ---- fine estimation at aligned windows |
|
Xu = np.array([_win_fft(r, T + i * M, M, cu) for i in range(n_pre)]) |
|
b = int(np.argmax((np.abs(Xu) ** 2).sum(axis=0))) |
|
ph = np.angle(np.sum(Xu[1:, b] * np.conj(Xu[:-1, b]))) |
|
f_frac = ph / (2 * np.pi) |
|
rot = np.exp(-2j * np.pi * f_frac * np.arange(n_pre)) |
|
bu_r = wrap(b + jacobsen(rot @ Xu, b), M) |
|
|
|
Xd = np.array([_win_fft(r, T + (n_pre + 2 + j) * M, M, up) for j in range(2)]) |
|
bdi = int(np.argmax((np.abs(Xd) ** 2).sum(axis=0))) |
|
Yd = Xd[0] + Xd[1] * np.exp(-2j * np.pi * f_frac) |
|
bd_r = wrap(bdi + jacobsen(Yd, bdi), M) |
|
|
|
f_real = f_frac + np.round((bu_r + bd_r) / 2 - f_frac) |
|
tau_r = (bd_r - bu_r) / 2 |
|
f_hz = f_real * p.bw / M |
|
return Sync(T=T, tau0=float(T + tau_r), f_bins=float(f_real), f_hz=float(f_hz), |
|
eps=float(f_hz / p.fc), metric=float(metric[k])) |
|
|
|
|
|
def extract_frame(x, sync, p, n_chips): |
|
"""Derotate CFO, drop DC, resample original samples onto the TX chip grid. |
|
|
|
Only the span of ``x`` the resampler touches is derotated. The DC estimate |
|
still uses the whole buffer. |
|
""" |
|
m = np.arange(n_chips) |
|
pos = p.osr * (sync.tau0 + m / (1.0 + sync.eps)) |
|
margin = 16 * p.osr + 2 # > resampler half-width (6 * osr) |
|
lo = max(0, int(np.floor(pos[0])) - margin) |
|
hi = min(len(x), int(np.ceil(pos[-1])) + margin) |
|
if hi <= lo: |
|
return np.zeros(n_chips, complex) |
|
n = np.arange(lo, hi) |
|
xd = (x[lo:hi] - x.mean()) * np.exp(-2j * np.pi * sync.f_hz * n / p.fs) |
|
return resample_at(xd, pos - lo, cutoff=1.0 / p.osr) |
|
|
|
|
|
def refine(x, sync, p, iters=3): |
|
"""Iterative fine sync on the clock-corrected chip grid. |
|
|
|
In the raw RX grid the sample-clock error makes the timing offset drift |
|
across the preamble (~1 chip per 50k chips at 20 ppm), which biases the |
|
up/down-chirp estimates. Resampling with the current eps estimate makes |
|
the residual offset constant, so the up/down pair measures it without |
|
bias. eps follows the CFO under the shared-crystal model. |
|
""" |
|
M, n_pre = p.M, p.n_preamble |
|
up = base_chirp_chips(M) |
|
cu = np.conj(up) |
|
for _ in range(iters): |
|
z = extract_frame(x, sync, p, p.payload_start) |
|
Xu = np.array([np.fft.fft(z[i * M:(i + 1) * M] * cu) for i in range(n_pre)]) |
|
b = int(np.argmax((np.abs(Xu) ** 2).sum(axis=0))) |
|
ph = np.angle(np.sum(Xu[1:, b] * np.conj(Xu[:-1, b]))) |
|
f_frac = ph / (2 * np.pi) |
|
rot = np.exp(-2j * np.pi * f_frac * np.arange(n_pre)) |
|
bu = wrap(b + jacobsen(rot @ Xu, b), M) |
|
o = (n_pre + 2) * M |
|
Xd = np.array([np.fft.fft(z[o + j * M:o + (j + 1) * M] * up) for j in range(2)]) |
|
bdi = int(np.argmax((np.abs(Xd) ** 2).sum(axis=0))) |
|
bd = wrap(bdi + jacobsen(Xd[0] + Xd[1] * np.exp(-2j * np.pi * f_frac), bdi), M) |
|
f_res = f_frac + np.round((bu + bd) / 2 - f_frac) |
|
tau_res = (bd - bu) / 2 |
|
f_hz = sync.f_hz + f_res * p.bw / M |
|
sync = Sync(T=sync.T, tau0=sync.tau0 + tau_res, f_bins=f_hz * M / p.bw, |
|
f_hz=f_hz, eps=f_hz / p.fc, metric=sync.metric) |
|
return sync |
|
|
|
|
|
def _smooth_phase(slots, pil, ph, half=2): |
|
"""Moving linear regression of unwrapped pilot phase, evaluated at slots. |
|
|
|
Each slot fits a line through the 2*half+2 pilots around it (fewer at the |
|
frame edges). Closed-form least squares from running sums, vectorized. |
|
""" |
|
ph = np.unwrap(ph) |
|
npil = len(pil) |
|
slots = np.asarray(slots) |
|
c = np.searchsorted(pil, slots) |
|
lo = np.maximum(0, np.minimum(c - half - 1, npil - 2 * half - 2)) |
|
hi = np.minimum(npil, lo + 2 * half + 2) |
|
xv = np.asarray(pil, np.float64) |
|
|
|
def csum(v): |
|
return np.concatenate([[0.0], np.cumsum(v)]) |
|
c1, cx, cy, cxx, cxy = (csum(np.ones(npil)), csum(xv), csum(ph), |
|
csum(xv * xv), csum(xv * ph)) |
|
n = c1[hi] - c1[lo] |
|
sx, sy = cx[hi] - cx[lo], cy[hi] - cy[lo] |
|
sxx, sxy = cxx[hi] - cxx[lo], cxy[hi] - cxy[lo] |
|
den = n * sxx - sx * sx |
|
ok = (n >= 2) & (den > 0) |
|
a = np.where(ok, (n * sxy - sx * sy) / np.where(ok, den, 1.0), 0.0) |
|
b0 = np.where(ok, (sy - a * sx) / np.where(n > 0, n, 1.0), ph[np.minimum(lo, npil - 1)]) |
|
return a * slots + b0 |
|
|
|
|
|
def _track_pilots(X, pil, M, win=2): |
|
"""Locate each pilot peak within +-win bins of the expected offset. |
|
|
|
After refine() the residual bin offset is ~0 and drifts slowly, so an |
|
unconstrained argmax over all M bins only adds chances of locking onto a |
|
noise bin (a few % per pilot at 10 dB peak SNR, M=128), which then corrupts |
|
alignment and phase for every symbol around it. The expected offset follows |
|
the running median of accepted pilots. |
|
Returns (pilot peak bins, sub-bin offsets b_real, pilot complex values). |
|
""" |
|
offs = np.arange(-win, win + 1) |
|
ref = 0 |
|
hist, am_p, b_real, pv = [], [], [], [] |
|
for k in pil: |
|
cand = (ref + offs) % M |
|
a = int(cand[np.argmax(np.abs(X[k, cand]))]) |
|
br = wrap(a, M) + jacobsen(X[k], a) |
|
am_p.append(a) |
|
b_real.append(br) |
|
pv.append(X[k, a]) |
|
hist.append(br) |
|
ref = int(np.round(np.median(hist[-5:]))) |
|
return np.array(am_p), np.array(b_real), np.array(pv) |
|
|
|
|
|
def demodulate(z, p, n_data, tau_comp=False): |
|
M = p.M |
|
is_p, _ = payload_layout(n_data, p.pilot_every) |
|
S = len(is_p) |
|
cu = np.conj(base_chirp_chips(M)) |
|
W = z[p.payload_start:p.payload_start + S * M].reshape(S, M) |
|
X = np.fft.fft(W * cu, axis=1) |
|
am = np.argmax(np.abs(X), axis=1) |
|
pil = np.where(is_p)[0] |
|
slots = np.arange(S) |
|
|
|
am_p, b_real, pv = _track_pilots(X, pil, M) |
|
am[pil] = am_p |
|
b_i = np.interp(slots, pil, b_real) # bin offset per slot |
|
b_int = np.round(b_i) |
|
delta = b_i - b_int # sub-bin part (= -tau_res) |
|
|
|
phi = _smooth_phase(slots, pil, np.angle(pv)) |
|
|
|
idx_hat = (am - b_int.astype(int)) % M |
|
v = X[slots, am] * np.exp(-1j * phi) |
|
if tau_comp: |
|
v = v * np.exp(-2j * np.pi * idx_hat * delta / M) |
|
d = ~is_p |
|
idx_d, a_d = idx_hat[d], v[d] |
|
|
|
val = inv_gray(idx_d) |
|
bits_idx = ((val[:, None] >> np.arange(p.sf - 1, -1, -1)) & 1).astype(np.uint8) |
|
bi, bq = qpsk_demap(a_d) |
|
bits = np.concatenate([bits_idx, bi[:, None], bq[:, None]], axis=1).reshape(-1) |
|
info = dict(idx=idx_d, a=a_d, pilot_bin=b_real, pilot_phase=phi[pil], |
|
peak_snr=np.abs(X[slots, am]) ** 2) |
|
return bits, info |
|
|
|
|
|
# ------------------------------------------------------------------ soft demap |
|
def _lse(a, axis): |
|
m = np.max(a, axis=axis, keepdims=True) |
|
return np.squeeze(m, axis) + np.log(np.sum(np.exp(a - m), axis=axis)) |
|
|
|
|
|
def _logcosh2(x): |
|
"""log(2 cosh x), stable.""" |
|
ax = np.abs(x) |
|
return ax + np.log1p(np.exp(-2 * ax)) |
|
|
|
|
|
def _logI0(x): |
|
small = x < 5 |
|
out = np.empty_like(x) |
|
out[small] = np.log(np.i0(x[small])) |
|
xl = x[~small] |
|
out[~small] = xl - 0.5 * np.log(2 * np.pi * xl) + np.log1p(1 / (8 * xl)) |
|
return out |
|
|
|
|
|
def _local_linear(xp, yp, xq, W, loo=False): |
|
"""Local linear least-squares fit of (complex) pilot values yp at xp. |
|
|
|
Each query point uses the W pilots nearest to it in index (shifted inward at |
|
the frame edges). With ``loo`` the queries are the pilots themselves and |
|
each pilot is left out of its own fit (window W+1 minus itself), which |
|
gives an honest prediction error. Vectorized with running sums. |
|
""" |
|
xp = np.asarray(xp, np.float64) |
|
yp = np.asarray(yp) |
|
n = len(xp) |
|
Wn = min(W + (1 if loo else 0), n) |
|
c = np.arange(n) if loo else np.searchsorted(xp, xq) |
|
lo = np.clip(c - Wn // 2, 0, n - Wn) |
|
hi = lo + Wn |
|
|
|
def cs(v): |
|
return np.concatenate([[0], np.cumsum(v)]) |
|
c1, cx, cy, cxx, cxy = cs(np.ones(n)), cs(xp), cs(yp), cs(xp * xp), cs(xp * yp) |
|
N = c1[hi] - c1[lo] |
|
sx, sy = cx[hi] - cx[lo], cy[hi] - cy[lo] |
|
sxx, sxy = cxx[hi] - cxx[lo], cxy[hi] - cxy[lo] |
|
if loo: |
|
N, sx, sy = N - 1, sx - xp, sy - yp |
|
sxx, sxy = sxx - xp * xp, sxy - xp * yp |
|
xq = xp |
|
den = N * sxx - sx * sx |
|
ok = (N >= 2) & (den > 1e-9) |
|
a = np.where(ok, (N * sxy - sx * sy) / np.where(ok, den, 1.0), 0.0) |
|
b = (sy - a * sx) / np.maximum(N, 1) |
|
return a * np.asarray(xq, np.float64) + b |
|
|
|
|
|
TRACK_WINDOWS = (2, 3, 4, 6, 8, 12, 16, 24) |
|
|
|
|
|
def _fit_channel(xs, ys, query, windows): |
|
"""Channel gain at ``query`` slots from samples ys at slots xs. |
|
|
|
1. Residual frequency: the preamble's CFO estimate can be tens of Hz off |
|
when it lands near a fade (a Rayleigh channel's instantaneous frequency |
|
swings well past the Doppler limit there). Estimate the common |
|
rotation from consecutive samples at the most frequent spacing |
|
(weighted phase-increment estimator) and remove it. |
|
2. Local linear fit of the derotated samples, window chosen by |
|
leave-one-out prediction error. |
|
3. Put the rotation back. |
|
Returns (h at query, window). |
|
""" |
|
xs = np.asarray(xs, np.float64) |
|
n = len(xs) |
|
if n < 3: |
|
return np.full(len(query), np.mean(ys)), n |
|
dx = np.diff(xs) |
|
step = np.bincount(dx.astype(int)).argmax() |
|
pr = ys[1:] * np.conj(ys[:-1]) |
|
w = 0.0 if step == 0 else np.angle(np.sum(pr[dx == step])) / step |
|
yd = ys * np.exp(-1j * w * xs) |
|
best = None |
|
for W in windows: |
|
if W > n - 1: |
|
break |
|
err = np.mean(np.abs(yd - _local_linear(xs, yd, None, W, loo=True)) ** 2) |
|
if best is None or err < best[0]: |
|
best = (err, W) |
|
W = best[1] if best else n - 1 |
|
q = np.asarray(query, np.float64) |
|
return _local_linear(xs, yd, q, W) * np.exp(1j * w * q), W |
|
|
|
|
|
def _channel_track(pil, pv, slots): |
|
"""Per-slot complex channel gain from the pilots (see _fit_channel).""" |
|
return _fit_channel(pil, pv, slots, TRACK_WINDOWS) |
|
|
|
|
|
def _freq_ramp(Y, is_p, p, min_ratio=4.0): |
|
"""Linear frequency drift across a frame, from where each symbol's peak sits. |
|
|
|
The preamble fixes the frequency at the start of the frame; if the carrier |
|
then drifts (an oscillator warming up), the residual grows through the |
|
frame. Pilot *phase* can only follow it up to +-1/(2 * pilot spacing) |
|
(+-0.85 Hz at SF12, 62.5 kHz) before wrapping, but each symbol's peak |
|
*position* (sub-bin, Jacobsen) shows the frequency unambiguously to |
|
+-0.5 bin. Fit offset = c1 * t through the preamble (where the residual |
|
is zero), using pilots and data symbols whose peak clearly stands out, |
|
with two Huber reweightings. Returns c1 in bins per chip (shrunk by |
|
c1^2 / (c1^2 + sigma^2)) and the slot-centre times in chips. |
|
""" |
|
M = p.M |
|
S = Y.shape[0] |
|
t = p.payload_start + np.arange(S) * M + M / 2 - p.n_preamble * M / 2 |
|
A2 = np.abs(Y) ** 2 |
|
kp = np.where(is_p, 0, np.argmax(A2, axis=1)) |
|
top = A2[np.arange(S), kp] |
|
second = np.partition(A2, -2, axis=1)[:, -2] |
|
use = is_p | (top > min_ratio * second) |
|
off = np.array([jacobsen(Y[i], int(kp[i])) for i in range(S)]) |
|
w0 = top * use |
|
w = w0.copy() |
|
c1, sig = 0.0, np.inf |
|
for _ in range(3): |
|
if w.sum() <= 0: |
|
return 0.0, t |
|
c1 = float(np.sum(w * t * off) / np.sum(w * t * t)) |
|
r = off - c1 * t |
|
scale = 1.4826 * np.median(np.abs(r[use])) + 1e-6 |
|
w = w0 * np.minimum(1.0, 1.5 * scale / (np.abs(r) + 1e-12)) |
|
n_eff = max(int(use.sum()) - 1, 1) |
|
sig = float(np.sqrt(np.sum(w * r * r) / (n_eff * np.sum(w * t * t)))) |
|
return c1 * c1 * c1 / (c1 * c1 + sig * sig) if c1 else 0.0, t |
|
|
|
|
|
def demodulate_soft(z, p, n_data, coherent=True, clip=50.0, tracker="adaptive", h=None, |
|
freq_ramp=False): |
|
"""Exact (log-sum-exp) bit LLRs over all M x 4 (index, QPSK) hypotheses. |
|
|
|
Model per data symbol s: X_k = h_s a delta(k-idx) + n_k, n_k ~ CN(0, sigma2), |
|
with h_s the complex channel gain (FFT units) estimated from the pilots. |
|
Hypothesis metric lambda(k,a) = (2/sigma2) Re(X_k conj(h_s) a*). The QPSK |
|
sum factors per axis, giving mu_k = lc(yr_k) + lc(yi_k) with |
|
y = sqrt(2)/sigma2 * X conj(h_s) and lc(x) = log 2cosh(x). |
|
``coherent=False`` uses log I0(2|h_s||X_k|/sigma2) for the index (phase-free) |
|
and keeps the coherent QPSK terms. |
|
|
|
``tracker``: "adaptive" (per-symbol complex gain, window chosen per frame) |
|
or "legacy" (sliding phase fit over 6 pilots, one amplitude per frame). |
|
``h``: per-slot channel gains to use instead of tracking the pilots (from |
|
``channel_from_decisions``). |
|
``freq_ramp``: remove a linear frequency drift across the frame first |
|
(see _freq_ramp); used as a retry when a stage fails. |
|
|
|
Returns LLRs in modulator bit order: per symbol [index bits MSB..LSB, I, Q]. |
|
""" |
|
M, sf = p.M, p.sf |
|
is_p, _ = payload_layout(n_data, p.pilot_every) |
|
S = len(is_p) |
|
cu = np.conj(base_chirp_chips(M)) |
|
W = z[p.payload_start:p.payload_start + S * M].reshape(S, M) |
|
X = np.fft.fft(W * cu, axis=1) |
|
pil = np.where(is_p)[0] |
|
slots = np.arange(S) |
|
|
|
# pilots: bin offset (integer part used to align bins) and channel gain |
|
am_p, b_real, pv = _track_pilots(X, pil, M) |
|
b_int = np.round(np.interp(slots, pil, b_real)).astype(int) |
|
|
|
# align: Y[s, k] = X[s, (k + b_int[s]) mod M] |
|
cols = (np.arange(M)[None, :] + b_int[:, None]) % M |
|
Y = X[slots[:, None], cols] |
|
|
|
# noise power per bin |
|
E = np.abs(X) ** 2 |
|
pk = E.max(axis=1) |
|
sigma2 = float(np.mean((E.sum(axis=1) - pk) / (M - 1))) |
|
|
|
ramp = 0.0 |
|
if freq_ramp: |
|
ramp, tc = _freq_ramp(Y, is_p, p) |
|
rot = np.exp(-1j * np.pi * ramp * tc * tc / M) # phase of the drifting carrier |
|
Y = Y * rot[:, None] |
|
pv = pv * rot[pil] |
|
|
|
if h is not None: |
|
win = None |
|
elif tracker == "legacy": |
|
phi = _smooth_phase(slots, pil, np.angle(pv)) |
|
A = float(np.sqrt(max(np.mean(np.abs(pv) ** 2) - sigma2, 1e-12))) |
|
h, win = A * np.exp(1j * phi), 6 |
|
else: |
|
h, win = _channel_track(pil, pv, slots) |
|
|
|
hd = h[~is_p] |
|
Yd = Y[~is_p] * (np.sqrt(2) / sigma2 * np.conj(hd))[:, None] |
|
yr, yi = Yd.real, Yd.imag |
|
lr, li = _logcosh2(yr), _logcosh2(yi) |
|
if coherent: |
|
mu = lr + li |
|
else: |
|
mu = _logI0(2 * np.abs(hd)[:, None] / sigma2 * np.abs(Y[~is_p])) |
|
A = float(np.sqrt(np.mean(np.abs(h) ** 2))) |
|
|
|
val = inv_gray(np.arange(M)) |
|
llr = np.empty((len(Yd), sf + 2)) |
|
for j in range(sf): |
|
bit = (val >> (sf - 1 - j)) & 1 |
|
llr[:, j] = _lse(mu[:, bit == 0], 1) - _lse(mu[:, bit == 1], 1) |
|
# QPSK: index-marginalized, using the coherent per-bin terms |
|
llr[:, sf] = _lse(yr + li, 1) - _lse(-yr + li, 1) |
|
llr[:, sf + 1] = _lse(yi + lr, 1) - _lse(-yi + lr, 1) |
|
llr = np.clip(llr, -clip, clip) |
|
info = dict(sigma2=sigma2, A=A, peak_snr_db=10 * np.log10(A * A / sigma2), |
|
pilot_bin=b_real, pilot_phase=np.angle(h[pil]), yr=yr, yi=yi, |
|
track_window=win, Y=Y, is_pilot=is_p, pilot_val=pv, pilot_slots=pil, |
|
freq_ramp_hz_per_s=ramp * p.bw * p.bw / M) |
|
return llr.reshape(-1), info |
|
|
|
|
|
def _bit_llrs(mu, sf, clip=50.0): |
|
"""Index-bit LLRs (S, sf) from per-bin log-likelihoods mu (S, M).""" |
|
M = mu.shape[1] |
|
val = inv_gray(np.arange(M)) |
|
out = np.empty((mu.shape[0], sf)) |
|
for j in range(sf): |
|
b0 = ((val >> (sf - 1 - j)) & 1) == 0 |
|
out[:, j] = _lse(mu[:, b0], 1) - _lse(mu[:, ~b0], 1) |
|
return np.clip(out, -clip, clip) |
|
|
|
|
|
def demodulate_nc(z, p, n_data, clip=50.0, amp_smooth=3): |
|
"""Non-coherent demodulator: index bits only, no use of carrier phase. |
|
|
|
Pilots give the frequency (sub-bin peak position, interpolated per slot) |
|
and the amplitude (smoothed pilot power minus noise); each symbol is |
|
de-rotated by its own frequency before the FFT, so a residual offset of a |
|
fraction of a bin costs no scalloping loss. Per-bin metric for "the |
|
symbol is in bin k": mu_k = log I0(2 A |Y_k| / sigma2) (Rice vs Rayleigh |
|
likelihood ratio), whatever the phase does between or within symbols. |
|
|
|
Returns (llr (n_data * sf,), info); info['mu'] (data rows) feeds |
|
demap_extrinsic_nc for iterative decoding. |
|
""" |
|
M, sf = p.M, p.sf |
|
is_p, _ = payload_layout(n_data, p.pilot_every) |
|
S = len(is_p) |
|
cu = np.conj(base_chirp_chips(M)) |
|
D = z[p.payload_start:p.payload_start + S * M].reshape(S, M) * cu |
|
X = np.fft.fft(D, axis=1) |
|
pil = np.where(is_p)[0] |
|
slots = np.arange(S) |
|
_, b_real, pv = _track_pilots(X, pil, M) |
|
b = np.interp(slots, pil, b_real) |
|
Y = np.fft.fft(D * np.exp(-2j * np.pi * b[:, None] * np.arange(M)[None, :] / M), axis=1) |
|
|
|
E = np.abs(Y) ** 2 |
|
pk = E.max(axis=1) |
|
sigma2 = float(np.mean((E.sum(axis=1) - pk) / (M - 1))) |
|
a2 = np.maximum(E[pil, 0] - sigma2, 0.1 * sigma2) |
|
if amp_smooth > 1 and len(a2) >= amp_smooth: |
|
k = np.ones(amp_smooth) / amp_smooth |
|
a2 = np.convolve(np.pad(a2, amp_smooth // 2, mode="edge"), k, mode="valid") |
|
A = np.sqrt(np.interp(slots, pil, a2)) |
|
|
|
d = ~is_p |
|
mu = _logI0((2 * A[d] / sigma2)[:, None] * np.sqrt(E[d])) |
|
llr = _bit_llrs(mu, sf, clip) |
|
info = dict(sigma2=sigma2, A=float(np.sqrt(np.mean(A * A))), |
|
peak_snr_db=10 * np.log10(np.mean(A * A) / sigma2), mu=mu, Y=Y, |
|
is_pilot=is_p, pilot_bin=b_real, pilot_val=pv, pilot_slots=pil, |
|
freq_ramp_hz_per_s=0.0) |
|
return llr.reshape(-1), info |
|
|
|
|
|
def demap_extrinsic_nc(mu, sf, La, clip=50.0): |
|
"""Non-coherent demapper with a-priori index-bit LLRs (S, sf); returns |
|
extrinsic LLRs (S, sf).""" |
|
M = mu.shape[1] |
|
val = inv_gray(np.arange(M)) |
|
bits = ((val[:, None] >> np.arange(sf - 1, -1, -1)[None, :]) & 1) |
|
pi = 0.5 * La @ (1.0 - 2.0 * bits).T |
|
return np.clip(_bit_llrs(mu + pi, sf, np.inf) - La, -clip, clip) |
|
|
|
|
|
def estimate_drift(info, p): |
|
"""Residual sample-clock error from how the pilot peaks slide across a frame. |
|
|
|
The receiver's clock-error estimate comes from the CFO (eps = f / fc), |
|
which is wrong whenever part of the carrier offset has no matching clock |
|
error: a transverter LO, a dongle with separate tuner and ADC crystals. A |
|
leftover clock error makes the timing slip linearly across the frame, which |
|
moves each dechirped pilot peak linearly in frequency. Fit a line through |
|
the pilots' sub-bin peak positions, anchored at the preamble (where |
|
refine() zeroed the timing), weighted by pilot power. |
|
|
|
Returns (delta, sigma, span): the eps correction (eps_true = eps - delta), |
|
its standard error, and the frame span in chips it was measured over. |
|
""" |
|
pil, b, pv = info["pilot_slots"], info["pilot_bin"], info["pilot_val"] |
|
M = p.M |
|
m0 = p.n_preamble * M / 2 # preamble center |
|
if len(pil) < 2: |
|
return 0.0, np.inf, 0.0 |
|
m = np.concatenate([[m0], p.payload_start + pil * M + M / 2]) |
|
b = np.concatenate([[0.0], b]) |
|
w = np.abs(pv) ** 2 |
|
w = np.concatenate([[np.median(w)], w]) # anchor: one pilot's weight |
|
sw = np.sqrt(w) |
|
A = np.stack([np.ones_like(m), m - m0], axis=1) |
|
coef, *_ = np.linalg.lstsq(A * sw[:, None], b * sw, rcond=None) |
|
r = (b - A @ coef) * sw |
|
dof = max(len(m) - 2, 1) |
|
cov = np.linalg.pinv((A * w[:, None]).T @ A) * (r @ r) / dof |
|
# peak moves opposite to the clock error: delta = -slope |
|
return float(-coef[1]), float(np.sqrt(max(cov[1, 1], 0.0))), float(m[-1] - m0) |
|
|
|
|
|
DRIFT_MIN_CHIPS = 0.1 # skip corrections that move the frame end less than this |
|
# (frame._drift_correct also shrinks each estimate by d^2 / (d^2 + sigma^2), so |
|
# noise-level estimates barely move anything and confident ones apply in full) |
|
|
|
|
|
def apply_drift(sync, delta, p): |
|
"""Sync with eps corrected by delta, keeping the preamble timing fixed.""" |
|
m0 = p.n_preamble * p.M / 2 |
|
return Sync(T=sync.T, tau0=sync.tau0 - m0 * delta, f_bins=sync.f_bins, |
|
f_hz=sync.f_hz, eps=sync.eps - delta, metric=sync.metric) |
|
|
|
|
|
def channel_from_decisions(Y, is_p, bits, conf, p): |
|
"""Re-estimate the per-slot channel using decided data symbols as pilots. |
|
|
|
Y : (S, M) aligned dechirped spectra (demodulate_soft info['Y']) |
|
is_p : (S,) pilot mask |
|
bits : (n_data, sf+2) decided on-air bits per data symbol |
|
conf : (n_data,) bool, use this symbol (decision trusted) |
|
Each trusted symbol gives a channel sample Y[s, idx] * conj(a); with the |
|
pilots that is one sample per symbol instead of one per ``pilot_every``, |
|
which is what fast fading needs. Smoothing window chosen by |
|
leave-one-out, as for the pilots. |
|
""" |
|
idx, a = bits_to_symbols(np.asarray(bits, np.uint8).reshape(-1), p) |
|
ds = np.where(~is_p)[0] |
|
pil = np.where(is_p)[0] |
|
xs = np.concatenate([pil, ds[conf]]) |
|
ys = np.concatenate([Y[pil, 0], Y[ds[conf], idx[conf]] * np.conj(a[conf])]) |
|
o = np.argsort(xs) |
|
h, _ = _fit_channel(xs[o], ys[o], np.arange(len(is_p)), TRACK_WINDOWS + (32, 48)) |
|
return h |
|
|
|
|
|
def demap_extrinsic(yr, yi, sf, La, clip=50.0): |
|
"""Coherent demapper with a-priori bit LLRs (for iterative demap/decode). |
|
|
|
yr, yi : (S, M) scaled bin metrics from demodulate_soft (info['yr'/'yi']) |
|
La : (S, sf+2) a-priori LLRs in modulator bit order (log P0/P1) |
|
Returns extrinsic LLRs (S, sf+2): posterior minus the bit's own prior. |
|
|
|
Prior per hypothesis = sum_j (La_j / 2) * (+1 if b_j = 0 else -1); the QPSK |
|
priors fold into the per-axis log-cosh terms, the index priors form a |
|
per-bin offset pi_k = 0.5 * La_idx @ sign(k)^T. |
|
""" |
|
M = yr.shape[1] |
|
val = inv_gray(np.arange(M)) |
|
bits = ((val[:, None] >> np.arange(sf - 1, -1, -1)[None, :]) & 1) # (M, sf) |
|
sgn = 1.0 - 2.0 * bits |
|
pi = 0.5 * La[:, :sf] @ sgn.T # (S, M) |
|
ur = yr + 0.5 * La[:, sf:sf + 1] |
|
ui = yi + 0.5 * La[:, sf + 1:sf + 2] |
|
lr, li = _logcosh2(ur), _logcosh2(ui) |
|
mu = pi + lr + li |
|
out = np.empty_like(La) |
|
for j in range(sf): |
|
b0 = bits[:, j] == 0 |
|
out[:, j] = _lse(mu[:, b0], 1) - _lse(mu[:, ~b0], 1) - La[:, j] |
|
out[:, sf] = _lse(pi + ur + li, 1) - _lse(pi - ur + li, 1) - La[:, sf] |
|
out[:, sf + 1] = _lse(pi + ui + lr, 1) - _lse(pi - ui + lr, 1) - La[:, sf + 1] |
|
return np.clip(out, -clip, clip) |
|
|
|
|
|
def receive_soft(x, p, n_data, det_thresh=4.0, coherent=True): |
|
"""Sync + soft demap. Returns (llr or None, sync, info).""" |
|
r = decimate(x, p) |
|
sync = acquire(r, p, det_thresh) |
|
if sync is None: |
|
return None, None, None |
|
S = len(payload_layout(n_data, p.pilot_every)[0]) |
|
n_chips = p.payload_start + S * p.M |
|
if sync.T + n_chips + 8 > len(r): |
|
return None, sync, None |
|
sync = refine(x, sync, p) |
|
z = extract_frame(x, sync, p, n_chips) |
|
llr, info = demodulate_soft(z, p, n_data, coherent=coherent) |
|
return llr, sync, info |
|
|
|
|
|
def receive(x, p, n_bits, det_thresh=4.0): |
|
n_data = n_symbols_for_bits(n_bits, p) |
|
r = decimate(x, p) |
|
sync = acquire(r, p, det_thresh) |
|
if sync is None: |
|
return None, None, None |
|
S = len(payload_layout(n_data, p.pilot_every)[0]) |
|
n_chips = p.payload_start + S * p.M |
|
if sync.T + n_chips + 8 > len(r): |
|
return None, sync, None |
|
sync = refine(x, sync, p) |
|
z = extract_frame(x, sync, p, n_chips) |
|
bits, info = demodulate(z, p, n_data) |
|
return bits[:n_bits], sync, info
|
|
|