Files
noaa-apt/tools/decode.js
T
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

62 lines
2.4 KiB
JavaScript

/*
* node tools/decode.js in.wav out.pgm [lines.txt] [--envelope] [--repair[=strong]]
* offline decode; prints telemetry frames and, with --repair, repaired pixels
*/
'use strict';
const fs = require('fs');
const path = require('path');
const APT = require(path.join(__dirname, '..', 'apt.js'));
const Wav = require(path.join(__dirname, '..', 'wav.js'));
const flags = process.argv.filter((a) => a.startsWith('--'));
process.argv = process.argv.filter((a) => !a.startsWith('--'));
const repair = flags.find((f) => f.startsWith('--repair'));
const w = Wav.Parse(fs.readFileSync(process.argv[2]));
const lines = [];
const t0 = Date.now();
const dec = new APT.Decoder(w.rate, { onLine: (l) => lines.push(l), demod: flags.includes('--envelope') ? 'envelope' : 'coherent' });
const CH = 4096;
for (let i = 0; i < w.data.length; i += CH)
dec.process(w.data.subarray(i, i + CH));
dec.flush();
console.log(`rate ${w.rate} lines ${lines.length} in ${Date.now() - t0} ms, clock ${dec.clockPpm().toFixed(1)} ppm`);
if (repair) {
const opts = repair === '--repair=strong' ? { k: 3.5, kq: 2.5, ks: 2.5 } : {};
const ok = (l) => (l && l.locked ? l : null);
const fixed = lines.map((l, i) => APT.RepairRow(l, ok(lines[i - 1]), ok(lines[i + 1]), opts));
let n = 0;
fixed.forEach((r, i) => {
if (lines[i].locked) {
n += r.count;
lines[i] = Object.assign({}, lines[i], { words: r.words });
}
});
console.log(`repaired ${n} pixels (${(100 * n / (lines.length * 1818)).toFixed(3)}%)`);
}
/* contrast from image areas of locked lines */
const vals = [];
for (const l of lines)
if (l.sync > 0.4)
for (let k = 86; k < 995; k += 7)
vals.push(l.words[k], l.words[k + 1040]);
vals.sort((a, b) => a - b);
const lo = vals[Math.floor(vals.length * 0.005)], hi = vals[Math.floor(vals.length * 0.995)];
const out = Buffer.alloc(lines.length * 2080);
lines.forEach((l, i) => {
for (let k = 0; k < 2080; k++)
out[i * 2080 + k] = Math.max(0, Math.min(255, Math.round((l.words[k] - lo) / (hi - lo) * 255)));
});
fs.writeFileSync(process.argv[3], Buffer.concat([Buffer.from(`P5\n2080 ${lines.length}\n255\n`), out]));
if (process.argv[4]) {
let s = '';
for (const l of lines)
s += `${l.index} ${l.time.toFixed(4)} ${l.sync.toFixed(3)} ${l.locked ? 1 : 0}\n`;
fs.writeFileSync(process.argv[4], s);
}
const tel = APT.AnalyzeTelemetry(lines);
console.log('telemetry phase', tel.phase);
for (const f of tel.frames)
console.log(`frame @${f.start} q=${f.quality.toFixed(2)} A=ch${f.chA} B=ch${f.chB}`);