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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Spectral flux computation kernels.
//!
//! This module provides low-level functions for computing spectral flux—the rate of change in
//! a signal's frequency spectrum over time. Spectral flux is a fundamental feature for onset
//! detection, as sudden increases in spectral energy or magnitude typically correspond to
//! note onsets, percussive hits, or other transient events.
//!
//! Four spectral flux variants are provided, each with different characteristics:
//! - **Energy flux**: Emphasizes large amplitude changes (quadratic sensitivity)
//! - **Magnitude flux**: Linear sensitivity to amplitude changes
//! - **Complex flux**: Incorporates both magnitude and phase information
//! - **Rectified complex flux**: Focuses on magnitude increases with phase awareness
//!
//! All flux functions operate on 2D spectrograms with shape `(frequency_bins, frames)` and
//! return a 1D time series of flux values, one per frame. The first frame always has zero flux.
use Array2;
use NonEmptyVec;
use Complex;
/// Computes energy-based spectral flux.
///
/// Calculates the sum of positive energy differences across all frequency bins:
/// `flux[t] = Σ max(0, |X[b,t]|² - |X[b,t-1]|²)` for all bins b.
///
/// Energy flux emphasizes large amplitude changes due to squaring, making it particularly
/// effective for detecting percussive onsets with sharp transients.
///
/// # Arguments
///
/// * `mag` — 2D magnitude spectrogram with shape `(frequency_bins, frames)`
///
/// # Returns
///
/// Flux values for each frame. The first frame is always 0.0 (no previous frame to compare).
///
/// # Example
///
/// ```rust,ignore
/// use ndarray::Array2;
/// use audio_samples::operations::onset::flux::energy_flux;
///
/// let mag = Array2::from_shape_vec((128, 100), vec![0.0; 12800]).unwrap();
/// let flux = energy_flux(&mag);
/// assert_eq!(flux.len().get(), 100);
/// assert_eq!(flux[0], 0.0);
/// ```
/// Computes magnitude-based spectral flux.
///
/// Calculates the sum of absolute magnitude differences across all frequency bins:
/// `flux[t] = Σ |mag[b,t] - mag[b,t-1]|` for all bins b.
///
/// Magnitude flux has linear sensitivity to amplitude changes, making it more sensitive to
/// subtle onsets compared to energy flux. Good for tonal instruments with gradual attacks.
///
/// # Arguments
///
/// * `mag` — 2D magnitude spectrogram with shape `(frequency_bins, frames)`
///
/// # Returns
///
/// Flux values for each frame. The first frame is always 0.0 (no previous frame to compare).
///
/// # Example
///
/// ```rust,ignore
/// use ndarray::Array2;
/// use audio_samples::operations::onset::flux::magnitude_flux;
///
/// let mag = Array2::from_shape_vec((128, 100), vec![0.0; 12800]).unwrap();
/// let flux = magnitude_flux(&mag);
/// assert_eq!(flux.len().get(), 100);
/// ```
/// Computes complex domain spectral flux using magnitude and phase.
///
/// Calculates the Euclidean distance between consecutive complex spectra:
/// `flux[t] = Σ |X[b,t] - X[b,t-1]|` for all bins b, where X is complex-valued.
///
/// Complex flux incorporates both magnitude and phase changes, providing the most complete
/// measure of spectral change. More computationally expensive but most accurate for
/// polyphonic music with complex timbres.
///
/// # Arguments
///
/// * `spec` — 2D complex spectrogram with shape `(frequency_bins, frames)`
///
/// # Returns
///
/// Flux values for each frame. The first frame is always 0.0 (no previous frame to compare).
///
/// # Example
///
/// ```rust,ignore
/// use ndarray::Array2;
/// use num_complex::Complex;
/// use audio_samples::operations::onset::flux::complex_flux;
///
/// let spec = Array2::from_elem((128, 100), Complex::new(0.0, 0.0));
/// let flux = complex_flux(&spec);
/// assert_eq!(flux.len().get(), 100);
/// ```
/// Computes rectified complex domain spectral flux.
///
/// Calculates the sum of positive magnitude differences:
/// `flux[t] = Σ max(0, |X[b,t]| - |X[b,t-1]|)` for all bins b.
///
/// This variant considers phase information when computing the norm but only keeps positive
/// magnitude changes. Balances the robustness of complex methods with the interpretability
/// of rectified magnitude-based approaches.
///
/// # Arguments
///
/// * `spec` — 2D complex spectrogram with shape `(frequency_bins, frames)`
///
/// # Returns
///
/// Flux values for each frame. The first frame is always 0.0 (no previous frame to compare).
///
/// # Example
///
/// ```rust,ignore
/// use ndarray::Array2;
/// use num_complex::Complex;
/// use audio_samples::operations::onset::flux::rectified_complex_flux;
///
/// let spec = Array2::from_elem((128, 100), Complex::new(1.0, 0.0));
/// let flux = rectified_complex_flux(&spec);
/// assert_eq!(flux.len().get(), 100);
/// ```