Files
zefieandClaude Opus 5.5 4bc15bb585 NOAA APT decoder and encoder (browser, no build step)
Decoder: streaming APT.Decoder (coherent PLL or envelope demod, sync A+B
tracking loop with coasting and timing-jump re-lock), telemetry analysis,
Q-gated impulse (FM click) repair. GUI with fast / realtime decode,
Listen, live input, telemetry table, markers.

Encoder: APT.Encoder with test impairments (noise, clicks, clock offset,
fading, line-timing jump); encoder dialog that feeds the decoder.

Tools: offline decode/encode, quality survey, stub-DOM GUI smoke test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 20:58:22 -05:00

749 lines
22 KiB
JavaScript

/*
* apt.js - NOAA APT (Automatic Picture Transmission) core: format constants,
* streaming decoder and telemetry analysis. No DOM use: runs in the browser
* (classic <script>, exposes window.APT) and in node (module.exports).
*
* Format (NOAA KLM User's Guide, sec. 4.2):
* 2400 Hz AM subcarrier, 4160 words/s, 2 lines/s, 2080 words/line.
* words 0.. 38 Sync A (4 low + 7 cycles of 1040 Hz)
* 39.. 85 Space A (minute markers every 60 lines)
* 86.. 994 Image A
* 995..1039 Telemetry A (16 wedges x 8 lines, 128-line frame)
* 1040..1078 Sync B (4 low + 7 pulses at 832 pps)
* 1079..1125 Space B
* 1126..2034 Image B
* 2035..2079 Telemetry B
*/
(function (root) {
'use strict';
const WORD_RATE = 4160;
const LINE_WORDS = 2080;
const CARRIER = 2400;
const OSR = 2; /* decoder samples per word */
const RATE = WORD_RATE * OSR; /* 8320 Hz envelope rate */
const LINE_LEN = LINE_WORDS * OSR; /* 4160 envelope samples per line */
const FRAME_LINES = 128;
const WEDGE_LINES = 8;
const REGIONS = [
{ name: 'Sync A', start: 0, len: 39 },
{ name: 'Space A', start: 39, len: 47 },
{ name: 'Image A', start: 86, len: 909 },
{ name: 'Telemetry A', start: 995, len: 45 },
{ name: 'Sync B', start: 1040, len: 39 },
{ name: 'Space B', start: 1079, len: 47 },
{ name: 'Image B', start: 1126, len: 909 },
{ name: 'Telemetry B', start: 2035, len: 45 },
];
/* AVHRR channel for the wedge (1..6) that wedge 16 matches */
const CHANNEL_NAMES = ['1', '2', '3A', '4', '5', '3B'];
const CHANNEL_DESC = {
'1': 'visible 0.6 um', '2': 'near IR 0.9 um', '3A': 'near IR 1.6 um',
'3B': 'IR 3.7 um', '4': 'thermal IR 10.8 um', '5': 'thermal IR 12 um',
};
function SyncAWords()
{
const w = [0, 0, 0, 0];
for (let i = 0; i < 7; i++)
w.push(1, 1, 0, 0);
while (w.length < 39)
w.push(0);
return w;
}
function SyncBWords()
{
const w = [0, 0, 0, 0];
for (let i = 0; i < 7; i++)
w.push(1, 1, 1, 0, 0);
return w;
}
function RegionAt(word)
{
for (const r of REGIONS)
if (word >= r.start && word < r.start + r.len)
return r;
return null;
}
/* Windowed-sinc (Blackman) lowpass, odd length, unity DC gain */
function LowpassTaps(fs, cutoff, ntaps)
{
ntaps |= 1;
const taps = new Float32Array(ntaps);
const m = (ntaps - 1) / 2;
const fc = cutoff / fs;
let sum = 0;
for (let i = 0; i < ntaps; i++) {
const n = i - m;
const sinc = n === 0 ? 2 * fc : Math.sin(2 * Math.PI * fc * n) / (Math.PI * n);
const win = 0.42 - 0.5 * Math.cos(2 * Math.PI * i / (ntaps - 1)) +
0.08 * Math.cos(4 * Math.PI * i / (ntaps - 1));
taps[i] = sinc * win;
sum += taps[i];
}
for (let i = 0; i < ntaps; i++)
taps[i] /= sum;
return taps;
}
/*
* Streaming decoder. Feed mono float samples at any rate >= ~9.6 kHz with
* process(); complete lines are delivered to onLine(line) as
* { index, words: Float32Array(2080) demodulated, quad: Float32Array(2080)
* quadrature (coherent demod only, else null), sync: 0..1 quality,
* locked, time: seconds into the input of the line start, hi, lo }
*/
class Decoder {
constructor(fs, opts)
{
opts = opts || {};
this.fs = fs;
this.onLine = opts.onLine || null;
/* I/Q mix to baseband, lowpass, magnitude */
this.phInc = 2 * Math.PI * CARRIER / fs;
this.ph = 0;
const ntaps = Math.max(31, Math.round(fs / 2400 * 14)) | 1;
this.taps = LowpassTaps(fs, 2150, ntaps);
this.ntaps = ntaps;
this.dI = new Float32Array(ntaps * 2);
this.dQ = new Float32Array(ntaps * 2);
this.di = 0;
this.groupDelay = (ntaps - 1) / 2;
/*
* Demodulation. 'coherent' (default): a slow PLL (2 Hz) tracks the
* carrier phase and the in-phase component is the pixel value.
* Linear, unbiased, and half the noise power of 'envelope' (|z|),
* which also lifts dark pixels at low SNR (Rician bias). The PLL
* averages ~0.1 s of carrier, so it holds where single pixels are
* buried in noise; the APT carrier never drops below ~13%.
*/
this.coherent = (opts.demod || 'coherent') === 'coherent';
const wn = 2 * Math.PI * 2 / fs;
this.pllKp = 2 * 0.707 * wn;
this.pllKi = wn * wn;
this.pllTh = 0;
this.pllF = 0;
this.pllAmp = 0;
this.pllA = 1 - Math.exp(-2 * Math.PI * 5 / fs);
/* linear resampler to RATE */
this.step = fs / RATE;
this.inPos = -1; /* absolute index of the current input sample */
this.prevMag = 0;
this.tNext = 0; /* input position of the next output sample */
/* envelope buffer */
this.buf = new Float32Array(LINE_LEN * 8);
this.qbuf = new Float32Array(LINE_LEN * 8); /* quadrature (coherent only) */
this.prevQ = 0;
this.bufLen = 0;
this.bufBase = 0; /* absolute envelope index of buf[0] */
/* sync */
const sa = SyncAWords();
this.tmplOn = [];
this.tmplOff = [];
const t = new Float32Array(sa.length * OSR);
for (let i = 0; i < t.length; i++)
t[i] = sa[(i / OSR) | 0] ? 1 : -1;
let mean = 0;
for (let i = 0; i < t.length; i++)
mean += t[i];
mean /= t.length;
let nrm = 0;
for (let i = 0; i < t.length; i++) {
t[i] -= mean;
nrm += t[i] * t[i];
}
this.tmpl = t;
this.tmplNorm = Math.sqrt(nrm);
/* sync B, 1040 words later: a second timing measurement per line */
const sbw = SyncBWords();
const tb = new Float32Array(sbw.length * OSR);
let mb = 0;
for (let i = 0; i < tb.length; i++)
mb += (tb[i] = sbw[(i / OSR) | 0] ? 1 : -1);
mb /= tb.length;
let nb = 0;
for (let i = 0; i < tb.length; i++) {
tb[i] -= mb;
nb += tb[i] * tb[i];
}
this.tmplB = tb;
this.tmplBNorm = Math.sqrt(nb);
this.offB = 1040 * OSR;
/* plateau samples for level estimates: middle of each 2-word run */
for (let c = 0; c < 7; c++) {
const on = (4 + 4 * c) * OSR, off = (6 + 4 * c) * OSR;
this.tmplOn.push(on + 1, on + 2);
this.tmplOff.push(off + 1, off + 2);
}
this.tracking = opts.tracking !== false;
this.coastLines = opts.coast || 120;
this.jumped = false;
this.trackLines = 0;
this.nextStart = LINE_LEN / 2; /* relative to buf[0] */
this.locked = false;
this.weak = 0;
this.lineLenEst = LINE_LEN;
this.lastStrongAbs = -1;
this.lastStrongLine = -1;
this.lineCount = 0;
this.lastSync = 0;
}
/* Input samples (Float32Array or Array), any length */
process(x)
{
const taps = this.taps, nt = this.ntaps;
const dI = this.dI, dQ = this.dQ;
const c0 = Math.cos(this.phInc), s0 = Math.sin(this.phInc);
let cr = Math.cos(this.ph), ci = Math.sin(this.ph);
let di = this.di;
for (let n = 0; n < x.length; n++) {
const v = x[n];
dI[di] = dI[di + nt] = v * cr;
dQ[di] = dQ[di + nt] = -v * ci;
di++;
if (di >= nt)
di = 0;
/* rotate oscillator */
const t = cr * c0 - ci * s0;
ci = cr * s0 + ci * c0;
cr = t;
let si = 0, sq = 0;
for (let k = 0; k < nt; k++) {
si += taps[k] * dI[di + k];
sq += taps[k] * dQ[di + k];
}
let mag, quad = 0;
if (this.coherent) {
const ct = Math.cos(this.pllTh), st = Math.sin(this.pllTh);
const a = si * ct + sq * st, b = sq * ct - si * st;
this.pllAmp += this.pllA * (Math.sqrt(si * si + sq * sq) - this.pllAmp);
const e = this.pllAmp > 1e-9 ? Math.max(-1, Math.min(1, b / this.pllAmp)) : 0;
this.pllF += this.pllKi * e;
this.pllTh += this.pllF + this.pllKp * e;
if (this.pllTh > Math.PI)
this.pllTh -= 2 * Math.PI;
else if (this.pllTh < -Math.PI)
this.pllTh += 2 * Math.PI;
mag = 2 * a;
quad = 2 * b;
} else {
mag = 2 * Math.sqrt(si * si + sq * sq);
}
this.inPos++;
while (this.tNext <= this.inPos) {
const f = this.tNext - (this.inPos - 1);
this._push(this.prevMag + (mag - this.prevMag) * f, this.prevQ + (quad - this.prevQ) * f);
this.tNext += this.step;
}
this.prevMag = mag;
this.prevQ = quad;
}
/* renormalize oscillator against drift */
const r = 1 / Math.sqrt(cr * cr + ci * ci);
this.ph = Math.atan2(ci * r, cr * r);
this.di = di;
this._scan();
}
/* Emit whatever is buffered as (possibly partial-sync) lines */
flush()
{
this._scan();
}
_push(v, q)
{
if (this.bufLen >= this.buf.length) {
const nb = new Float32Array(this.buf.length * 2);
nb.set(this.buf);
this.buf = nb;
const nq = new Float32Array(this.qbuf.length * 2);
nq.set(this.qbuf);
this.qbuf = nq;
}
this.qbuf[this.bufLen] = q;
this.buf[this.bufLen++] = v;
}
/* sync A and sync B together: the timing estimate for a line */
_ncorrAB(k)
{
return 0.5 * (this._ncorr(k) + this._ncorr(k + this.offB, this.tmplB, this.tmplBNorm));
}
_ncorr(k, tmpl, tnorm)
{
const b = this.buf, t = tmpl || this.tmpl, n = t.length;
let sum = 0, s1 = 0, s2 = 0;
for (let i = 0; i < n; i++) {
const v = b[k + i];
sum += t[i] * v;
s1 += v;
s2 += v * v;
}
const varn = s2 - s1 * s1 / n;
if (varn <= 1e-12)
return 0;
return sum / (Math.sqrt(varn) * (tnorm || this.tmplNorm));
}
_scan()
{
const L = LINE_LEN, T = this.tmpl.length;
for (;;) {
/* drop consumed samples */
const drop = Math.floor(this.nextStart - L);
if (drop > L * 2) {
this.buf.copyWithin(0, drop, this.bufLen);
this.qbuf.copyWithin(0, drop, this.bufLen);
this.bufLen -= drop;
this.bufBase += drop;
this.nextStart -= drop;
}
const S = this.locked ? 16 : L / 2;
const lo = Math.max(0, Math.floor(this.nextStart - S));
const hi = Math.floor(this.nextStart + S);
const need = this.locked ? Math.floor(this.nextStart) + L / 2 + L + T + 2 : hi + 2 * L + T + 2;
if (this.bufLen < need)
break;
let best = -2, bestK = Math.round(this.nextStart);
for (let k = lo; k <= hi; k++) {
let c;
if (this.locked)
c = this._ncorrAB(k);
else
c = 0.5 * (this._ncorr(k) + this._ncorr(k + L));
if (c > best) {
best = c;
bestK = k;
}
}
const thr = this.locked ? 0.3 : 0.5;
if (this.locked && best <= thr) {
/*
* Lost the expected sync: the satellite may have jumped
* its line timing. Look across the whole line for one
* clear sync and re-lock there at once.
*/
const wlo = Math.max(0, Math.floor(this.nextStart - L / 2));
let bestAB = -2, bestKAB = bestK;
for (let k = wlo; k < wlo + L; k++) {
const c = this._ncorr(k);
if (c > best) {
best = c;
bestK = k;
}
/* sync A + B: re-locks through noise after a jump */
if (c > 0.2) {
const cab = 0.5 * (c + this._ncorr(k + this.offB, this.tmplB, this.tmplBNorm));
if (cab > bestAB) {
bestAB = cab;
bestKAB = k;
}
}
}
if (best < 0.75 && bestAB >= 0.6) {
best = 0.75;
bestK = bestKAB;
}
if (best < 0.75)
best = Math.min(best, thr);
else {
this.lastStrongLine = -1;
this.jumped = true;
}
}
let start;
if (best > thr) {
/* parabolic sub-sample refinement */
const cf = this.locked && !this.jumped ? (k) => this._ncorrAB(k) : (k) => this._ncorr(k);
let frac = 0;
if (bestK > 0) {
const a = cf(bestK - 1), b = cf(bestK), c = cf(bestK + 1);
const d = a - 2 * b + c;
if (d < 0)
frac = Math.max(-0.5, Math.min(0.5, 0.5 * (a - c) / d));
}
start = bestK + frac;
/*
* Tracking: the satellite's line clock is very stable, so a
* small deviation from the prediction is mostly noise. Move
* a quarter of the way unless the measured position is
* clearly better (dropped samples, a timing jump), which is
* taken at once. Pixels are not touched, only line placement.
*/
let tracked = false;
if (this.locked && !this.jumped && this.tracking) {
const dev = start - this.nextStart;
const kp = Math.round(this.nextStart);
if (Math.abs(dev) < 4 || best - cf(kp) < 0.12) {
/* second-order loop: also steer the line length */
/*
* wide loop while acquiring, then narrow; weighted
* by sync confidence so noise peaks (0.3..0.4)
* barely move the timing or the line length
*/
const acq = this.trackLines < 24;
const wgt = Math.max(0, Math.min(1, (best - 0.35) / 0.3));
start = this.nextStart + (acq ? 0.6 : 0.25) * wgt * dev;
this.lineLenEst += (acq ? 0.15 : 0.03) * wgt * dev;
tracked = true;
}
}
/* sound cards are within a few hundred ppm */
this.lineLenEst = Math.max(LINE_LEN * 0.997, Math.min(LINE_LEN * 1.003, this.lineLenEst));
this.trackLines = tracked ? this.trackLines + 1 : 0;
this.jumped = false;
const abs = this.bufBase + start;
if (!tracked && this.lastStrongLine === this.lineCount - 1 && this.lastStrongAbs >= 0) {
const obs = abs - this.lastStrongAbs;
if (Math.abs(obs - this.lineLenEst) < 8 && best > 0.5)
this.lineLenEst += 0.05 * (obs - this.lineLenEst);
}
this.lastStrongAbs = abs;
this.lastStrongLine = this.lineCount;
this.locked = true;
this.weak = 0;
} else {
const wasLocked = this.locked;
start = wasLocked ? this.nextStart : Math.max(0, this.nextStart - L / 2);
/* coast on the predicted timing through fades: the line
* clock is stable, and the wide search above still
* catches a real jump */
if (++this.weak > this.coastLines)
this.locked = false;
this.lastSync = Math.max(0, best);
this._emit(start, this.lastSync);
/* unlocked: nextStart is the centre of a one-line search window */
this.nextStart = start + this.lineLenEst + (this.locked ? 0 : L / 2);
continue;
}
this.lastSync = Math.max(0, best);
this._emit(start, this.lastSync);
this.nextStart = start + this.lineLenEst;
}
}
_sample(p, b)
{
const i = Math.floor(p), f = p - i;
b = b || this.buf;
return b[i] + (b[i + 1] - b[i]) * f;
}
_emit(start, q)
{
const words = new Float32Array(LINE_WORDS);
for (let w = 0; w < LINE_WORDS; w++) {
const p = start + w * OSR;
words[w] = 0.5 * (this._sample(p) + this._sample(p + 1));
}
/* quadrature words: noise only (no image), used to spot clicks */
let quad = null;
if (this.coherent) {
quad = new Float32Array(LINE_WORDS);
const qb = this.qbuf;
for (let w = 0; w < LINE_WORDS; w++) {
const p = start + w * OSR;
quad[w] = 0.5 * (this._sample(p, qb) + this._sample(p + 1, qb));
}
}
let hi = 0, lo = 0;
for (const s of this.tmplOn)
hi += this._sample(start + s);
for (const s of this.tmplOff)
lo += this._sample(start + s);
hi /= this.tmplOn.length;
lo /= this.tmplOff.length;
const abs = this.bufBase + start;
const line = {
index: this.lineCount++,
words: words,
quad: quad,
sync: q,
locked: this.locked,
time: (abs * this.step - this.groupDelay) / this.fs,
hi: hi,
lo: lo,
};
if (this.onLine)
this.onLine(line);
}
/* Sample clock offset implied by the tracked line length, in ppm */
clockPpm()
{
return (this.lineLenEst / LINE_LEN - 1) * 1e6;
}
}
/*
* Telemetry: find the 128-line frames in a list of decoded lines and read
* the wedges. Returns { phase, frames: [{ start, a: [16], b: [16], chA,
* chB, quality }] }. Wedge 8 is white, wedge 9 is black (zero modulation),
* wedge 16 equals the wedge (1..6) of the channel being sent.
*/
function TelemetryColumns(lines)
{
const n = lines.length;
const ta = new Float32Array(n), tb = new Float32Array(n);
/* median across the wedge columns: clicks do not pull it */
const va = new Float32Array(33), vb = new Float32Array(33);
for (let i = 0; i < n; i++) {
const w = lines[i].words;
for (let k = 0; k < 33; k++) {
va[k] = w[1001 + k];
vb[k] = w[2041 + k];
}
va.sort();
vb.sort();
ta[i] = va[16];
tb[i] = vb[16];
}
return { ta, tb };
}
function WedgeMean(col, start, k)
{
const a = start + (k - 1) * WEDGE_LINES + 2, b = start + k * WEDGE_LINES - 2;
if (a < 0 || b > col.length)
return NaN;
let s = 0;
for (let i = a; i < b; i++)
s += col[i];
return s / (b - a);
}
/* Sum of the steps at the wedge 1..9 boundaries if a frame starts here */
function EdgeScore(col, start)
{
let e = 0;
for (let k = 0; k <= 9; k++) {
const i = start + k * WEDGE_LINES;
if (i < 1 || i >= col.length)
continue;
e += Math.abs(col[i] - col[i - 1]);
}
return e;
}
/* Correlation of wedges 1..9 against the 1/8..8/8, 0 ramp */
function RampScore(col, start)
{
const ref = [1, 2, 3, 4, 5, 6, 7, 8, 0];
const v = [];
for (let k = 1; k <= 9; k++) {
const m = WedgeMean(col, start, k);
if (isNaN(m))
return NaN;
v.push(m);
}
let mr = 0, mv = 0;
for (let i = 0; i < 9; i++) {
mr += ref[i];
mv += v[i];
}
mr /= 9;
mv /= 9;
let sxy = 0, sxx = 0, syy = 0;
for (let i = 0; i < 9; i++) {
sxy += (ref[i] - mr) * (v[i] - mv);
sxx += (ref[i] - mr) ** 2;
syy += (v[i] - mv) ** 2;
}
if (sxx === 0 || syy === 0)
return 0;
return sxy / Math.sqrt(sxx * syy);
}
function AnalyzeTelemetry(lines)
{
const out = { phase: -1, frames: [] };
if (lines.length < FRAME_LINES)
return out;
const { ta, tb } = TelemetryColumns(lines);
/*
* Frame starts: local maxima of the wedge 1..9 ramp score (both
* columns). Found per frame rather than with one global phase, since a
* line-timing jump on the satellite moves the frame phase too.
*/
const n = lines.length;
const score = new Float32Array(n).fill(-2);
for (let st = 0; st + 9 * WEDGE_LINES <= n; st++) {
const ra = RampScore(ta, st), rb = RampScore(tb, st);
if (!isNaN(ra) && !isNaN(rb))
score[st] = 0.5 * (ra + rb);
}
const starts = [];
for (let st = 0; st < n; st++) {
if (score[st] < 0.9)
continue;
let isMax = true;
for (let k = Math.max(0, st - 60); k < Math.min(n, st + 60) && isMax; k++)
if (score[k] > score[st] || (score[k] === score[st] && k < st))
isMax = false;
if (!isMax)
continue;
/* the ramp score is flat over a few lines: refine on wedge edges */
let bestSt = st, bestE = -1;
for (let k = Math.max(1, st - 4); k <= st + 4; k++) {
const e = EdgeScore(ta, k) + EdgeScore(tb, k);
if (e > bestE) {
bestE = e;
bestSt = k;
}
}
if (!starts.length || bestSt > starts[starts.length - 1].st)
starts.push({ st: bestSt, q: score[st] });
}
out.phase = starts.length ? starts[0].st % FRAME_LINES : -1;
for (const { st, q } of starts) {
const a = [], b = [];
for (let k = 1; k <= 16; k++) {
a.push(WedgeMean(ta, st, k));
b.push(WedgeMean(tb, st, k));
}
const complete = !isNaN(a[15]);
out.frames.push({
start: st,
a: a,
b: b,
chA: complete ? ChannelFromWedges(a) : '?',
chB: complete ? ChannelFromWedges(b) : '?',
quality: q,
complete: complete,
});
}
return out;
}
/*
* Impulse repair for FM threshold clicks. A switching filter, not a
* smoothing one: a word is replaced by the median of its 8 neighbours only
* when all of these hold:
* - it is off that median by more than K x the row's noise scale and
* KS x the spread of the neighbours themselves (edges, texture stay);
* - the quadrature channel spikes there (|Q| > KQ x its noise). The
* image is all in-phase, so real detail never shows in Q, while a
* click lands at a random carrier phase and almost always does.
* Everything else is copied unchanged; with no Q (envelope demod) nothing
* is repaired. line/prev/next are decoded lines; prev/next may be null.
* Returns { words, mask, count }.
*/
const IMG_A = 86, IMG_B = 1126, IMG_W = 909;
const med8 = new Float32Array(8), dev8 = new Float32Array(8);
function Median8(a)
{
for (let i = 1; i < 8; i++) {
const v = a[i];
let j = i - 1;
while (j >= 0 && a[j] > v) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = v;
}
return 0.5 * (a[3] + a[4]);
}
function RobustSigma(v, off, len)
{
const a = new Float32Array(Math.ceil(len / 2));
let n = 0;
for (let x = 0; x < len; x += 2)
a[n++] = Math.abs(v[off + x]);
a.sort();
return 1.4826 * a[n >> 1] + 1e-9;
}
function RepairRow(line, prev, next, opts)
{
opts = opts || {};
const cur = line.words, q = line.quad;
const out = new Float32Array(cur);
const mask = new Uint8Array(LINE_WORDS);
if (!prev || !next || !q)
return { words: out, mask, count: 0 };
const K = opts.k || 4, KS = opts.ks || 3, KQ = opts.kq || 3;
const p = prev.words, n = next.words;
/* noise scales never below 1.5% of the sync contrast: on a clean
* signal Q holds only filter leakage at sharp edges */
const floor = 0.015 * Math.max(1e-6, line.hi - line.lo);
let count = 0;
const dev = new Float32Array(IMG_W), med = new Float32Array(IMG_W), spr = new Float32Array(IMG_W);
for (const off of [IMG_A, IMG_B]) {
const sq = Math.max(floor, RobustSigma(q, off, IMG_W));
for (let x = 1; x < IMG_W - 1; x++) {
const k = off + x;
med8[0] = p[k - 1]; med8[1] = p[k]; med8[2] = p[k + 1];
med8[3] = cur[k - 1]; med8[4] = cur[k + 1];
med8[5] = n[k - 1]; med8[6] = n[k]; med8[7] = n[k + 1];
const m = Median8(med8);
for (let i = 0; i < 8; i++)
dev8[i] = Math.abs(med8[i] - m);
spr[x] = 1.4826 * Median8(dev8);
med[x] = m;
dev[x] = cur[k] - m;
}
const sd = Math.max(floor, RobustSigma(dev, 1, IMG_W - 2));
for (let x = 1; x < IMG_W - 1; x++) {
const k = off + x, a = Math.abs(dev[x]);
if (a <= K * sd || a <= KS * spr[x])
continue;
const qa = Math.max(Math.abs(q[k - 1]), Math.abs(q[k]), Math.abs(q[k + 1]));
if (qa <= KQ * sq)
continue;
out[k] = med[x];
mask[k] = 1;
count++;
}
}
return { words: out, mask, count };
}
function ChannelFromWedges(w)
{
let best = -1, bd = 1e9;
for (let k = 0; k < 6; k++) {
const d = Math.abs(w[15] - w[k]);
if (d < bd) {
bd = d;
best = k;
}
}
return best < 0 ? '?' : CHANNEL_NAMES[best];
}
const APT = {
WORD_RATE, LINE_WORDS, CARRIER, OSR, RATE, LINE_LEN, FRAME_LINES, WEDGE_LINES,
REGIONS, CHANNEL_NAMES, CHANNEL_DESC,
SyncAWords, SyncBWords, RegionAt, LowpassTaps,
Decoder, AnalyzeTelemetry, TelemetryColumns, RepairRow,
};
if (typeof module !== 'undefined' && module.exports)
module.exports = APT;
else
root.APT = APT;
})(typeof window !== 'undefined' ? window : globalThis);