Skip to content

Commit f8701ff

Browse files
committed
denoise-detect: click score is impulses per second, not residual kurtosis; noise-estimate: minimum statistics by monotonic deques
- classify: a glottal pulse train raised the kurtosis too, so declick fired on 6 of 10 clean narrations. Now isolated impulses per second (a pitch period repeats, a click doesn't), trigger CLICK_RATE = 1: speech and a sung vowel 0.00/s, clicks 2.58/s. scores.click changes meaning: 0.2.0 - minStats: per-bin monotonic deques instead of a rescan of D frames; bit-identical, 7.6x faster. 1.0.2 - umbrella 0.3.11
1 parent 9eb894b commit f8701ff

8 files changed

Lines changed: 104 additions & 53 deletions

File tree

‎package-lock.json‎

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@audio/denoise",
3-
"version": "0.3.10",
3+
"version": "0.3.11",
44
"type": "module",
55
"description": "Noise reduction — gate, spectral subtraction, Wiener, OM-LSA, dehum, declick, decrackle, declip, deesser, dereverb",
66
"sideEffects": false,
@@ -51,7 +51,7 @@
5151
},
5252
"dependencies": {
5353
"@audio/vad": "^1.0.0",
54-
"@audio/noise-estimate": "^1.0.0",
54+
"@audio/noise-estimate": "^1.0.2",
5555
"@audio/denoise-spectral": "^0.1.0",
5656
"@audio/denoise-wiener": "^0.1.0",
5757
"@audio/denoise-omlsa": "^0.1.0",
@@ -63,7 +63,7 @@
6363
"@audio/denoise-deplosive": "^0.1.0",
6464
"@audio/denoise-debreath": "^0.1.0",
6565
"@audio/denoise-dereverb": "^0.1.0",
66-
"@audio/denoise-detect": "^0.1.0",
66+
"@audio/denoise-detect": "^0.2.0",
6767
"@audio/dynamics-gate": "^0.1.4",
6868
"@audio/denoise-repair": "^0.1.0",
6969
"@audio/quality": "^1.0.0",

‎packages/denoise-detect/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ denoise(data, { force: 'wiener' }) // skip classifie
2626

2727
**Routing (in priority order):**
2828
1. tonal hum (Goertzel — ≥2 of first 3 harmonics show 50× line/off-line ratio at 50 or 60 Hz)
29-
2. impulses (excess kurtosis of AR residual > 12)
29+
2. impulses: AR(30) residual samples over 12× the residual RMS within ±10 ms and 2× every residual 2.5–15 ms away (a glottal pulse has its like one pitch period off), more than 1 a second (`CLICK_RATE`)
3030
3. sibilance (high/mid band power ratio > 8)
3131
4. LF rumble (low/mid band power ratio > 3)
3232
5. non-stationary noise (frame-energy CV > 0.6) → omlsa

‎packages/denoise-detect/denoise.js‎

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// Classification (single STFT sweep over the input):
55
// - hum — narrow peaks at mains harmonics (Goertzel) → dehum
6-
// - click — high kurtosis of AR residual → declick
6+
// - click — impulses standing out of the AR residual, over 1 a second → declick
77
// - hi — 5–9 kHz / mid energy ratio → deesser
88
// - lf — LF/mid energy ratio → dewind
99
// - stationarity — frame-energy floor CV: stable → wiener, wandering → omlsa
@@ -111,31 +111,10 @@ export function classify(data, fs = 44100) {
111111
// HI/MF ratio
112112
let hiRatio = hiSum / Math.max(mfSum, 1e-30)
113113

114-
// Click score: max AR-residual excess kurtosis over windows spanning the whole
115-
// signal. Clicks are sparse and impulsive — a fixed prefix window can miss them
116-
// entirely, so scan across and take the peak (the window holding a click spikes).
117-
let clickScore = 0
118-
let cw = 4096
119-
if (data.length >= cw) {
120-
let stride = Math.max(cw, Math.floor((data.length - cw) / 8) || cw)
121-
for (let start = 0; start + cw <= data.length; start += stride) {
122-
let seg = data.subarray(start, start + cw)
123-
try {
124-
let { a } = arFit(seg, 30)
125-
let resid = new Float64Array(cw), mean = 0, m2 = 0, m4 = 0
126-
for (let i = 30; i < cw; i++) {
127-
let s = seg[i]
128-
for (let k = 1; k <= 30; k++) s += a[k] * seg[i - k]
129-
resid[i] = s; mean += s
130-
}
131-
mean /= (cw - 30)
132-
for (let i = 30; i < cw; i++) { let d = resid[i] - mean; m2 += d * d; m4 += d * d * d * d }
133-
m2 /= (cw - 30); m4 /= (cw - 30)
134-
let ex = m2 > 0 ? m4 / (m2 * m2) - 3 : 0 // excess kurtosis
135-
if (ex > clickScore) clickScore = ex
136-
} catch {}
137-
}
138-
}
114+
// Click score: impulses per second. A click stands far out of the AR(30) prediction error around it; a glottal
115+
// pulse doesn't, its neighbours 2.5–12 ms away being pulses too (the error's kurtosis can't tell them apart:
116+
// clean narration read 2.5–401 against a trigger of 12).
117+
let clickScore = impulseRate(data, fs)
139118

140119
// Noise stationarity: CV of the frame-energy FLOOR (rolling minimum over ~0.75 s).
141120
// Speech dynamics ride above the floor, so the floor tracks the *noise bed*:
@@ -170,7 +149,7 @@ export function classify(data, fs = 44100) {
170149
// humBest is a hit count: ≥2 of the first 3 harmonics show 20× peak-to-median sharpness.
171150
let method = 'wiener'
172151
if (humBest >= 2) method = 'dehum'
173-
else if (clickScore > 12) method = 'declick'
152+
else if (clickScore > CLICK_RATE) method = 'declick'
174153
else if (hiRatio > 8) method = 'deesser' // white noise scores ~3.9 by bandwidth alone
175154
else if (lfRatio > 3) method = 'dewind'
176155
else if (floorCV > 0.3) method = 'omlsa' // white ~0.06 · rumble ~0.2 · babble ~0.5
@@ -179,6 +158,39 @@ export function classify(data, fs = 44100) {
179158
}
180159

181160
// Goertzel power at frequency f.
161+
/** Impulses per second a recording must carry for declick. Measured: clean narration, music and a sung vowel 0–0.87;
162+
* the same with clicks at 2.5 a second 1.9 and up, faint ones (0.05) 1.3 and up (Spoken Wikipedia takes, lena). */
163+
export const CLICK_RATE = 1
164+
165+
/** Impulses per second. An impulse is an AR(30) residual sample over 12× the residual RMS within ±10 ms that also
166+
* towers (2×) over every residual 2.5–15 ms away: a glottal pulse has its like one pitch period off, a click doesn't.
167+
* Events 5 ms apart, over up to 64 windows of 4096 samples spread across the signal (≈ 6 s at 44.1 kHz). */
168+
function impulseRate(data, fs) {
169+
const W = 4096, P = 30, K = 12, M = 2
170+
const h = Math.round(0.01 * fs), gap = Math.round(0.005 * fs), near = Math.round(0.0025 * fs), far = Math.round(0.015 * fs)
171+
if (data.length < W || W - P <= 2 * h) return 0
172+
let count = Math.min(64, Math.floor(data.length / W)), stride = count > 1 ? (data.length - W) / (count - 1) : 0
173+
let r = new Float64Array(W), events = 0, seconds = 0
174+
for (let w = 0; w < count; w++) {
175+
let seg = data.subarray(Math.round(w * stride), Math.round(w * stride) + W), a
176+
try { ({ a } = arFit(seg, P)) } catch { continue }
177+
for (let i = P; i < W; i++) { let e = seg[i]; for (let k = 1; k <= P; k++) e += a[k] * seg[i - k]; r[i] = e }
178+
let e2 = 0, last = -gap
179+
for (let i = P; i < P + 2 * h; i++) e2 += r[i] * r[i]
180+
for (let i = P + h; i < W - h; i++) {
181+
let v = Math.abs(r[i]), rms = Math.sqrt(e2 / (2 * h))
182+
if (rms > 0 && v > K * rms && i - last > gap) {
183+
let m = 0
184+
for (let j = Math.max(P, i - far); j <= Math.min(W - 1, i + far); j++) if (Math.abs(j - i) >= near && Math.abs(r[j]) > m) m = Math.abs(r[j])
185+
if (v > M * m) { events++; last = i }
186+
}
187+
e2 += r[i + h] * r[i + h] - r[i - h] * r[i - h]
188+
}
189+
seconds += (W - P - 2 * h) / fs
190+
}
191+
return seconds ? events / seconds : 0
192+
}
193+
182194
function goertzelE(data, f, fs) {
183195
let w = 2 * Math.PI * f / fs, c = 2 * Math.cos(w), s1 = 0, s2 = 0
184196
for (let i = 0; i < data.length; i++) { let s = data[i] + c * s1 - s2; s2 = s1; s1 = s }

‎packages/denoise-detect/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@audio/denoise-detect",
3-
"version": "0.1.9",
3+
"version": "0.2.0",
44
"description": "denoise — content-aware auto-selector that classifies the dominant noise type",
55
"type": "module",
66
"sideEffects": false,

‎packages/noise-estimate/index.js‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,29 @@ export function minStats(half, opts = {}) {
4040
let D = opts.D || 96
4141
let alpha = opts.alpha ?? 0.7 // smoothing on PSD
4242
let bias = opts.bias ?? 1.5 // empirical comp from Martin §VII
43-
let smoothed = new Float64Array(half + 1)
44-
let psd = new Float64Array(half + 1)
45-
let buf = [] // ring of recent smoothed frames
43+
let bins = half + 1
44+
let smoothed = new Float64Array(bins)
45+
let psd = new Float64Array(bins)
46+
// The D-frame minimum per bin, by monotonic deque: each bin keeps the frames that can still be its minimum,
47+
// values rising from head to tail (O(1) amortized per bin per frame, not a rescan of D frames)
48+
let val = new Float64Array(bins * D), at = new Int32Array(bins * D)
49+
let head = new Int32Array(bins), size = new Int32Array(bins), frame = 0
4650

4751
return {
4852
psd,
4953
update(mag) {
50-
let p = new Float64Array(half + 1)
51-
for (let k = 0; k <= half; k++) {
54+
for (let k = 0, o = 0; k <= half; k++, o += D) {
5255
let pk = mag[k] * mag[k]
53-
smoothed[k] = alpha * smoothed[k] + (1 - alpha) * pk
54-
p[k] = smoothed[k]
55-
}
56-
buf.push(p)
57-
if (buf.length > D) buf.shift()
58-
for (let k = 0; k <= half; k++) {
59-
let mn = Infinity
60-
for (let i = 0; i < buf.length; i++) if (buf[i][k] < mn) mn = buf[i][k]
61-
psd[k] = mn * bias
56+
let v = smoothed[k] = alpha * smoothed[k] + (1 - alpha) * pk
57+
let h = head[k], n = size[k], t
58+
if (n && at[o + h] <= frame - D) { if (++h === D) h = 0; n-- } // oldest left the window
59+
while (n && val[o + ((t = h + n - 1) >= D ? t - D : t)] >= v) n-- // newer and no larger: they can't be minima
60+
t = h + n >= D ? h + n - D : h + n
61+
val[o + t] = v; at[o + t] = frame; n++
62+
head[k] = h; size[k] = n
63+
psd[k] = val[o + h] * bias
6264
}
65+
frame++
6366
}
6467
}
6568
}

‎packages/noise-estimate/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@audio/noise-estimate",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Noise PSD estimation — minimum statistics (Martin 2001), IMCRA (Cohen 2003), and quiet-segment profiling for spectral denoisers",
55
"type": "module",
66
"sideEffects": false,

‎test.js‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,23 @@ test('omlsa — improves segSNR on noisy speech', () => {
221221
ok(segSnr(clean, speech) > segSnr(dirty, speech), 'segSNR improved')
222222
})
223223

224+
// minStats keeps each bin's minimum over the last D frames by monotonic deque; the rescan it replaced is the reference.
225+
// Windows of 1, 3 and 96 frames, fewer frames than the window, repeated values, minima falling and rising.
226+
test('minStats — the D-frame minimum equals a rescan of the last D frames', () => {
227+
let seed = 5, rnd = () => (seed = (seed * 16807) % 2147483647) / 2147483647
228+
for (let [half, D, frames] of [[64, 96, 300], [16, 1, 50], [16, 3, 200], [64, 96, 20]]) {
229+
let est = minStats(half, { D }), smoothed = new Float64Array(half + 1), hist = [], bad = 0
230+
for (let f = 0; f < frames; f++) {
231+
let mag = Float64Array.from({ length: half + 1 }, () => f % 37 < 5 ? 0 : rnd() < 0.1 ? 1 : rnd() * (1 + (f % 300) / 30))
232+
est.update(mag)
233+
hist.push(Float64Array.from(mag, (m, k) => smoothed[k] = 0.7 * smoothed[k] + (1 - 0.7) * (m * m)))
234+
if (hist.length > D) hist.shift()
235+
for (let k = 0; k <= half; k++) if (est.psd[k] !== Math.min(...hist.map(p => p[k])) * 1.5) bad++
236+
}
237+
is(bad, 0, `${half + 1} bins, D ${D}, ${frames} frames`)
238+
}
239+
})
240+
224241
// =================== declick ===================
225242

226243
test('declick — removes injected clicks', () => {
@@ -394,6 +411,25 @@ test('classify — clicks route to declick', () => {
394411
is(classify(x, fs).method, 'declick')
395412
})
396413

414+
// Speech isn't clicks. Its AR residual is a glottal pulse train, impulsive but every 2.5–12 ms at like size; a click
415+
// stands alone. The residual's kurtosis, the old score, read 2.5–401 on clean narration against a trigger of 12.
416+
test('classify — speech and a sung vowel are not clicks; speech with clicks at 2.5 a second is', async () => {
417+
let { CLICK_RATE } = await import('@audio/denoise-detect')
418+
let speech = lena.subarray(0, fs * 4)
419+
let vowel = new Float32Array(fs * 2), y1 = 0, y2 = 0, z1 = 0, z2 = 0
420+
for (let i = 0; i < vowel.length; i++) {
421+
let x = i % Math.round(fs / 120) === 0 ? 1 : 0 // 120 Hz glottal pulses
422+
let y = x + 1.9 * Math.cos(2 * Math.PI * 700 / fs) * 0.95 * y1 - 0.9025 * y2; y2 = y1; y1 = y // formant at 700 Hz
423+
let z = y + 2 * Math.cos(2 * Math.PI * 1200 / fs) * 0.93 * z1 - 0.8649 * z2; z2 = z1; z1 = z // and 1200 Hz
424+
vowel[i] = z * 0.01
425+
}
426+
ok(classify(speech, fs).scores.click < CLICK_RATE, 'speech: ' + classify(speech, fs).scores.click.toFixed(2) + ' impulses/s')
427+
ok(classify(vowel, fs).scores.click < CLICK_RATE, 'vowel: ' + classify(vowel, fs).scores.click.toFixed(2) + ' impulses/s')
428+
let dirty = Float32Array.from(speech)
429+
for (let t = 0.2; t < 4; t += 0.4) dirty[Math.round(t * fs)] += 0.3
430+
is(classify(dirty, fs).method, 'declick', 'clicks: ' + classify(dirty, fs).scores.click.toFixed(2) + ' impulses/s')
431+
})
432+
397433
test('classify — sibilance routes to deesser', () => {
398434
let x = add(noise(fs, 0.05), sine(7000, fs, 0.3))
399435
is(classify(x, fs).method, 'deesser')

0 commit comments

Comments
 (0)