21abc12audio-spike-sorting: benchmark framework for audio event detection and classificationJeremy Magland 1/**
2 * End-to-end pipeline test without a microphone: three synthetic event
3 * types (a low ringing tone, a high short tone, a noise burst) stand in
4 * for recorded taps. Builds a snippet library, synthesizes datasets across
5 * runs, executes each baseline algorithm through numbl, and prints scores.
6 * The matched filter and the kNN sorter should sit far above the naive
7 * floor; the run fails loudly if they do not.
8 */
16328b9Download runs as a .zip for offline development: WAVs, ground truth, training snippets, algorithm sources, and a numbl evaluation harness that reproduces the browser scoresJeremy Magland 9import { FS, type SynthConfig } from '../src/bench/types.ts';
21abc12audio-spike-sorting: benchmark framework for audio event detection and classificationJeremy Magland 10import { synthesize } from '../src/bench/synthesize.ts';
11import { score } from '../src/bench/score.ts';
12import { buildTraining } from '../src/bench/training.ts';
13import { runAlgorithm } from '../src/bench/execute.ts';
16328b9Download runs as a .zip for offline development: WAVs, ground truth, training snippets, algorithm sources, and a numbl evaluation harness that reproduces the browser scoresJeremy Magland 14import { makeSyntheticLibrary } from './synthetic-lib.ts';
21abc12audio-spike-sorting: benchmark framework for audio event detection and classificationJeremy Magland 15import matchedFilterSrc from '../src/algorithms/template_matched_filter.m?raw';
16import onsetKnnSrc from '../src/algorithms/onset_knn.m?raw';
17import naiveSrc from '../src/algorithms/naive_threshold.m?raw';
16328b9Download runs as a .zip for offline development: WAVs, ground truth, training snippets, algorithm sources, and a numbl evaluation harness that reproduces the browser scoresJeremy Magland 19const lib = makeSyntheticLibrary();
21abc12audio-spike-sorting: benchmark framework for audio event detection and classificationJeremy Magland 20
21const cfg: SynthConfig = {
22 durationS: 30,
23 eventsPerType: 30,
24 gainMinDb: -12,
25 overlap: 0.2,
26 trainFraction: 0.3,
27 seed: 1,
28};
30const algos = [
31 { name: 'template_matched_filter', source: matchedFilterSrc },
32 { name: 'onset_knn', source: onsetKnnSrc },
33 { name: 'naive_threshold', source: naiveSrc },
34];
36const NRUNS = 2;
37const TOL = 0.02;
38const acc: Record<string, number[]> = {};
39for (let r = 0; r < NRUNS; r++) {
40 const ds = synthesize(lib, cfg, r);
41 const training = buildTraining(lib, ds.trainIndices);
42 console.log(`run ${r}: ${ds.truth.length} ground-truth events, ${ds.x.length} samples`);
43 for (const a of algos) {
44 const t0 = Date.now();
45 try {
46 const out = runAlgorithm(a.source, ds.x, FS, training);
47 const s = score(ds.truth, out.detections, lib.names.length, TOL);
48 (acc[a.name] ??= []).push(s.accuracy);
49 console.log(
50 ` ${a.name.padEnd(26)} acc=${s.accuracy.toFixed(3)} correct=${s.correct}` +
51 ` miss=${s.missed} fp=${s.falsePositives} misclass=${s.misclassified}` +
52 ` (${Date.now() - t0} ms)`,
53 );
54 } catch (err) {
55 console.log(` ${a.name.padEnd(26)} ERROR: ${(err as Error).message}`);
56 }
57 }
58}
60const mean = (v: number[]) => v.reduce((a, b) => a + b, 0) / Math.max(v.length, 1);
61console.log('\nmean accuracy over runs:');
62for (const a of algos) console.log(` ${a.name.padEnd(26)} ${mean(acc[a.name] ?? []).toFixed(3)}`);
64const mf = mean(acc['template_matched_filter'] ?? []);
65const knn = mean(acc['onset_knn'] ?? []);
66if (!(mf > 0.7)) { console.error('FAIL: matched filter accuracy too low'); process.exit(1); }
67if (!(knn > 0.5)) { console.error('FAIL: onset_knn accuracy too low'); process.exit(1); }
68console.log('PASS');