Skip to main content

sva_samples/measure/
modulation.rs

1// Concern: how each band group's envelopes and centroid move per sliding window: rate, depth | Non-concern: the band filters, any verdict | IO: (&[f64], rate, start, hop, window, bpm) -> Modulation
2
3use std::f64::consts::{PI, TAU};
4use std::ops::Range;
5
6use sva_formula::C64;
7use sva_formula::filter::Shape;
8
9use super::bands::{self, Bands, Smoothing};
10use super::periodicity::SPLIT_HZ;
11use super::sliding::{self, Span, Stats, Unread, stats};
12use crate::biquad::{Coeffs, design};
13use crate::fft::fft;
14use crate::stft::hann_periodic;
15
16/// `low` under the first, `mid` under the second, `high` from it.
17pub const SPLITS_HZ: [f64; 2] = [SPLIT_HZ, 2000.0];
18pub const MIN_HZ: f64 = 0.5;
19pub const MAX_HZ: f64 = 20.0;
20pub const PEAKS: usize = 3;
21/// Each band's energy through four one-poles at 200 Hz before `bands` decimates it to 2 kHz:
22/// a tone's ripple at twice its frequency, near a multiple of 2 kHz, would alias onto a slow
23/// modulation, and falls some 78 dB by 1.92 kHz, while a 20 Hz modulation keeps 98%.
24pub const SMOOTHING: Smoothing = Smoothing {
25    hz: Some(SMOOTHING_HZ),
26    poles: 4,
27};
28const SMOOTHING_HZ: f64 = 200.0;
29/// The rate the envelopes are read at once the lowpass below has cleared the band.
30const ENVELOPE_HZ: f64 = 100.0;
31const LOWPASS_HZ: f64 = 50.0;
32/// Half the lowpass's taps less one: 257 taps at 2 kHz.
33const HALF_TAPS: usize = 128;
34
35pub const MODEL: &str = "modulation spectrum: the spectrum of each band's amplitude envelope \
36                         over a sliding window, the representation of Atlas, L. and Shamma, S. \
37                         A. (2003), Joint acoustic and modulation frequency, EURASIP J. Appl. \
38                         Signal Process. 2003(7) 668-675, read over the 0.5-20 Hz modulation \
39                         range the envelope filterbank of Dau, T., Kollmeier, B. and \
40                         Kohlrausch, A. (1997), Modeling auditory processing of amplitude \
41                         modulation I, J. Acoust. Soc. Am. 102(5) 2892-2905, models. Envelopes: \
42                         the RMS of each ERB band of `bands` (Glasberg and Moore's filter \
43                         widths), its energy smoothed by `smoothing.poles` one-pole lowpasses at \
44                         `smoothing.hz` in place of the one at the band's own width, decimated \
45                         to 2 kHz, then by a 257-tap Blackman-windowed sinc lowpass at 50 Hz to \
46                         `envelope_hz` (Harris, F. J. (1978), On the use of windows for \
47                         harmonic analysis with the discrete Fourier transform, Proc. IEEE \
48                         66(1) 51-83: stopband some 74 dB down, from about 71 Hz, so what \
49                         would fold into 0-20 Hz is that far down), the taps renormalized \
50                         where they reach past the target. G(f) = |H(f)| L(f), H the \
51                         smoothing's response on the energy and L the 50 Hz lowpass's on the \
52                         envelope, off their coefficients, is what a modulation at f keeps to \
53                         first order, and each envelope peak's depth is divided by G at its \
54                         rate, a centroid peak's by L; `passband_gain_at_max_hz` is G at \
55                         `max_hz`. The first \
56                         component alone. Groups: a band is `low` where its centre is under \
57                         `splits_hz[0]`, `mid` under `splits_hz[1]` and `high` from it; the \
58                         filters' skirts overlap, so a tone leaks attenuated into the \
59                         neighbouring group, and its modulation with it. Each window is \
60                         `window_secs` of envelope centred on t, t stepping by `hop_secs` \
61                         from the target's start as every framed reading steps, clipped to \
62                         the target at its ends, `from`/`to` its span; a periodic Hann window \
63                         w over it, each band's Hann-weighted mean c = sum w e / sum w \
64                         subtracted (so the mean leaves no lobe), zero-padded to `bin_step_hz` \
65                         and transformed by FFT. depth(f) = 2 sum over the group's bands of \
66                         |W(f)| over (sum w times sum of c): the modulation index m of an \
67                         envelope c (1 + m cos 2 pi f t), read as the c-weighted mean of the \
68                         bands' indices, magnitudes summed so bands moving out of phase, as \
69                         under a filter sweep, do not cancel; an envelope near silent \
70                         between swells reads past 1, up to 2 for a pulse train. `level` is \
71                         the sum of c, linear amplitude. peaks: the three local maxima of \
72                         depth from `min_hz` to `max_hz` whose bins read deepest, the deepest \
73                         after correction first, each placed by inverting the periodic Hann's \
74                         exact transform W through its bin and the deeper neighbour, as \
75                         Grandke, T. (1983), Interpolation algorithms for discrete Fourier \
76                         transforms of weighted signals, IEEE Trans. Instrum. Meas. 32(2) \
77                         350-355, does between unpadded bins: the offset u, within q of the \
78                         bin, where |W(q - u)| / |W(u)| is the two bins' ratio, q the padded \
79                         bin in window bins, that ratio rising with u; its depth the bin's \
80                         over |W(u)| / W(0), then over G. Bounds: under the model that in the \
81                         window each band's envelope ahead of the smoothing is a carrier's \
82                         amplitude c (1 + m cos(2 pi f t + phi)), one m <= 1 in every band, \
83                         the carrier's ripple wholly removed by the smoothing, and a centroid \
84                         is mean + depth cos(2 pi f t + phi), the two bins read the main lobe \
85                         moved by: the modulation's image at -f and the removed mean, at most \
86                         r(2 (x - q)) + 2 r(x - q)^2 of its amplitude, x the bin in window \
87                         bins, r(y) the transform's envelope over its sidelobes (|W| with \
88                         |sin pi y| taken as 1, falling for 1 < y < n/2), the mean also moving \
89                         the level by m r(x - q); and for an envelope the square law: the \
90                         smoothing h, positive of unit sum, acts on energy, so each envelope \
91                         is h * a plus d, 0 <= d = var_h(a) / (sqrt(h * a^2) + h * a) <= c \
92                         min(m^2 V / (2 (1 - m)), m sqrt V), V = (1 - |H(f)|^2 + |H(2f) - \
93                         H(f)^2|) / 2 at the bin's rate plus q, V rising with f, so the \
94                         lowpass (S the sum of its taps' magnitudes) moves a bin by at most \
95                         (1 + 3 r(x - q)) S c min(...) and the level by S c min(...). The \
96                         ratio widened by those, \
97                         inverted, gives `hz_bounds`; the bin over the kernel's most and \
98                         least across them and over G's (|H| falling with f, L moving no \
99                         faster than its taps' slope) gives `depth_bounds`; m's ceiling is \
100                         1, or lower where the square law's sqrt V branch bounds it, and each \
101                         round's depth ceiling bounds the next. `period_beats_bounds` follow \
102                         from `hz_bounds`. They cover the bins, the interpolation, the \
103                         correction and the square law alone, under that model: a tone's \
104                         sidebands f either side meet each band's skirt unevenly, so the \
105                         bands' envelopes carry a tone's index changed, by up to several \
106                         percent near `max_hz` with where the tone falls among the bands, \
107                         which is the bands' view and not an error the bounds cover; a second \
108                         modulation, partial or component near f is outside the model. They \
109                         are null where the window is clipped, where no round bounds the \
110                         peak (the perturbations swamping the main lobe), or where the \
111                         depth's floor stands over its ceiling, refuting the model. Deep, fast \
112                         modulation bounds wide: near `max_hz` the square law's bound reaches \
113                         a fifth of m. A strong peak's Hann sidelobes, some \
114                         31 dB under it and 1.5 / window beside it, read as peaks of their \
115                         own; a modulation that is no sinusoid reads at its rate and its \
116                         harmonics; two modulations closer than \
117                         `resolution_hz`, the Hann main lobe's half-width 2 / window, or one \
118                         slower than that, interfere and are not resolved, and a window \
119                         clipped at the target's ends reads from 2 / (to - from) Hz. \
120                         period_beats, where `bpm` is given: bpm / (60 hz), so 0.5 is an \
121                         eighth-note wobble. centroid: per instant the group's spectral \
122                         centroid, sum of centre e over sum of e across its bands at 2 kHz, \
123                         lowpassed and decimated as the envelopes are; `mean_hz` its \
124                         Hann-weighted mean, and its peaks read as the envelopes' are, their \
125                         `depth_hz` the amplitude in Hz of a centroid swinging as mean + depth \
126                         cos 2 pi f t, which a filter sweep moves and tremolo does not; null \
127                         where any instant in the window has no energy in the group. Beating \
128                         between partials inside one band reads as modulation at their \
129                         difference, as it is heard; a modulation under `min_hz` (a pad \
130                         evolving over seconds) reads in `level` and `centroid.mean_hz` from \
131                         window to window, not in the peaks. No threshold: a static sound \
132                         reads peaks of small depth, never none. summary: the leading peak's \
133                         hz and depth over the windows with a peak, the centroid's mean_hz \
134                         over the windows with one, the leading centroid peak's depth_hz over \
135                         the windows with one, each its mean, min and max with the t each \
136                         stands at. Exact path: none; a closed form is read \
137                         off its rendered samples, because an envelope's band-limited \
138                         spectrum over a window is stated by no closed form";
139
140#[derive(Clone, Copy, Debug, PartialEq, Eq)]
141pub enum Group {
142    Low,
143    Mid,
144    High,
145}
146
147impl Group {
148    pub const ALL: [Group; 3] = [Group::Low, Group::Mid, Group::High];
149
150    pub fn key(self) -> &'static str {
151        match self {
152            Group::Low => "low",
153            Group::Mid => "mid",
154            Group::High => "high",
155        }
156    }
157}
158
159/// `depth` is a modulation index for an envelope, Hz for a centroid. Each bound is MODEL's,
160/// `None` where the window leaves or refutes MODEL's lone modulation.
161#[derive(Clone, Copy, Debug, PartialEq)]
162pub struct Peak {
163    pub hz: f64,
164    pub hz_bounds: Option<(f64, f64)>,
165    pub period_beats: Option<f64>,
166    pub period_beats_bounds: Option<(f64, f64)>,
167    pub depth: f64,
168    pub depth_bounds: Option<(f64, f64)>,
169}
170
171#[derive(Clone, Debug, PartialEq)]
172pub struct Centroid {
173    pub mean_hz: f64,
174    /// Highest first.
175    pub peaks: Vec<Peak>,
176}
177
178#[derive(Clone, Debug, PartialEq)]
179pub struct Window {
180    pub t_secs: f64,
181    pub from_secs: f64,
182    pub to_secs: f64,
183    pub level: f64,
184    /// Highest first.
185    pub peaks: Vec<Peak>,
186    pub centroid: Option<Centroid>,
187}
188
189#[derive(Clone, Copy, Debug, PartialEq)]
190pub struct Summary {
191    pub windows_with_peak: usize,
192    pub hz: Option<Stats>,
193    pub depth: Option<Stats>,
194    pub centroid_hz: Option<Stats>,
195    pub centroid_depth_hz: Option<Stats>,
196}
197
198#[derive(Clone, Debug, PartialEq)]
199pub struct Track {
200    pub group: Group,
201    pub centres_hz: Vec<f64>,
202    pub windows: Vec<Window>,
203    pub summary: Summary,
204}
205
206#[derive(Clone, Debug, PartialEq)]
207pub struct Modulation {
208    pub hop_secs: f64,
209    pub window_secs: f64,
210    pub envelope_hz: f64,
211    pub bin_step_hz: f64,
212    pub resolution_hz: f64,
213    pub min_hz: f64,
214    pub max_hz: f64,
215    pub bpm: Option<f64>,
216    pub passband_gain_at_max_hz: f64,
217    pub tracks: Vec<Track>,
218}
219
220pub fn measure(
221    samples: &[f64],
222    sample_rate: f64,
223    start_secs: f64,
224    hop_secs: f64,
225    window_secs: f64,
226    bpm: Option<f64>,
227) -> Result<Modulation, Unread> {
228    sliding::fits(samples.len(), sample_rate, window_secs, 1.0 / MIN_HZ)?;
229    let split = bands::smoothed(samples, sample_rate, start_secs, SMOOTHING);
230    let lowpass = Lowpass::new(split.rate_hz);
231    let groups = grouped(&split, &lowpass);
232    let rate = split.rate_hz / lowpass.step as f64;
233    let len = groups.first().map_or(0, |g| g.centroid.len());
234    let grid = sliding::spans(
235        samples.len(),
236        sample_rate,
237        start_secs,
238        hop_secs,
239        rate,
240        window_secs,
241        len,
242    );
243    let size = (grid.window_secs * rate).round() as usize;
244    let n = (2 * size).next_power_of_two();
245    let step = rate / n as f64;
246    let read = Read {
247        step,
248        bins: 1..((MAX_HZ / step).ceil() as usize + 2).min(n / 2),
249        n,
250        bpm,
251        size,
252        rate,
253        sample_rate,
254        smoothing: design(Shape::OnePole, SMOOTHING_HZ, 0.0, 0.0, sample_rate),
255        lowpass: &lowpass,
256    };
257    let tracks = Group::ALL
258        .into_iter()
259        .zip(groups)
260        .map(|(group, held)| {
261            let windows: Vec<Window> = grid.spans.iter().map(|s| read.window(&held, s)).collect();
262            Track {
263                group,
264                centres_hz: held.centres_hz,
265                summary: summary(&windows),
266                windows,
267            }
268        })
269        .collect();
270    Ok(Modulation {
271        hop_secs: grid.hop_secs,
272        window_secs: grid.window_secs,
273        envelope_hz: rate,
274        bin_step_hz: step,
275        resolution_hz: 2.0 / grid.window_secs,
276        min_hz: MIN_HZ.max(2.0 / grid.window_secs),
277        max_hz: MAX_HZ,
278        bpm,
279        passband_gain_at_max_hz: read.gain(MAX_HZ, Kind::Envelope),
280        tracks,
281    })
282}
283
284/// A Blackman-windowed sinc, unit gain at 0 Hz, taken every `step` samples.
285struct Lowpass {
286    taps: Vec<f64>,
287    step: usize,
288    rate: f64,
289}
290
291impl Lowpass {
292    fn new(rate: f64) -> Lowpass {
293        let cut = LOWPASS_HZ / rate;
294        let half = HALF_TAPS as f64;
295        let raw: Vec<f64> = (0..=2 * HALF_TAPS)
296            .map(|i| {
297                let j = i as f64 - half;
298                let sinc = match j == 0.0 {
299                    true => 2.0 * cut,
300                    false => (TAU * cut * j).sin() / (PI * j),
301                };
302                let x = PI * j / half;
303                sinc * (0.42 + 0.5 * x.cos() + 0.08 * (2.0 * x).cos())
304            })
305            .collect();
306        let sum: f64 = raw.iter().sum();
307        Lowpass {
308            taps: raw.into_iter().map(|h| h / sum).collect(),
309            step: (rate / ENVELOPE_HZ).round().max(1.0) as usize,
310            rate,
311        }
312    }
313
314    /// Symmetric, so its response is real.
315    fn gain(&self, hz: f64) -> f64 {
316        self.taps
317            .iter()
318            .enumerate()
319            .map(|(i, h)| h * (TAU * hz * (i as f64 - HALF_TAPS as f64) / self.rate).cos())
320            .sum()
321    }
322
323    /// Over the gain's slope in hz: each tap's cosine turns no faster than its lag.
324    fn slope(&self) -> f64 {
325        self.taps
326            .iter()
327            .enumerate()
328            .map(|(i, h)| h.abs() * TAU * (i as f64 - HALF_TAPS as f64).abs() / self.rate)
329            .sum()
330    }
331
332    /// The most the output can swing for an input swinging over a unit range.
333    fn spread(&self) -> f64 {
334        self.taps.iter().map(|h| h.abs()).sum()
335    }
336
337    /// Centred on each kept sample, the taps past either end left out and the rest
338    /// renormalized, so a constant stays that constant to the last sample. NaN in reach
339    /// stays NaN.
340    fn apply(&self, x: &[f64]) -> Vec<f64> {
341        (0..x.len())
342            .step_by(self.step)
343            .map(|m| {
344                let lo = m.saturating_sub(HALF_TAPS);
345                let hi = (m + HALF_TAPS + 1).min(x.len());
346                let taps = &self.taps[lo + HALF_TAPS - m..hi + HALF_TAPS - m];
347                let sum: f64 = taps.iter().sum();
348                taps.iter().zip(&x[lo..hi]).map(|(h, v)| h * v).sum::<f64>() / sum
349            })
350            .collect()
351    }
352}
353
354/// One group's decimated band envelopes and centroid.
355struct Held {
356    centres_hz: Vec<f64>,
357    envelopes: Vec<Vec<f64>>,
358    /// NaN where the group holds no energy.
359    centroid: Vec<f64>,
360}
361
362fn grouped(split: &Bands, lowpass: &Lowpass) -> Vec<Held> {
363    let tracks = &split.bands;
364    let edge = |hz: f64| {
365        tracks
366            .iter()
367            .position(|b| b.centre_hz >= hz)
368            .unwrap_or(tracks.len())
369    };
370    let (low, mid) = (edge(SPLITS_HZ[0]), edge(SPLITS_HZ[1]));
371    let len = tracks.first().map_or(0, |b| b.rms.len());
372    [0..low, low..mid, mid..tracks.len()]
373        .into_iter()
374        .map(|range: Range<usize>| {
375            let held = &tracks[range];
376            let centroid: Vec<f64> = (0..len)
377                .map(|i| {
378                    let (weighted, sum) = held.iter().fold((0.0, 0.0), |(w, s), b| {
379                        (w + b.centre_hz * b.rms[i], s + b.rms[i])
380                    });
381                    match sum > 0.0 {
382                        true => weighted / sum,
383                        false => f64::NAN,
384                    }
385                })
386                .collect();
387            Held {
388                centres_hz: held.iter().map(|b| b.centre_hz).collect(),
389                envelopes: held.iter().map(|b| lowpass.apply(&b.rms)).collect(),
390                centroid: lowpass.apply(&centroid),
391            }
392        })
393        .collect()
394}
395
396struct Read<'a> {
397    step: f64,
398    /// From the first bin past 0 Hz to one past `MAX_HZ`.
399    bins: Range<usize>,
400    n: usize,
401    bpm: Option<f64>,
402    /// A whole window's envelope samples.
403    size: usize,
404    rate: f64,
405    sample_rate: f64,
406    smoothing: Coeffs,
407    lowpass: &'a Lowpass,
408}
409
410/// An envelope's depth is an index under the smoothing's square law; a centroid's is Hz.
411#[derive(Clone, Copy, Debug, PartialEq, Eq)]
412enum Kind {
413    Envelope,
414    Centroid,
415}
416
417/// Each round of `Read::bounded` tightens the index's ceiling the last one bounded.
418const ROUNDS: usize = 8;
419
420impl Read<'_> {
421    fn window(&self, held: &Held, s: &Span) -> Window {
422        let w = hann_periodic(s.to - s.from);
423        let total: f64 = w.iter().sum();
424        let lowest = MIN_HZ.max(2.0 / (s.to_secs - s.from_secs));
425        let segments: Vec<&[f64]> = held.envelopes.iter().map(|e| &e[s.from..s.to]).collect();
426        let means: Vec<f64> = segments.iter().map(|e| weighted_mean(&w, e)).collect();
427        let level: f64 = means.iter().sum();
428        let peaks = match level > 0.0 {
429            true => {
430                let mut depth = vec![0.0; self.bins.len()];
431                for spectrum in self.magnitudes(&w, &segments, &means) {
432                    for (d, m) in depth.iter_mut().zip(spectrum) {
433                        *d += 2.0 * m / (total * level);
434                    }
435                }
436                self.peaks(&depth, lowest, s, Kind::Envelope)
437            }
438            false => Vec::new(),
439        };
440        let track = &held.centroid[s.from..s.to];
441        let centroid = (!track.iter().any(|c| c.is_nan())).then(|| {
442            let mean_hz = weighted_mean(&w, track);
443            let spectrum = self.magnitudes(&w, &[track], &[mean_hz]).remove(0);
444            let depth: Vec<f64> = spectrum.into_iter().map(|m| 2.0 * m / total).collect();
445            Centroid {
446                mean_hz,
447                peaks: self.peaks(&depth, lowest, s, Kind::Centroid),
448            }
449        });
450        Window {
451            t_secs: s.t_secs,
452            from_secs: s.from_secs,
453            to_secs: s.to_secs,
454            level,
455            peaks,
456            centroid,
457        }
458    }
459
460    /// |DFT| of each windowed, mean-removed segment at `bins`; two segments share one complex
461    /// transform as its real and imaginary parts.
462    fn magnitudes(&self, w: &[f64], segments: &[&[f64]], means: &[f64]) -> Vec<Vec<f64>> {
463        let fill = |out: &mut [f64], at: usize| {
464            for ((o, x), h) in out.iter_mut().zip(segments[at]).zip(w) {
465                *o = h * (x - means[at]);
466            }
467        };
468        let mut out = Vec::with_capacity(segments.len());
469        for pair in (0..segments.len()).step_by(2) {
470            let (mut re, mut im) = (vec![0.0; self.n], vec![0.0; self.n]);
471            fill(&mut re, pair);
472            let both = pair + 1 < segments.len();
473            if both {
474                fill(&mut im, pair + 1);
475            }
476            fft(&mut re, &mut im);
477            let half = |sign: f64| -> Vec<f64> {
478                self.bins
479                    .clone()
480                    .map(|k| {
481                        let (r, i) = (re[self.n - k], -im[self.n - k]);
482                        (re[k] + sign * r).hypot(im[k] + sign * i) / 2.0
483                    })
484                    .collect()
485            };
486            out.push(half(1.0));
487            if both {
488                out.push(half(-1.0));
489            }
490        }
491        out
492    }
493
494    /// `depth` is indexed from `bins.start`: the three local maxima whose bins read deepest,
495    /// each located as MODEL states, the deepest first.
496    fn peaks(&self, depth: &[f64], lowest: f64, s: &Span, kind: Kind) -> Vec<Peak> {
497        let first = self.bins.start;
498        let mut found: Vec<usize> = (1..depth.len().saturating_sub(1))
499            .filter(|&i| {
500                let hz = (first + i) as f64 * self.step;
501                (lowest..=MAX_HZ).contains(&hz)
502                    && depth[i] > depth[i - 1]
503                    && depth[i] >= depth[i + 1]
504            })
505            .collect();
506        found.sort_by(|a, b| depth[*b].total_cmp(&depth[*a]).then(a.cmp(b)));
507        let mut peaks: Vec<Peak> = found
508            .into_iter()
509            .take(PEAKS)
510            .map(|i| self.located(depth, i, s.to - s.from, kind))
511            .collect();
512        peaks.sort_by(|a, b| b.depth.total_cmp(&a.depth).then(a.hz.total_cmp(&b.hz)));
513        peaks
514    }
515
516    /// The Hann kernel inverted through the deepest bin and its deeper neighbour, and MODEL's
517    /// bounds where the window holds its whole span.
518    fn located(&self, depth: &[f64], i: usize, size: usize, kind: Kind) -> Peak {
519        let q = size as f64 / self.n as f64;
520        let x = (self.bins.start + i) as f64 * q;
521        let side = match depth[i + 1] >= depth[i - 1] {
522            true => 1.0,
523            false => -1.0,
524        };
525        let d0 = depth[i];
526        let ratio = depth[(i as f64 + side) as usize] / d0;
527        let u = offset(ratio, q, size);
528        let hz = (x + side * u) * self.rate / size as f64;
529        let bounds = match size == self.size {
530            true => self.bounded(d0, ratio, x, side, size, kind),
531            false => None,
532        };
533        let beats = |hz: f64| self.bpm.map(|bpm| bpm / (60.0 * hz));
534        Peak {
535            hz,
536            hz_bounds: bounds.map(|b| b.0),
537            period_beats: beats(hz),
538            period_beats_bounds: bounds.and_then(|((lo, hi), _)| beats(hi).zip(beats(lo))),
539            depth: d0 / kernel(u, size) / self.gain(hz, kind),
540            depth_bounds: bounds.map(|b| b.1),
541        }
542    }
543
544    /// MODEL's (hz, depth) bounds for a peak at natural bin `x` reading `d0`, its neighbour on
545    /// `side` reading `ratio` of that; `None` where no round can bound it.
546    fn bounded(
547        &self,
548        d0: f64,
549        ratio: f64,
550        x: f64,
551        side: f64,
552        size: usize,
553        kind: Kind,
554    ) -> Option<((f64, f64), (f64, f64))> {
555        let q = size as f64 / self.n as f64;
556        let to_hz = |nu: f64| nu * self.rate / size as f64;
557        let kappa = reach(x - q, size);
558        let lambda = reach(2.0 * (x - q), size) + 2.0 * kappa * kappa;
559        let spread = self.lowpass.spread();
560        let v = match kind {
561            Kind::Envelope => self.square_law(to_hz(x + q)),
562            Kind::Centroid => 0.0,
563        };
564        let law = |m: f64| {
565            let tight = match m < 1.0 {
566                true => m * m * v / (2.0 * (1.0 - m)),
567                false => f64::INFINITY,
568            };
569            tight.min(m * v.sqrt())
570        };
571        let square = |m: f64| (1.0 + 3.0 * kappa) * spread * law(m);
572        let mut m = match kind {
573            Kind::Envelope => {
574                let (lo, _) = self.gains((to_hz(x - q), to_hz(x + q)), kind);
575                let room = lo * kernel(q, size)
576                    - d0 * (kappa + spread * v.sqrt())
577                    - spread * lambda
578                    - square(1.0);
579                match room > 0.0 {
580                    true => (d0 / room).min(1.0),
581                    false => 1.0,
582                }
583            }
584            Kind::Centroid => f64::INFINITY,
585        };
586        let mut out = None;
587        for _ in 0..ROUNDS {
588            let (level, amp) = match kind {
589                Kind::Envelope => (m * kappa + spread * law(m), m * spread),
590                Kind::Centroid => (0.0, d0 / (kernel(q, size) - lambda)),
591            };
592            let e = amp * lambda + square(m);
593            let floor = d0 * (1.0 - level) - e;
594            if !(floor > 0.0 && amp > 0.0 && amp.is_finite()) {
595                break;
596            }
597            let wide = (1.0 + ratio) * e / floor;
598            let (ua, ub) = (offset(ratio - wide, q, size), offset(ratio + wide, q, size));
599            let near = match ua <= 0.0 && ub >= 0.0 {
600                true => 0.0,
601                false => ua.abs().min(ub.abs()),
602            };
603            let far = ua.abs().max(ub.abs());
604            let (ha, hb) = (to_hz(x + side * ua), to_hz(x + side * ub));
605            let hz = (ha.min(hb), ha.max(hb));
606            let g = self.gains(hz, kind);
607            let amps = (
608                floor / kernel(near, size),
609                (d0 * (1.0 + level) + e) / kernel(far, size),
610            );
611            let depth = (amps.0 / g.1, amps.1 / g.0);
612            if depth.0 > m {
613                return None;
614            }
615            out = Some((hz, (depth.0, depth.1.min(m))));
616            if kind == Kind::Centroid || depth.1 >= m {
617                break;
618            }
619            m = depth.1;
620        }
621        out
622    }
623
624    /// The smoothing's response on energy at `hz`.
625    fn smoothed(&self, hz: f64) -> C64 {
626        let w = TAU * hz / self.sample_rate;
627        let back = C64::new(w.cos(), -w.sin()).scale(self.smoothing.a1);
628        (C64::real(self.smoothing.b0) / (C64::ONE + back)).powi(SMOOTHING.poles as u32)
629    }
630
631    /// What a lone modulation at `hz` keeps to first order: the smoothing's gain on energy
632    /// and the lowpass's for an envelope, the lowpass's alone for a centroid.
633    fn gain(&self, hz: f64, kind: Kind) -> f64 {
634        match kind {
635            Kind::Envelope => self.smoothed(hz).abs() * self.lowpass.gain(hz),
636            Kind::Centroid => self.lowpass.gain(hz),
637        }
638    }
639
640    /// `gain`'s least and most over `hz`: the smoothing's falls with hz, the lowpass's moves
641    /// no faster than its slope from the middle.
642    fn gains(&self, hz: (f64, f64), kind: Kind) -> (f64, f64) {
643        let (mid, half) = ((hz.0 + hz.1) / 2.0, (hz.1 - hz.0) / 2.0);
644        let pass = self.lowpass.gain(mid);
645        let moved = self.lowpass.slope() * half;
646        let (lo, hi) = (pass - moved, pass + moved);
647        match kind {
648            Kind::Envelope => (
649                self.smoothed(hz.1).abs() * lo,
650                self.smoothed(hz.0).abs() * hi,
651            ),
652            Kind::Centroid => (lo, hi),
653        }
654    }
655
656    /// V at `hz`: (1 - |H(f)|^2 + |H(2f) - H(f)^2|) / 2, H the smoothing on energy.
657    fn square_law(&self, hz: f64) -> f64 {
658        let (one, two) = (self.smoothed(hz), self.smoothed(2.0 * hz));
659        (1.0 - one.norm_sqr() + (two - one * one).abs()) / 2.0
660    }
661}
662
663/// The periodic Hann over `n` samples, its transform as three Dirichlet kernels: at `x` of
664/// its bins, sin(pi x) times this.
665fn dirichlet(x: f64, n: usize) -> C64 {
666    let n = n as f64;
667    let turn = C64::new((PI / n).cos(), (PI / n).sin());
668    let s = |y: f64| (PI * y / n).sin();
669    C64::real(0.5 / s(x)) - turn.conj().over(4.0 * s(x - 1.0)) - turn.over(4.0 * s(x + 1.0))
670}
671
672/// |W(x)| / W(0), W the periodic Hann's transform over `n` samples at `x` of its bins: exact.
673fn kernel(x: f64, n: usize) -> f64 {
674    match x.abs() {
675        0.0 => 1.0,
676        1.0 => 0.5,
677        x => 2.0 / n as f64 * (PI * x).sin().abs() * dirichlet(x, n).abs(),
678    }
679}
680
681/// `kernel` with |sin(pi x)| taken as 1: over it, and falling, for 1 < x < n / 2.
682fn reach(x: f64, n: usize) -> f64 {
683    2.0 / n as f64 * dirichlet(x, n).abs()
684}
685
686/// The u in [-q, q] where kernel(q - u) / kernel(u) is `ratio`, by bisection; that ratio rises
687/// with u, so a ratio past either end gives that end.
688fn offset(ratio: f64, q: f64, n: usize) -> f64 {
689    let (mut lo, mut hi) = (-q, q);
690    for _ in 0..60 {
691        let mid = (lo + hi) / 2.0;
692        match kernel(q - mid, n) / kernel(mid, n) < ratio {
693            true => lo = mid,
694            false => hi = mid,
695        }
696    }
697    (lo + hi) / 2.0
698}
699
700fn weighted_mean(w: &[f64], x: &[f64]) -> f64 {
701    w.iter().zip(x).map(|(h, v)| h * v).sum::<f64>() / w.iter().sum::<f64>()
702}
703
704fn summary(windows: &[Window]) -> Summary {
705    let leads = || {
706        windows
707            .iter()
708            .filter_map(|w| w.peaks.first().map(|p| (w.t_secs, *p)))
709    };
710    let centroids = || {
711        windows
712            .iter()
713            .filter_map(|w| w.centroid.as_ref().map(|c| (w.t_secs, c)))
714    };
715    Summary {
716        windows_with_peak: leads().count(),
717        hz: stats(leads().map(|(t, p)| (t, p.hz))),
718        depth: stats(leads().map(|(t, p)| (t, p.depth))),
719        centroid_hz: stats(centroids().map(|(t, c)| (t, c.mean_hz))),
720        centroid_depth_hz: stats(
721            centroids().filter_map(|(t, c)| c.peaks.first().map(|p| (t, p.depth))),
722        ),
723    }
724}
725
726#[cfg(test)]
727mod tests {
728    use super::*;
729
730    /// The closed form against the transform summed sample by sample; `reach` over it and
731    /// falling on (1, n/2); V rising with f to `MAX_HZ`: what MODEL's bounds lean on.
732    #[test]
733    fn the_kernel_its_reach_and_the_square_law_hold_as_model_states() {
734        for n in [250, 400, 401] {
735            let w = hann_periodic(n);
736            let total: f64 = w.iter().sum();
737            for x in [0.0, 0.01, 0.39, 0.5, 1.0, 1.7, 2.3, 7.5, 61.2] {
738                let summed = w.iter().enumerate().fold(C64::ZERO, |acc, (k, h)| {
739                    let turn = -TAU * x * k as f64 / n as f64;
740                    acc + C64::new(turn.cos(), turn.sin()).scale(*h)
741                });
742                assert!(
743                    (kernel(x, n) - summed.abs() / total).abs() < 1e-12,
744                    "{n} {x}"
745                );
746            }
747            let ys: Vec<f64> = (1..(n * 5)).map(|k| 1.0 + k as f64 / 10.0).collect();
748            for pair in ys.windows(2).filter(|p| p[1] < n as f64 / 2.0) {
749                assert!(reach(pair[1], n) < reach(pair[0], n), "{n} {pair:?}");
750                assert!(kernel(pair[1], n) <= reach(pair[1], n) * (1.0 + 1e-12));
751            }
752        }
753        let lowpass = Lowpass::new(2000.0);
754        for sample_rate in [8_192.0, 16_000.0, 44_100.0, 48_000.0, 96_000.0] {
755            let read = Read {
756                step: 1.0,
757                bins: 1..2,
758                n: 2,
759                bpm: None,
760                size: 1,
761                rate: 100.0,
762                sample_rate,
763                smoothing: design(Shape::OnePole, SMOOTHING_HZ, 0.0, 0.0, sample_rate),
764                lowpass: &lowpass,
765            };
766            let v: Vec<f64> = (1..=2000)
767                .map(|k| read.square_law(k as f64 * 0.01))
768                .collect();
769            assert!(v.windows(2).all(|p| p[1] > p[0]), "{sample_rate}");
770        }
771    }
772}