/* * encoder.js - NOAA APT encoder. Loads after apt.js and adds APT.Encoder, * APT.BuildLine and APT.TelemetryWedges. No DOM use; also loads in node. * * Signal (NOAA KLM User's Guide, sec. 4.2): each 8-bit word sets the * amplitude of a 2400 Hz carrier, 255 = full scale, 0 = 13% (87% modulation). * The carrier is phase locked to the word clock: 1200 cycles per line. * The word stream is band-limited before modulation, like the D/A filter on * the satellite, so the output fits an 11025 Hz recording. * * Optional impairments, for testing decoders: additive noise at a given SNR, * a sample-clock offset, slow fading, and a line-timing jump (the satellite * restarting its line half a line late, as NOAA-15 did in July 2019). */ (function (root) { 'use strict'; const APT = root.APT || (typeof require !== 'undefined' ? require('./apt.js') : null); if (!APT) throw new Error('encoder.js needs apt.js'); const W = APT.LINE_WORDS; const SYNC_HI = 244, SYNC_LO = 11; const MOD_DEPTH = 0.87; /* Channel IDs: wedge number (1..6) that wedge 16 repeats */ const CHANNEL_WEDGE = { '1': 1, '2': 2, '3A': 3, '4': 4, '5': 5, '3B': 6 }; /* * Values of the 16 telemetry wedges for one channel. Wedges 1-8 are the * 1/8..8/8 ramp, 9 is zero modulation, 10-13 the black-body PRTs, 14 the * patch temperature, 15 the back scan, 16 the channel ID. */ function TelemetryWedges(channel, opts) { opts = opts || {}; const w = []; for (let k = 1; k <= 8; k++) w.push(Math.round(255 * k / 8)); w.push(0); const prt = opts.prt || [105, 106, 104, 105]; w.push(...prt); w.push(opts.patch !== undefined ? opts.patch : 98); const ir = channel === '3B' || channel === '4' || channel === '5'; w.push(opts.backScan !== undefined ? opts.backScan : (ir ? 120 : 10)); const id = CHANNEL_WEDGE[channel] || 1; w.push(w[id - 1]); return w; } /* * One 2080-word line. * index line number (sets the telemetry wedge and minute markers) * a, b Uint8Array/Array(909) image pixels, or null for black * chA, chB channel IDs ('1','2','3A','3B','4','5') for wedge 16 * spaceA/B space-view word (default: black for visible, white for IR) */ function BuildLine(index, a, b, opts, out) { opts = opts || {}; const words = out || new Uint8Array(W); const sa = APT.SyncAWords(), sb = APT.SyncBWords(); const chA = opts.chA || '2', chB = opts.chB || '4'; const tA = opts.telA || TelemetryWedges(chA, opts); const tB = opts.telB || TelemetryWedges(chB, opts); const isIR = (c) => c === '3B' || c === '4' || c === '5'; const spA = opts.spaceA !== undefined ? opts.spaceA : (isIR(chA) ? 255 : 0); const spB = opts.spaceB !== undefined ? opts.spaceB : (isIR(chB) ? 255 : 0); const frame = (index + (opts.framePhase || 0)) % APT.FRAME_LINES; const wedge = Math.floor((frame < 0 ? frame + APT.FRAME_LINES : frame) / APT.WEDGE_LINES); /* minute markers: every 120 lines, two black then two white */ const mm = index % 120; const marker = mm < 2 ? 0 : mm < 4 ? 255 : -1; for (let i = 0; i < 39; i++) { words[i] = sa[i] ? SYNC_HI : SYNC_LO; words[1040 + i] = sb[i] ? SYNC_HI : SYNC_LO; } words.fill(marker >= 0 ? marker : spA, 39, 86); words.fill(marker >= 0 ? marker : spB, 1079, 1126); for (let i = 0; i < 909; i++) { words[86 + i] = a ? a[i] : 0; words[1126 + i] = b ? b[i] : 0; } words.fill(tA[wedge], 995, 1040); words.fill(tB[wedge], 2035, 2080); return words; } /* * Streaming modulator. source(lineIndex) returns a 2080-word line (any * array of 0..255) or null when there are no more lines. render(n) returns * the next n samples (Float32Array, peak ~0.9) and fewer at the end. * * opts: snr (dB, carrier power over noise in the 0..fs/2 band; omit for * none), ppm (sample clock offset), fade (depth 0..1), fadePeriod (s), * jumpAt (line at which the satellite restarts its line timing), * jumpWords (how far, default 1040), clicks (FM threshold clicks per * second), seed, gain. */ class Encoder { constructor(fs, source, opts) { opts = opts || {}; this.fs = fs; this.source = source; this.opts = opts; this.gain = opts.gain !== undefined ? opts.gain : 0.9; /* word clock, in words per output sample */ this.wordStep = APT.WORD_RATE / fs * (1 + (opts.ppm || 0) * 1e-6); this.wordPos = 0; /* fractional word position in the stream */ this.carrierInc = 2 * Math.PI * APT.CARRIER / APT.WORD_RATE; /* per word */ this.line = null; this.lineIndex = -1; /* satellite line counter */ this.wordInLine = W; /* force a fetch */ this.done = false; this.samples = 0; /* band-limit the word stream: FIR over a word-rate ZOH, applied at * 8 sub-samples per word, then sampled at fs by linear interpolation */ this.sub = 8; this.taps = APT.LowpassTaps(APT.WORD_RATE * this.sub, opts.bandwidth || 2400, 8 * this.sub + 1); this.hist = new Float32Array(this.taps.length * 2); this.hi = 0; this.subPos = 0; /* sub-samples generated */ this.env0 = 0; this.env1 = 0; this.tail = 0; /* start ahead by the filter delay so line 0 begins at t = 0 */ this.wordPos = (this.taps.length - 1) / 2 / this.sub; this.wordPos0 = this.wordPos; this.noise = 0; if (opts.snr !== undefined && opts.snr !== null && isFinite(opts.snr)) { /* mean carrier power of an average APT line ~ (0.6 * gain)^2 / 2 */ const pc = (0.6 * this.gain) ** 2 / 2; this.noise = Math.sqrt(pc / Math.pow(10, opts.snr / 10)); } this.rng = Mulberry32(opts.seed || 1); this.clickRate = (opts.clicks || 0) / fs; /* clicks per second */ this.clickW = 0.06e-3 * fs; /* pulse sigma, samples */ this.clickHalf = Math.ceil(3 * this.clickW); this.clicks = []; this.spare = null; } /* next word of the stream, or -1 once the source has ended */ _word() { const o = this.opts; const cut = o.jumpAt !== undefined && this.lineIndex === o.jumpAt ? W - (o.jumpWords !== undefined ? o.jumpWords : 1040) : W; if (this.wordInLine >= cut) { /* at jumpAt the satellite drops the rest of the line and * starts the next one early */ this.lineIndex++; this.line = this.done ? null : this.source(this.lineIndex); if (!this.line) this.done = true; this.wordInLine = 0; } if (this.done) return -1; return this.line[this.wordInLine++]; } /* next band-limited sub-sample of the amplitude envelope */ _subSample() { if (this.subPos % this.sub === 0) this.curWord = this.done ? -1 : this._word(); this.subPos++; if (this.curWord < 0) this.tail++; const amp = this.curWord < 0 ? 0 : 1 - MOD_DEPTH * (1 - this.curWord / 255); const n = this.taps.length; this.hist[this.hi] = this.hist[this.hi + n] = amp; this.hi = (this.hi + 1) % n; let s = 0; for (let k = 0; k < n; k++) s += this.taps[k] * this.hist[this.hi + k]; return s; } render(n) { const out = new Float32Array(n); const o = this.opts; const fade = o.fade || 0, fadeP = o.fadePeriod || 7; let i = 0; for (; i < n; i++) { /* advance the envelope to wordPos */ const target = this.wordPos * this.sub; while (this.subPos <= target + 1) { this.env0 = this.env1; this.env1 = this._subSample(); } if (this.tail > this.taps.length) break; const f = target - (this.subPos - 2); const env = this.env0 + (this.env1 - this.env0) * Math.max(0, Math.min(1, f)); /* carrier locked to the word clock */ let v = env * Math.sin(this.carrierInc * (this.wordPos - this.wordPos0)); if (fade) v *= 1 - fade * 0.5 * (1 - Math.cos(2 * Math.PI * this.samples / this.fs / fadeP)); v *= this.gain; if (this.noise) v += this.noise * this._gauss(); if (this.clickRate) { /* FM threshold clicks: short pulses of random sign */ if (this.rng() < this.clickRate) this.clicks.push({ t: 0, a: (this.rng() < 0.5 ? -1 : 1) * (0.3 + this.rng()) * this.gain }); for (let k = this.clicks.length - 1; k >= 0; k--) { const c = this.clicks[k], u = (c.t - this.clickHalf) / this.clickW; v += c.a * Math.exp(-0.5 * u * u); if (++c.t > 2 * this.clickHalf) this.clicks.splice(k, 1); } } out[i] = v; this.wordPos += this.wordStep; this.samples++; } return i < n ? out.subarray(0, i) : out; } _gauss() { if (this.spare !== null) { const s = this.spare; this.spare = null; return s; } let u, v, r; do { u = 2 * this.rng() - 1; v = 2 * this.rng() - 1; r = u * u + v * v; } while (r >= 1 || r === 0); const m = Math.sqrt(-2 * Math.log(r) / r); this.spare = v * m; return u * m; } /* seconds of signal emitted so far */ time() { return this.samples / this.fs; } } function Mulberry32(a) { return function () { a |= 0; a = (a + 0x6D2B79F5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } /* Render a whole source to one Float32Array */ function EncodeAll(fs, source, opts) { const enc = new Encoder(fs, source, opts); const parts = []; let total = 0; for (;;) { const p = enc.render(fs); parts.push(p); total += p.length; if (p.length < fs) break; } const out = new Float32Array(total); let o = 0; for (const p of parts) { out.set(p, o); o += p.length; } return out; } Object.assign(APT, { SYNC_HI, SYNC_LO, MOD_DEPTH, CHANNEL_WEDGE, TelemetryWedges, BuildLine, Encoder, EncodeAll, }); if (typeof module !== 'undefined' && module.exports) module.exports = APT; })(typeof window !== 'undefined' ? window : globalThis);