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: (planes, 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`. Every \
56                         channel's band energy averaged. 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    planes: &[&[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    let held = crate::measure::held(planes);
229    sliding::fits(held, sample_rate, window_secs, 1.0 / MIN_HZ)?;
230    let split = bands::smoothed(planes, sample_rate, start_secs, SMOOTHING);
231    let lowpass = Lowpass::new(split.rate_hz);
232    let groups = grouped(&split, &lowpass);
233    let rate = split.rate_hz / lowpass.step as f64;
234    let len = groups.first().map_or(0, |g| g.centroid.len());
235    let grid = sliding::spans(
236        held,
237        sample_rate,
238        start_secs,
239        hop_secs,
240        rate,
241        window_secs,
242        len,
243    );
244    let size = (grid.window_secs * rate).round() as usize;
245    let n = (2 * size).next_power_of_two();
246    let step = rate / n as f64;
247    let read = Read {
248        step,
249        bins: 1..((MAX_HZ / step).ceil() as usize + 2).min(n / 2),
250        n,
251        bpm,
252        size,
253        rate,
254        sample_rate,
255        smoothing: design(Shape::OnePole, SMOOTHING_HZ, 0.0, 0.0, sample_rate),
256        lowpass: &lowpass,
257    };
258    let tracks = Group::ALL
259        .into_iter()
260        .zip(groups)
261        .map(|(group, held)| {
262            let windows: Vec<Window> = grid.spans.iter().map(|s| read.window(&held, s)).collect();
263            Track {
264                group,
265                centres_hz: held.centres_hz,
266                summary: summary(&windows),
267                windows,
268            }
269        })
270        .collect();
271    Ok(Modulation {
272        hop_secs: grid.hop_secs,
273        window_secs: grid.window_secs,
274        envelope_hz: rate,
275        bin_step_hz: step,
276        resolution_hz: 2.0 / grid.window_secs,
277        min_hz: MIN_HZ.max(2.0 / grid.window_secs),
278        max_hz: MAX_HZ,
279        bpm,
280        passband_gain_at_max_hz: read.gain(MAX_HZ, Kind::Envelope),
281        tracks,
282    })
283}
284
285/// A Blackman-windowed sinc, unit gain at 0 Hz, taken every `step` samples.
286struct Lowpass {
287    taps: Vec<f64>,
288    step: usize,
289    rate: f64,
290}
291
292impl Lowpass {
293    fn new(rate: f64) -> Lowpass {
294        let cut = LOWPASS_HZ / rate;
295        let half = HALF_TAPS as f64;
296        let raw: Vec<f64> = (0..=2 * HALF_TAPS)
297            .map(|i| {
298                let j = i as f64 - half;
299                let sinc = match j == 0.0 {
300                    true => 2.0 * cut,
301                    false => (TAU * cut * j).sin() / (PI * j),
302                };
303                let x = PI * j / half;
304                sinc * (0.42 + 0.5 * x.cos() + 0.08 * (2.0 * x).cos())
305            })
306            .collect();
307        let sum: f64 = raw.iter().sum();
308        Lowpass {
309            taps: raw.into_iter().map(|h| h / sum).collect(),
310            step: (rate / ENVELOPE_HZ).round().max(1.0) as usize,
311            rate,
312        }
313    }
314
315    /// Symmetric, so its response is real.
316    fn gain(&self, hz: f64) -> f64 {
317        self.taps
318            .iter()
319            .enumerate()
320            .map(|(i, h)| h * (TAU * hz * (i as f64 - HALF_TAPS as f64) / self.rate).cos())
321            .sum()
322    }
323
324    /// Over the gain's slope in hz: each tap's cosine turns no faster than its lag.
325    fn slope(&self) -> f64 {
326        self.taps
327            .iter()
328            .enumerate()
329            .map(|(i, h)| h.abs() * TAU * (i as f64 - HALF_TAPS as f64).abs() / self.rate)
330            .sum()
331    }
332
333    /// The most the output can swing for an input swinging over a unit range.
334    fn spread(&self) -> f64 {
335        self.taps.iter().map(|h| h.abs()).sum()
336    }
337
338    /// Centred on each kept sample, the taps past either end left out and the rest
339    /// renormalized, so a constant stays that constant to the last sample. NaN in reach
340    /// stays NaN.
341    fn apply(&self, x: &[f64]) -> Vec<f64> {
342        (0..x.len())
343            .step_by(self.step)
344            .map(|m| {
345                let lo = m.saturating_sub(HALF_TAPS);
346                let hi = (m + HALF_TAPS + 1).min(x.len());
347                let taps = &self.taps[lo + HALF_TAPS - m..hi + HALF_TAPS - m];
348                let sum: f64 = taps.iter().sum();
349                taps.iter().zip(&x[lo..hi]).map(|(h, v)| h * v).sum::<f64>() / sum
350            })
351            .collect()
352    }
353}
354
355/// One group's decimated band envelopes and centroid.
356struct Held {
357    centres_hz: Vec<f64>,
358    envelopes: Vec<Vec<f64>>,
359    /// NaN where the group holds no energy.
360    centroid: Vec<f64>,
361}
362
363fn grouped(split: &Bands, lowpass: &Lowpass) -> Vec<Held> {
364    let tracks = &split.bands;
365    let edge = |hz: f64| {
366        tracks
367            .iter()
368            .position(|b| b.centre_hz >= hz)
369            .unwrap_or(tracks.len())
370    };
371    let (low, mid) = (edge(SPLITS_HZ[0]), edge(SPLITS_HZ[1]));
372    let len = tracks.first().map_or(0, |b| b.rms.len());
373    [0..low, low..mid, mid..tracks.len()]
374        .into_iter()
375        .map(|range: Range<usize>| {
376            let held = &tracks[range];
377            let centroid: Vec<f64> = (0..len)
378                .map(|i| {
379                    let (weighted, sum) = held.iter().fold((0.0, 0.0), |(w, s), b| {
380                        (w + b.centre_hz * b.rms[i], s + b.rms[i])
381                    });
382                    match sum > 0.0 {
383                        true => weighted / sum,
384                        false => f64::NAN,
385                    }
386                })
387                .collect();
388            Held {
389                centres_hz: held.iter().map(|b| b.centre_hz).collect(),
390                envelopes: held.iter().map(|b| lowpass.apply(&b.rms)).collect(),
391                centroid: lowpass.apply(&centroid),
392            }
393        })
394        .collect()
395}
396
397struct Read<'a> {
398    step: f64,
399    /// From the first bin past 0 Hz to one past `MAX_HZ`.
400    bins: Range<usize>,
401    n: usize,
402    bpm: Option<f64>,
403    /// A whole window's envelope samples.
404    size: usize,
405    rate: f64,
406    sample_rate: f64,
407    smoothing: Coeffs,
408    lowpass: &'a Lowpass,
409}
410
411/// An envelope's depth is an index under the smoothing's square law; a centroid's is Hz.
412#[derive(Clone, Copy, Debug, PartialEq, Eq)]
413enum Kind {
414    Envelope,
415    Centroid,
416}
417
418/// Each round of `Read::bounded` tightens the index's ceiling the last one bounded.
419const ROUNDS: usize = 8;
420
421impl Read<'_> {
422    fn window(&self, held: &Held, s: &Span) -> Window {
423        let w = hann_periodic(s.to - s.from);
424        let total: f64 = w.iter().sum();
425        let lowest = MIN_HZ.max(2.0 / (s.to_secs - s.from_secs));
426        let segments: Vec<&[f64]> = held.envelopes.iter().map(|e| &e[s.from..s.to]).collect();
427        let means: Vec<f64> = segments.iter().map(|e| weighted_mean(&w, e)).collect();
428        let level: f64 = means.iter().sum();
429        let peaks = match level > 0.0 {
430            true => {
431                let mut depth = vec![0.0; self.bins.len()];
432                for spectrum in self.magnitudes(&w, &segments, &means) {
433                    for (d, m) in depth.iter_mut().zip(spectrum) {
434                        *d += 2.0 * m / (total * level);
435                    }
436                }
437                self.peaks(&depth, lowest, s, Kind::Envelope)
438            }
439            false => Vec::new(),
440        };
441        let track = &held.centroid[s.from..s.to];
442        let centroid = (!track.iter().any(|c| c.is_nan())).then(|| {
443            let mean_hz = weighted_mean(&w, track);
444            let spectrum = self.magnitudes(&w, &[track], &[mean_hz]).remove(0);
445            let depth: Vec<f64> = spectrum.into_iter().map(|m| 2.0 * m / total).collect();
446            Centroid {
447                mean_hz,
448                peaks: self.peaks(&depth, lowest, s, Kind::Centroid),
449            }
450        });
451        Window {
452            t_secs: s.t_secs,
453            from_secs: s.from_secs,
454            to_secs: s.to_secs,
455            level,
456            peaks,
457            centroid,
458        }
459    }
460
461    /// |DFT| of each windowed, mean-removed segment at `bins`; two segments share one complex
462    /// transform as its real and imaginary parts.
463    fn magnitudes(&self, w: &[f64], segments: &[&[f64]], means: &[f64]) -> Vec<Vec<f64>> {
464        let fill = |out: &mut [f64], at: usize| {
465            for ((o, x), h) in out.iter_mut().zip(segments[at]).zip(w) {
466                *o = h * (x - means[at]);
467            }
468        };
469        let mut out = Vec::with_capacity(segments.len());
470        for pair in (0..segments.len()).step_by(2) {
471            let (mut re, mut im) = (vec![0.0; self.n], vec![0.0; self.n]);
472            fill(&mut re, pair);
473            let both = pair + 1 < segments.len();
474            if both {
475                fill(&mut im, pair + 1);
476            }
477            fft(&mut re, &mut im);
478            let half = |sign: f64| -> Vec<f64> {
479                self.bins
480                    .clone()
481                    .map(|k| {
482                        let (r, i) = (re[self.n - k], -im[self.n - k]);
483                        (re[k] + sign * r).hypot(im[k] + sign * i) / 2.0
484                    })
485                    .collect()
486            };
487            out.push(half(1.0));
488            if both {
489                out.push(half(-1.0));
490            }
491        }
492        out
493    }
494
495    /// `depth` is indexed from `bins.start`: the three local maxima whose bins read deepest,
496    /// each located as MODEL states, the deepest first.
497    fn peaks(&self, depth: &[f64], lowest: f64, s: &Span, kind: Kind) -> Vec<Peak> {
498        let first = self.bins.start;
499        let mut found: Vec<usize> = (1..depth.len().saturating_sub(1))
500            .filter(|&i| {
501                let hz = (first + i) as f64 * self.step;
502                (lowest..=MAX_HZ).contains(&hz)
503                    && depth[i] > depth[i - 1]
504                    && depth[i] >= depth[i + 1]
505            })
506            .collect();
507        found.sort_by(|a, b| depth[*b].total_cmp(&depth[*a]).then(a.cmp(b)));
508        let mut peaks: Vec<Peak> = found
509            .into_iter()
510            .take(PEAKS)
511            .map(|i| self.located(depth, i, s.to - s.from, kind))
512            .collect();
513        peaks.sort_by(|a, b| b.depth.total_cmp(&a.depth).then(a.hz.total_cmp(&b.hz)));
514        peaks
515    }
516
517    /// The Hann kernel inverted through the deepest bin and its deeper neighbour, and MODEL's
518    /// bounds where the window holds its whole span.
519    fn located(&self, depth: &[f64], i: usize, size: usize, kind: Kind) -> Peak {
520        let q = size as f64 / self.n as f64;
521        let x = (self.bins.start + i) as f64 * q;
522        let side = match depth[i + 1] >= depth[i - 1] {
523            true => 1.0,
524            false => -1.0,
525        };
526        let d0 = depth[i];
527        let ratio = depth[(i as f64 + side) as usize] / d0;
528        let u = offset(ratio, q, size);
529        let hz = (x + side * u) * self.rate / size as f64;
530        let bounds = match size == self.size {
531            true => self.bounded(d0, ratio, x, side, size, kind),
532            false => None,
533        };
534        let beats = |hz: f64| self.bpm.map(|bpm| bpm / (60.0 * hz));
535        Peak {
536            hz,
537            hz_bounds: bounds.map(|b| b.0),
538            period_beats: beats(hz),
539            period_beats_bounds: bounds.and_then(|((lo, hi), _)| beats(hi).zip(beats(lo))),
540            depth: d0 / kernel(u, size) / self.gain(hz, kind),
541            depth_bounds: bounds.map(|b| b.1),
542        }
543    }
544
545    /// MODEL's (hz, depth) bounds for a peak at natural bin `x` reading `d0`, its neighbour on
546    /// `side` reading `ratio` of that; `None` where no round can bound it.
547    fn bounded(
548        &self,
549        d0: f64,
550        ratio: f64,
551        x: f64,
552        side: f64,
553        size: usize,
554        kind: Kind,
555    ) -> Option<((f64, f64), (f64, f64))> {
556        let q = size as f64 / self.n as f64;
557        let to_hz = |nu: f64| nu * self.rate / size as f64;
558        let kappa = reach(x - q, size);
559        let lambda = reach(2.0 * (x - q), size) + 2.0 * kappa * kappa;
560        let spread = self.lowpass.spread();
561        let v = match kind {
562            Kind::Envelope => self.square_law(to_hz(x + q)),
563            Kind::Centroid => 0.0,
564        };
565        let law = |m: f64| {
566            let tight = match m < 1.0 {
567                true => m * m * v / (2.0 * (1.0 - m)),
568                false => f64::INFINITY,
569            };
570            tight.min(m * v.sqrt())
571        };
572        let square = |m: f64| (1.0 + 3.0 * kappa) * spread * law(m);
573        let mut m = match kind {
574            Kind::Envelope => {
575                let (lo, _) = self.gains((to_hz(x - q), to_hz(x + q)), kind);
576                let room = lo * kernel(q, size)
577                    - d0 * (kappa + spread * v.sqrt())
578                    - spread * lambda
579                    - square(1.0);
580                match room > 0.0 {
581                    true => (d0 / room).min(1.0),
582                    false => 1.0,
583                }
584            }
585            Kind::Centroid => f64::INFINITY,
586        };
587        let mut out = None;
588        for _ in 0..ROUNDS {
589            let (level, amp) = match kind {
590                Kind::Envelope => (m * kappa + spread * law(m), m * spread),
591                Kind::Centroid => (0.0, d0 / (kernel(q, size) - lambda)),
592            };
593            let e = amp * lambda + square(m);
594            let floor = d0 * (1.0 - level) - e;
595            if !(floor > 0.0 && amp > 0.0 && amp.is_finite()) {
596                break;
597            }
598            let wide = (1.0 + ratio) * e / floor;
599            let (ua, ub) = (offset(ratio - wide, q, size), offset(ratio + wide, q, size));
600            let near = match ua <= 0.0 && ub >= 0.0 {
601                true => 0.0,
602                false => ua.abs().min(ub.abs()),
603            };
604            let far = ua.abs().max(ub.abs());
605            let (ha, hb) = (to_hz(x + side * ua), to_hz(x + side * ub));
606            let hz = (ha.min(hb), ha.max(hb));
607            let g = self.gains(hz, kind);
608            let amps = (
609                floor / kernel(near, size),
610                (d0 * (1.0 + level) + e) / kernel(far, size),
611            );
612            let depth = (amps.0 / g.1, amps.1 / g.0);
613            if depth.0 > m {
614                return None;
615            }
616            out = Some((hz, (depth.0, depth.1.min(m))));
617            if kind == Kind::Centroid || depth.1 >= m {
618                break;
619            }
620            m = depth.1;
621        }
622        out
623    }
624
625    /// The smoothing's response on energy at `hz`.
626    fn smoothed(&self, hz: f64) -> C64 {
627        let w = TAU * hz / self.sample_rate;
628        let back = C64::new(w.cos(), -w.sin()).scale(self.smoothing.a1);
629        (C64::real(self.smoothing.b0) / (C64::ONE + back)).powi(SMOOTHING.poles as u32)
630    }
631
632    /// What a lone modulation at `hz` keeps to first order: the smoothing's gain on energy
633    /// and the lowpass's for an envelope, the lowpass's alone for a centroid.
634    fn gain(&self, hz: f64, kind: Kind) -> f64 {
635        match kind {
636            Kind::Envelope => self.smoothed(hz).abs() * self.lowpass.gain(hz),
637            Kind::Centroid => self.lowpass.gain(hz),
638        }
639    }
640
641    /// `gain`'s least and most over `hz`: the smoothing's falls with hz, the lowpass's moves
642    /// no faster than its slope from the middle.
643    fn gains(&self, hz: (f64, f64), kind: Kind) -> (f64, f64) {
644        let (mid, half) = ((hz.0 + hz.1) / 2.0, (hz.1 - hz.0) / 2.0);
645        let pass = self.lowpass.gain(mid);
646        let moved = self.lowpass.slope() * half;
647        let (lo, hi) = (pass - moved, pass + moved);
648        match kind {
649            Kind::Envelope => (
650                self.smoothed(hz.1).abs() * lo,
651                self.smoothed(hz.0).abs() * hi,
652            ),
653            Kind::Centroid => (lo, hi),
654        }
655    }
656
657    /// V at `hz`: (1 - |H(f)|^2 + |H(2f) - H(f)^2|) / 2, H the smoothing on energy.
658    fn square_law(&self, hz: f64) -> f64 {
659        let (one, two) = (self.smoothed(hz), self.smoothed(2.0 * hz));
660        (1.0 - one.norm_sqr() + (two - one * one).abs()) / 2.0
661    }
662}
663
664/// The periodic Hann over `n` samples, its transform as three Dirichlet kernels: at `x` of
665/// its bins, sin(pi x) times this.
666fn dirichlet(x: f64, n: usize) -> C64 {
667    let n = n as f64;
668    let turn = C64::new((PI / n).cos(), (PI / n).sin());
669    let s = |y: f64| (PI * y / n).sin();
670    C64::real(0.5 / s(x)) - turn.conj().over(4.0 * s(x - 1.0)) - turn.over(4.0 * s(x + 1.0))
671}
672
673/// |W(x)| / W(0), W the periodic Hann's transform over `n` samples at `x` of its bins: exact.
674fn kernel(x: f64, n: usize) -> f64 {
675    match x.abs() {
676        0.0 => 1.0,
677        1.0 => 0.5,
678        x => 2.0 / n as f64 * (PI * x).sin().abs() * dirichlet(x, n).abs(),
679    }
680}
681
682/// `kernel` with |sin(pi x)| taken as 1: over it, and falling, for 1 < x < n / 2.
683fn reach(x: f64, n: usize) -> f64 {
684    2.0 / n as f64 * dirichlet(x, n).abs()
685}
686
687/// The u in [-q, q] where kernel(q - u) / kernel(u) is `ratio`, by bisection; that ratio rises
688/// with u, so a ratio past either end gives that end.
689fn offset(ratio: f64, q: f64, n: usize) -> f64 {
690    let (mut lo, mut hi) = (-q, q);
691    for _ in 0..60 {
692        let mid = (lo + hi) / 2.0;
693        match kernel(q - mid, n) / kernel(mid, n) < ratio {
694            true => lo = mid,
695            false => hi = mid,
696        }
697    }
698    (lo + hi) / 2.0
699}
700
701fn weighted_mean(w: &[f64], x: &[f64]) -> f64 {
702    w.iter().zip(x).map(|(h, v)| h * v).sum::<f64>() / w.iter().sum::<f64>()
703}
704
705fn summary(windows: &[Window]) -> Summary {
706    let leads = || {
707        windows
708            .iter()
709            .filter_map(|w| w.peaks.first().map(|p| (w.t_secs, *p)))
710    };
711    let centroids = || {
712        windows
713            .iter()
714            .filter_map(|w| w.centroid.as_ref().map(|c| (w.t_secs, c)))
715    };
716    Summary {
717        windows_with_peak: leads().count(),
718        hz: stats(leads().map(|(t, p)| (t, p.hz))),
719        depth: stats(leads().map(|(t, p)| (t, p.depth))),
720        centroid_hz: stats(centroids().map(|(t, c)| (t, c.mean_hz))),
721        centroid_depth_hz: stats(
722            centroids().filter_map(|(t, c)| c.peaks.first().map(|p| (t, p.depth))),
723        ),
724    }
725}
726
727#[cfg(test)]
728mod tests {
729    use super::*;
730
731    /// The closed form against the transform summed sample by sample; `reach` over it and
732    /// falling on (1, n/2); V rising with f to `MAX_HZ`: what MODEL's bounds lean on.
733    #[test]
734    fn the_kernel_its_reach_and_the_square_law_hold_as_model_states() {
735        for n in [250, 400, 401] {
736            let w = hann_periodic(n);
737            let total: f64 = w.iter().sum();
738            for x in [0.0, 0.01, 0.39, 0.5, 1.0, 1.7, 2.3, 7.5, 61.2] {
739                let summed = w.iter().enumerate().fold(C64::ZERO, |acc, (k, h)| {
740                    let turn = -TAU * x * k as f64 / n as f64;
741                    acc + C64::new(turn.cos(), turn.sin()).scale(*h)
742                });
743                assert!(
744                    (kernel(x, n) - summed.abs() / total).abs() < 1e-12,
745                    "{n} {x}"
746                );
747            }
748            let ys: Vec<f64> = (1..(n * 5)).map(|k| 1.0 + k as f64 / 10.0).collect();
749            for pair in ys.windows(2).filter(|p| p[1] < n as f64 / 2.0) {
750                assert!(reach(pair[1], n) < reach(pair[0], n), "{n} {pair:?}");
751                assert!(kernel(pair[1], n) <= reach(pair[1], n) * (1.0 + 1e-12));
752            }
753        }
754        let lowpass = Lowpass::new(2000.0);
755        for sample_rate in [8_192.0, 16_000.0, 44_100.0, 48_000.0, 96_000.0] {
756            let read = Read {
757                step: 1.0,
758                bins: 1..2,
759                n: 2,
760                bpm: None,
761                size: 1,
762                rate: 100.0,
763                sample_rate,
764                smoothing: design(Shape::OnePole, SMOOTHING_HZ, 0.0, 0.0, sample_rate),
765                lowpass: &lowpass,
766            };
767            let v: Vec<f64> = (1..=2000)
768                .map(|k| read.square_law(k as f64 * 0.01))
769                .collect();
770            assert!(v.windows(2).all(|p| p[1] > p[0]), "{sample_rate}");
771        }
772    }
773}