/ magland / rip-sound-analyzer
Sign in
magland / rip-sound-analyzer
rip-sound-analyzer / scripts / check-analysis.ts
73 lines · 3.2 KBCodeBlameHistory
d0a8b32Rip Sound Analyzer: record tears, inspect one, compare across speedsJeremy Magland 1// Sanity checks for the analysis, run with `npm test`.
8bf83cfMeasure energy and find the tear in a 15–20 kHz bandJeremy Magland 2import { analyze, detectSegment, DEFAULT_SETTINGS, energyBand, bandLevels } from "../src/analysis/analyze.ts";
d0a8b32Rip Sound Analyzer: record tears, inspect one, compare across speedsJeremy Magland 3import { syntheticTear } from "../src/audio/synthetic.ts";
4import { decodeWav, encodeWavFloat32 } from "../src/audio/wav.ts";
6let failed = 0;
7function check(name: string, ok: boolean, detail = "") {
8 console.log(`${ok ? "ok " : "FAIL"} ${name}${detail ? " " + detail : ""}`);
9 if (!ok) failed++;
12const fs = 48000;
14// Parseval: white noise of known power, full band
16 const x = new Float32Array(fs * 2);
17 let s = 7;
18 for (let i = 0; i < x.length; i++) {
19 s = (s * 1103515245 + 12345) & 0x7fffffff;
20 x[i] = (s / 0x7fffffff - 0.5) * 0.2;
21 }
22 let e = 0;
23 for (const v of x) e += v * v;
24 e /= fs;
8bf83cfMeasure energy and find the tear in a 15–20 kHz bandJeremy Magland 25 const a = analyze(x, fs, { start: 0, end: 2 }, { ...DEFAULT_SETTINGS, energyLow: 0, energyHigh: fs / 2 });
d0a8b32Rip Sound Analyzer: record tears, inspect one, compare across speedsJeremy Magland 26 check("Parseval (energy of white noise)", Math.abs(a.energyRaw / e - 1) < 0.02, `${a.energyRaw.toExponential(3)} vs ${e.toExponential(3)}`);
8bf83cfMeasure energy and find the tear in a 15–20 kHz bandJeremy Magland 29// Band levels: white noise in 15–20 kHz carries 5/24 of the power
31 const x = new Float32Array(fs);
32 let s = 11;
33 for (let i = 0; i < x.length; i++) {
34 s = (s * 1103515245 + 12345) & 0x7fffffff;
35 x[i] = (s / 0x7fffffff - 0.5) * 0.2;
36 }
37 let p = 0;
38 for (const v of x) p += v * v;
39 p /= x.length;
40 const lv = bandLevels(x, fs, { lo: 15000, hi: 20000 });
41 const mid = lv.power.slice(10, -10);
42 const mean = mid.reduce((a, v) => a + v, 0) / mid.length;
43 check("band level of white noise", Math.abs(mean / (p * 5 / 24) - 1) < 0.05, `${(mean / p).toFixed(4)} vs ${(5 / 24).toFixed(4)}`);
d0a8b32Rip Sound Analyzer: record tears, inspect one, compare across speedsJeremy Magland 46// Synthetic tears: same clicks at different durations
47const results: { T: number; energyDb: number; median: number; seg: [number, number] }[] = [];
48for (const T of [0.3, 1, 3, 10]) {
49 const x = syntheticTear({ tearSeconds: T, seed: 3 });
8bf83cfMeasure energy and find the tear in a 15–20 kHz bandJeremy Magland 50 const seg = detectSegment(x, fs, energyBand(DEFAULT_SETTINGS));
d0a8b32Rip Sound Analyzer: record tears, inspect one, compare across speedsJeremy Magland 51 check(`detect segment T=${T}`, !!seg && Math.abs(seg.start - 1) < 0.08 && Math.abs(seg.end - (1 + T)) < 0.08, seg ? `${seg.start.toFixed(3)}–${seg.end.toFixed(3)}` : "none");
52 if (!seg) continue;
53 const a = analyze(x, fs, seg, DEFAULT_SETTINGS);
54 results.push({ T, energyDb: 10 * Math.log10(a.energy), median: a.medianFreq, seg: [seg.start, seg.end] });
55 console.log(` T=${T}s energy=${(10 * Math.log10(a.energy)).toFixed(2)} dB snr=${a.snrDb.toFixed(1)} dB median=${a.medianFreq.toFixed(0)} Hz centroid=${a.centroid.toFixed(0)} Hz peak=${a.peakDbfs.toFixed(1)} dBFS`);
57const es = results.map((r) => r.energyDb);
58check("synthetic energy independent of duration (range < 1 dB)", Math.max(...es) - Math.min(...es) < 1);
59const ms = results.map((r) => r.median);
60check("synthetic median frequency independent of duration (range < 10%)", Math.max(...ms) / Math.min(...ms) < 1.1);
62// WAV round trip
64 const x = syntheticTear({ tearSeconds: 0.5 });
65 const blob = encodeWavFloat32(x, fs);
66 const d = decodeWav(await blob.arrayBuffer());
67 check("WAV float32 round trip", !!d && d.sampleRate === fs && d.samples.length === x.length && d.samples.every((v, i) => v === x[i]));
70if (failed) {
71 console.log(`${failed} check(s) failed`);
72 throw new Error("checks failed");
↑↓ move↵openescclose