Files
noaa-apt/tools/encode.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

80 lines
2.5 KiB
JavaScript

/*
* node tools/encode.js out.wav [options] - test-pattern / image encoder
* --rate 11025 output sample rate
* --lines 600 lines for the test pattern
* --pgm in.pgm 2080-wide P5 image: re-encode every word as is
* (any other width: used as both channel images, scaled to 909)
* --snr 20 add noise, dB
* --ppm 50 sample clock offset
* --jump 300 line-timing jump at line 300
* --chA 2 --chB 4 channel IDs
*/
'use strict';
const fs = require('fs');
const path = require('path');
const APT = require(path.join(__dirname, '..', 'encoder.js'));
const Wav = require(path.join(__dirname, '..', 'wav.js'));
const args = process.argv.slice(2);
const out = args.shift();
const opt = {};
for (let i = 0; i < args.length; i += 2)
opt[args[i].replace(/^--/, '')] = args[i + 1];
function ReadPGM(file)
{
const b = fs.readFileSync(file);
const hdr = b.toString('latin1', 0, 64).match(/^P5\s+(\d+)\s+(\d+)\s+(\d+)\s/);
if (!hdr)
throw new Error('need a binary P5 PGM');
return { w: +hdr[1], h: +hdr[2], data: b.subarray(hdr[0].length) };
}
/* gradient + grid test pattern, different in A and B */
function Pattern(i, x, ch)
{
if (ch === 0)
return ((x >> 5) + (i >> 5)) & 1 ? Math.round(x / 908 * 255) : 255 - Math.round(x / 908 * 255);
return Math.round(127.5 + 127.5 * Math.sin(x / 40 + i / 25) * Math.cos(i / 60));
}
const lineOpts = { chA: opt.chA || '2', chB: opt.chB || '4' };
let source;
if (opt.pgm) {
const img = ReadPGM(opt.pgm);
if (img.w === 2080) {
source = (i) => i < img.h ? img.data.subarray(i * 2080, (i + 1) * 2080) : null;
} else {
const row = new Uint8Array(909);
source = (i) => {
if (i >= img.h)
return null;
for (let x = 0; x < 909; x++)
row[x] = img.data[i * img.w + Math.min(img.w - 1, Math.floor(x * img.w / 909))];
return APT.BuildLine(i, row, row, lineOpts);
};
}
} else {
const n = +(opt.lines || 600);
const a = new Uint8Array(909), b = new Uint8Array(909);
source = (i) => {
if (i >= n)
return null;
for (let x = 0; x < 909; x++) {
a[x] = Pattern(i, x, 0);
b[x] = Pattern(i, x, 1);
}
return APT.BuildLine(i, a, b, lineOpts);
};
}
const rate = +(opt.rate || 11025);
const sig = APT.EncodeAll(rate, source, {
snr: opt.snr !== undefined ? +opt.snr : undefined,
ppm: +(opt.ppm || 0),
jumpAt: opt.jump !== undefined ? +opt.jump : undefined,
seed: +(opt.seed || 1),
});
fs.writeFileSync(out, Buffer.from(Wav.Build(sig, rate)));
console.log(`${out}: ${(sig.length / rate).toFixed(1)} s at ${rate} Hz`);