-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetection.lib
52 lines (40 loc) · 1.67 KB
/
detection.lib
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* This library contains information processing functions for the analysis of
* signals and the detection of Larsen tones.
*
* Copyright (c) 2019-2020, Dario Sanfilippo <sanfilippo.dario at gmail dot com>
*
* All rights reserved.
*/
dt = library("detection.lib");
// Averaging function based on a 1-pole lowpass filter and the T19 time
// constant, that is, the system decays by 1/e^2.2 in "frame" seconds.
avg_t19(frame, x) = si.smooth(ba.tau2pole(frame / 2.2), x);
// Bandpass filter bank with 6th-order elliptic filters on the 0-20kHz
// range. N is the number of bands including DC and Nyquist.
bpbank(N) = an.mth_octave_analyzer6e(1, 20000, N);
// NAN-safe division
div(x1, x2) = ba.if(x2 == 0, 0, x1 / x2);
// Backwards Euler's integrator with cut-off frequency.
integrator(cf, x) = x * w(cf) : (+ : (min(1, max(0))))
~ _
with {
w(f) = 2 * ma.PI * f / ma.SR;
};
// Root mean square. Averaging frame in seconds.
rms(frame, x) = x * x : avg_t19(frame) : sqrt;
// Spectral centroid through N-th-order recursive adaptive filtering. Analysis
// frame in seconds.
sc(N, frame, x) = ( (_ ,
x) : xover(N) : ro.cross(2) : (rms(frame) - rms(frame)) :
div(_, rms(frame, x)) ^ 3 : integrator(1 / frame) ^ 2)
~ *(ma.SR / 2);
// N-th-order Butterworth crossover. N must be known at compile-time.
xover(N, cf, x) = fi.lowpass(N, cf1, x) ,
fi.highpass(N, cf1, x)
with {
cf1 = min(max(cf, 20), ma.SR / 2 - 20);
};
// Zero-crossing indicator function.
zc(x) = x * x' < 0;
// Zero-crossing rate. Analysis frame in seconds.
zcr(frame, x) = zc(x) : avg_t19(frame);