/** * End-to-end pipeline test without a microphone: three synthetic event * types (a low ringing tone, a high short tone, a noise burst) stand in * for recorded taps. Builds a snippet library, synthesizes datasets across * runs, executes each baseline algorithm through numbl, and prints scores. * The matched filter and the kNN sorter should sit far above the naive * floor; the run fails loudly if they do not. */ import { FS, type SynthConfig } from '../src/bench/types.ts'; import { synthesize } from '../src/bench/synthesize.ts'; import { score } from '../src/bench/score.ts'; import { buildTraining } from '../src/bench/training.ts'; import { runAlgorithm } from '../src/bench/execute.ts'; import { makeSyntheticLibrary } from './synthetic-lib.ts'; import matchedFilterSrc from '../src/algorithms/template_matched_filter.m?raw'; import onsetKnnSrc from '../src/algorithms/onset_knn.m?raw'; import naiveSrc from '../src/algorithms/naive_threshold.m?raw'; const lib = makeSyntheticLibrary(); const cfg: SynthConfig = { durationS: 30, eventsPerType: 30, gainMinDb: -12, overlap: 0.2, trainFraction: 0.3, seed: 1, }; const algos = [ { name: 'template_matched_filter', source: matchedFilterSrc }, { name: 'onset_knn', source: onsetKnnSrc }, { name: 'naive_threshold', source: naiveSrc }, ]; const NRUNS = 2; const TOL = 0.02; const acc: Record = {}; for (let r = 0; r < NRUNS; r++) { const ds = synthesize(lib, cfg, r); const training = buildTraining(lib, ds.trainIndices); console.log(`run ${r}: ${ds.truth.length} ground-truth events, ${ds.x.length} samples`); for (const a of algos) { const t0 = Date.now(); try { const out = runAlgorithm(a.source, ds.x, FS, training); const s = score(ds.truth, out.detections, lib.names.length, TOL); (acc[a.name] ??= []).push(s.accuracy); console.log( ` ${a.name.padEnd(26)} acc=${s.accuracy.toFixed(3)} correct=${s.correct}` + ` miss=${s.missed} fp=${s.falsePositives} misclass=${s.misclassified}` + ` (${Date.now() - t0} ms)`, ); } catch (err) { console.log(` ${a.name.padEnd(26)} ERROR: ${(err as Error).message}`); } } } const mean = (v: number[]) => v.reduce((a, b) => a + b, 0) / Math.max(v.length, 1); console.log('\nmean accuracy over runs:'); for (const a of algos) console.log(` ${a.name.padEnd(26)} ${mean(acc[a.name] ?? []).toFixed(3)}`); const mf = mean(acc['template_matched_filter'] ?? []); const knn = mean(acc['onset_knn'] ?? []); if (!(mf > 0.7)) { console.error('FAIL: matched filter accuracy too low'); process.exit(1); } if (!(knn > 0.5)) { console.error('FAIL: onset_knn accuracy too low'); process.exit(1); } console.log('PASS');