/** * 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 silenceGapKnnSrc from '../src/algorithms/silence_gap_knn.m?raw'; import overlapPursuitSrc from '../src/algorithms/overlap_pursuit.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, coincidence: 0, coincidenceMs: 5, trainFraction: 0.3, seed: 1, }; const algos = [ { name: 'template_matched_filter', source: matchedFilterSrc }, { name: 'onset_knn', source: onsetKnnSrc }, { name: 'silence_gap_knn', source: silenceGapKnnSrc }, { name: 'overlap_pursuit', source: overlapPursuitSrc }, { 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)}`); // The coincidence knob must actually place events near-simultaneously: // with every overlapper aimed at its host's onset, most consecutive // ground-truth pairs closer than a snippet length should be inside the // coincidence window, where they are outside it when the knob is off. function nearPairs(coincidence: number, windowMs: number): { close: number; overlapping: number } { const ds = synthesize(lib, { ...cfg, overlap: 0.5, coincidence, coincidenceMs: windowMs }, 0); let close = 0, overlapping = 0; for (let i = 1; i < ds.truth.length; i++) { const dtMs = (ds.truth[i].t - ds.truth[i - 1].t) * 1000; if (dtMs < 200) overlapping++; if (dtMs <= windowMs) close++; } return { close, overlapping }; } const off = nearPairs(0, 5); const on = nearPairs(1, 5); console.log(`\ncoincidence off: ${off.close}/${off.overlapping} overlapping pairs within 5 ms`); console.log(`coincidence on: ${on.close}/${on.overlapping} overlapping pairs within 5 ms`); if (!(on.close > 0.6 * on.overlapping)) { console.error('FAIL: coincidence knob did not place events near-simultaneously'); process.exit(1); } if (!(off.close < 0.35 * off.overlapping)) { console.error('FAIL: events cluster within the coincidence window with the knob off'); process.exit(1); } 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');