Skip to main content

rlx_ir/ops/
spectral.rs

1// RLX — versatile ML compiler + runtime.
2// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5//! Spectral front-ends for EEG models — windowed spectrogram, per-band power,
6//! and per-band differential entropy — composed on the existing `rfft` helper.
7//!
8//! These are the tokenizer/feature stages that BrainBERT, BIOT,
9//! SleepTransformer, CBraMod, Uni-NTFM, FoME and MAET currently compute
10//! host-side in plain Rust. Expressed as graph subtrees they run on-device and
11//! are differentiable.
12//!
13//! Because `rfft` zero-pads the frame to the next power of two, the frequency
14//! axis has `next_pow2(frame_len)/2 + 1` bins.
15
16use crate::infer::GraphExt as _;
17use crate::op::Activation;
18use crate::{DType, Graph, NodeId, fft::FftNorm};
19
20/// Analysis window applied to each frame before the FFT.
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub enum WindowKind {
23    /// No window (boxcar).
24    Rectangular,
25    /// Hann (a.k.a. Hanning) — the usual EEG default.
26    Hann,
27    /// Hamming.
28    Hamming,
29    /// Blackman.
30    Blackman,
31}
32
33impl WindowKind {
34    fn coeffs(self, n: usize) -> Vec<f32> {
35        use std::f32::consts::PI;
36        if n == 1 {
37            return vec![1.0];
38        }
39        let d = (n - 1) as f32;
40        (0..n)
41            .map(|i| {
42                let t = i as f32 / d;
43                match self {
44                    WindowKind::Rectangular => 1.0,
45                    WindowKind::Hann => 0.5 - 0.5 * (2.0 * PI * t).cos(),
46                    WindowKind::Hamming => 0.54 - 0.46 * (2.0 * PI * t).cos(),
47                    WindowKind::Blackman => {
48                        0.42 - 0.5 * (2.0 * PI * t).cos() + 0.08 * (4.0 * PI * t).cos()
49                    }
50                }
51            })
52            .collect()
53    }
54}
55
56impl Graph {
57    /// Windowed short-time spectrogram of the last axis.
58    ///
59    /// `[..., T]` → `[n_frames, ..., n_bins]` where
60    /// `n_frames = 1 + (T − frame_len)/hop` and `n_bins = next_pow2(frame_len)/2 + 1`.
61    ///
62    /// * `power` — return `|X|²` (true) or magnitude `|X|` (false).
63    /// * `log`   — apply `log(· + 1e-8)` to the result (log-power / log-magnitude).
64    pub fn spectrogram(
65        &mut self,
66        x: NodeId,
67        frame_len: usize,
68        hop: usize,
69        window: WindowKind,
70        power: bool,
71        log: bool,
72    ) -> NodeId {
73        assert!(frame_len > 0 && hop > 0, "spectrogram: frame_len/hop > 0");
74        let shape = self.shape(x).clone();
75        let last = shape.rank() - 1;
76        let t = shape.dim(last).unwrap_static();
77        assert!(t >= frame_len, "spectrogram: T {t} < frame_len {frame_len}");
78        let n_frames = 1 + (t - frame_len) / hop;
79
80        // Stack time-domain frames into [n_frames, ..., frame_len].
81        let mut rows = Vec::with_capacity(n_frames);
82        for f in 0..n_frames {
83            let start = f * hop;
84            let frame = self.narrow_(x, last, start, frame_len);
85            let mut dims: Vec<i64> = self
86                .shape(frame)
87                .dims()
88                .iter()
89                .map(|d| d.unwrap_static() as i64)
90                .collect();
91            dims.insert(0, 1);
92            rows.push(self.reshape_(frame, dims));
93        }
94        let framed = if rows.len() == 1 {
95            rows.pop().unwrap()
96        } else {
97            self.concat_(rows, 0)
98        };
99
100        // Apply the analysis window (broadcast over the frame axis).
101        let framed = if window == WindowKind::Rectangular {
102            framed
103        } else {
104            let w = self.const_f32_tensor(window.coeffs(frame_len), &[frame_len]);
105            self.mul(framed, w)
106        };
107
108        let (re, im) = self.rfft(framed, FftNorm::Backward);
109        let re2 = self.mul(re, re);
110        let im2 = self.mul(im, im);
111        let mag2 = self.add(re2, im2);
112        let out = if power { mag2 } else { self.sqrt(mag2) };
113        if log { self.log_eps(out, 1e-8) } else { out }
114    }
115
116    /// Per-band power of the last axis via a single `rfft`.
117    ///
118    /// `[..., T]` → `[..., n_bands]`. Each band `(lo, hi)` in Hz sums the
119    /// (unnormalized) power `|X|²` over the rFFT bins whose center frequency
120    /// falls in `[lo, hi]`, using `sample_rate` to map Hz → bin index.
121    pub fn band_power(&mut self, x: NodeId, sample_rate: f32, bands: &[(f32, f32)]) -> NodeId {
122        assert!(!bands.is_empty(), "band_power: need ≥1 band");
123        let shape = self.shape(x).clone();
124        let last = shape.rank() - 1;
125        let t = shape.dim(last).unwrap_static();
126        let n_pad = crate::fft::next_pow2(t);
127        let n_bins = n_pad / 2 + 1;
128
129        let (re, im) = self.rfft(x, FftNorm::Backward);
130        let re2 = self.mul(re, re);
131        let im2 = self.mul(im, im);
132        let power = self.add(re2, im2); // [.., n_bins]
133        let plast = self.shape(power).rank() - 1;
134
135        let hz_per_bin = sample_rate / n_pad as f32;
136        let mut cols = Vec::with_capacity(bands.len());
137        for &(lo, hi) in bands {
138            let mut b0 = (lo / hz_per_bin).ceil() as isize;
139            let mut b1 = (hi / hz_per_bin).floor() as isize;
140            b0 = b0.clamp(0, n_bins as isize - 1);
141            b1 = b1.clamp(0, n_bins as isize - 1);
142            let (b0, b1) = if b1 < b0 { (b0, b0) } else { (b0, b1) };
143            let seg = self.narrow_(power, plast, b0 as usize, (b1 - b0 + 1) as usize);
144            cols.push(self.sum(seg, vec![plast], true)); // [.., 1]
145        }
146        if cols.len() == 1 {
147            cols.pop().unwrap()
148        } else {
149            self.concat_(cols, plast)
150        }
151    }
152
153    /// Per-band differential entropy: `0.5·log(2πe · band_power + 1e-8)`.
154    ///
155    /// For a band-limited Gaussian signal this equals its differential entropy
156    /// up to the constant — the classic EEG "DE" feature (SEED, MAET, FoME).
157    /// Returns `[..., n_bands]`.
158    pub fn differential_entropy(
159        &mut self,
160        x: NodeId,
161        sample_rate: f32,
162        bands: &[(f32, f32)],
163    ) -> NodeId {
164        let bp = self.band_power(x, sample_rate, bands);
165        // 2πe
166        let c = self.constant(2.0 * std::f64::consts::PI * std::f64::consts::E, DType::F32);
167        let scaled = self.mul(bp, c);
168        let logv = self.log_eps(scaled, 1e-8);
169        let half = self.constant(0.5, DType::F32);
170        self.mul(logv, half)
171    }
172
173    fn log_eps(&mut self, x: NodeId, eps: f32) -> NodeId {
174        let e = self.constant(eps as f64, DType::F32);
175        let shifted = self.add(x, e);
176        let s = crate::shape::unary_shape(self.shape(shifted));
177        self.activation(Activation::Log, shifted, s)
178    }
179}
180
181#[cfg(test)]
182mod tests {
183    use super::*;
184    use crate::Shape;
185
186    fn dims(g: &Graph, id: NodeId) -> Vec<usize> {
187        g.shape(id)
188            .dims()
189            .iter()
190            .map(|d| d.unwrap_static())
191            .collect()
192    }
193    fn input(g: &mut Graph, shape: &[usize]) -> NodeId {
194        g.input("x", Shape::new(shape, DType::F32))
195    }
196
197    #[test]
198    fn spectrogram_shape() {
199        let mut g = Graph::new("spec");
200        let x = input(&mut g, &[2, 256]); // [C, T]
201        // frame_len=64 (pow2) → n_bins = 64/2+1 = 33; n_frames = 1+(256-64)/32 = 7
202        let y = g.spectrogram(x, 64, 32, WindowKind::Hann, true, true);
203        assert_eq!(dims(&g, y), vec![7, 2, 33]);
204    }
205
206    #[test]
207    fn band_power_shape() {
208        let mut g = Graph::new("bp");
209        let x = input(&mut g, &[4, 512]); // [C, T]
210        let bands = [
211            (0.5, 4.0),
212            (4.0, 8.0),
213            (8.0, 13.0),
214            (13.0, 30.0),
215            (30.0, 45.0),
216        ];
217        let y = g.band_power(x, 128.0, &bands);
218        assert_eq!(dims(&g, y), vec![4, 5]);
219    }
220
221    #[test]
222    fn differential_entropy_shape() {
223        let mut g = Graph::new("de");
224        let x = input(&mut g, &[4, 512]);
225        let bands = [(1.0, 4.0), (4.0, 8.0), (8.0, 14.0)];
226        let y = g.differential_entropy(x, 200.0, &bands);
227        assert_eq!(dims(&g, y), vec![4, 3]);
228    }
229}