// Synthetic "tears" built exactly as the hypothesis says: the same set of clicks // (same shape, same amplitude distribution, same count) placed at random times over // the tear duration. Useful for checking that the analysis behaves as expected: // energy and spectrum should not depend on duration. function mulberry32(seed: number) { return () => { seed |= 0; seed = (seed + 0x6d2b79f5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } function gauss(rand: () => number) { const u = Math.max(rand(), 1e-12); return Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * rand()); } export interface SyntheticOptions { tearSeconds: number; clicks?: number; sampleRate?: number; quietSeconds?: number; // before and after the tear backgroundDbfs?: number; seed?: number; } export function syntheticTear(opts: SyntheticOptions): Float32Array { const fs = opts.sampleRate ?? 48000; const clicks = opts.clicks ?? 4000; const quiet = opts.quietSeconds ?? 1; const rand = mulberry32(opts.seed ?? 1); // one fixed click template (same for every synthetic tear): decaying high-passed noise, ~1 ms const tRand = mulberry32(12345); const tl = Math.round(0.002 * fs); const template = new Float32Array(tl); let prev = 0; for (let i = 0; i < tl; i++) { const g = gauss(tRand); template[i] = (g - prev) * Math.exp(-i / (0.00025 * fs)); prev = g; } let te = 0; for (const v of template) te += v * v; for (let i = 0; i < tl; i++) template[i] /= Math.sqrt(te); const n = Math.round((2 * quiet + opts.tearSeconds) * fs); const out = new Float32Array(n); const bg = 10 ** ((opts.backgroundDbfs ?? -75) / 20); for (let i = 0; i < n; i++) out[i] = bg * gauss(rand); const start = quiet * fs; const len = opts.tearSeconds * fs; for (let c = 0; c < clicks; c++) { const pos = Math.floor(start + rand() * (len - tl)); // log-normal amplitudes, random polarity const amp = 0.02 * Math.exp(0.8 * gauss(rand)) * (rand() < 0.5 ? -1 : 1); for (let i = 0; i < tl; i++) out[pos + i] += amp * template[i]; } return out; }