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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// False positives with NonZeroUsize conversions
// False positives with NonZeroUsize conversions
// False positives with ndarray indexing
// False positives with PyO3
// False positives with PyO3 (likes of __repr__ and any pymethod requires &self)
//! # Spectrograms - FFT-Based Computations
//!
//! High-performance FFT-based computations for audio and image processing.
//!
//! # Overview
//!
//! This library provides:
//! - **1D FFTs**: For time-series and audio signals
//! - **2D FFTs**: For images and spatial data
//! - **Spectrograms**: Time-frequency representations (STFT, Mel, ERB, CQT)
//! - **Image operations**: Convolution, filtering, edge detection
//! - **Two backends**: `RealFFT` (pure Rust) or FFTW (fastest)
//! - **Plan-based API**: Reusable plans for batch processing
//!
//! # Domain Organization
//!
//! The library is organized by application domain:
//!
//! - [`audio`] - Audio processing (spectrograms, MFCC, chroma, pitch analysis)
//! - [`image`] - Image processing (convolution, filtering, frequency analysis)
//! - [`mod@fft`] - Core FFT operations (1D and 2D transforms)
//!
//! All functionality is also exported at the crate root for convenience.
//!
//! # Audio Processing
//!
//! Compute various types of spectrograms:
//! - Linear-frequency spectrograms
//! - Mel-frequency spectrograms
//! - ERB spectrograms
//! - Logarithmic-frequency spectrograms
//! - CQT (Constant-Q Transform)
//!
//! With multiple amplitude scales:
//! - Power (`|X|²`)
//! - Magnitude (`|X|`)
//! - Decibels (`10·log₁₀(power)`)
//!
//! # Image Processing
//!
//! Frequency-domain operations for images:
//! - 2D FFT and inverse FFT
//! - Convolution via FFT (faster for large kernels)
//! - Spatial filtering (low-pass, high-pass, band-pass)
//! - Edge detection
//! - Sharpening and blurring
//!
//! # Features
//!
//! - **Two FFT backends**: `RealFFT` (default, pure Rust) or FFTW (fastest performance)
//! - **Plan-based computation**: Reuse FFT plans for efficient batch processing
//! - **Comprehensive window functions**: Hanning, Hamming, Blackman, Kaiser, Gaussian, etc.
//! - **Type-safe API**: Compile-time guarantees for spectrogram types
//!
//! # Quick Start
//!
//! ## Audio: Compute a Mel Spectrogram
//!
//! ```
//! use spectrograms::*;
//! use std::f64::consts::PI;
//! use non_empty_slice::NonEmptyVec;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Generate a sine wave at 440 Hz
//! let sample_rate = 16000.0;
//! let samples_vec: Vec<f64> = (0..16000)
//! .map(|i| (2.0 * PI * 440.0 * i as f64 / sample_rate).sin())
//! .collect();
//! let samples = NonEmptyVec::new(samples_vec).unwrap();
//!
//! // Set up parameters
//! let stft = StftParams::new(nzu!(512), nzu!(256), WindowType::Hanning, true)?;
//! let params = SpectrogramParams::new(stft, sample_rate)?;
//! let mel = MelParams::new(nzu!(80), 0.0, 8000.0)?;
//!
//! // Compute Mel spectrogram
//! let spec = MelPowerSpectrogram::compute(samples.as_ref(), ¶ms, &mel, None)?;
//! println!("Computed {} bins x {} frames", spec.n_bins(), spec.n_frames());
//! # Ok(())
//! # }
//! ```
//!
//! ## Image: Apply Gaussian Blur via FFT
//!
//! ```
//! use spectrograms::image_ops::*;
//! use spectrograms::nzu;
//! use ndarray::Array2;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Create a 256x256 image
//! let image = Array2::<f64>::from_shape_fn((256, 256), |(i, j)| {
//! ((i as f64 - 128.0).powi(2) + (j as f64 - 128.0).powi(2)).sqrt()
//! });
//!
//! // Apply Gaussian blur
//! let kernel = gaussian_kernel_2d(nzu!(9), 2.0)?;
//! let blurred = convolve_fft(&image.view(), &kernel.view())?;
//! # Ok(())
//! # }
//! ```
//!
//! ## General: 2D FFT
//!
//! ```
//! use spectrograms::fft2d::*;
//! use ndarray::Array2;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let data = Array2::<f64>::zeros((128, 128));
//! let spectrum = fft2d(&data.view())?;
//! let power = power_spectrum_2d(&data.view())?;
//! # Ok(())
//! # }
//! ```
//!
//! # Feature Flags
//!
//! The library requires exactly one FFT backend:
//!
//! - `realfft` (default): Pure-Rust FFT implementation, no system dependencies
//! - `fftw`: Uses FFTW C library for fastest performance (requires system install)
//!
//! # Examples
//!
//! ## Mel Spectrogram
//!
//! ```
//! use spectrograms::*;
//! use non_empty_slice::non_empty_vec;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let samples = non_empty_vec![0.0; nzu!(16000)];
//!
//! let stft = StftParams::new(nzu!(512), nzu!(256), WindowType::Hanning, true)?;
//! let params = SpectrogramParams::new(stft, 16000.0)?;
//! let mel = MelParams::new(nzu!(80), 0.0, 8000.0)?;
//! let db = LogParams::new(-80.0)?;
//!
//! let spec = MelDbSpectrogram::compute(samples.as_ref(), ¶ms, &mel, Some(&db))?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Efficient Batch Processing
//!
//! ```
//! use spectrograms::*;
//! use non_empty_slice::non_empty_vec;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let signals = vec![non_empty_vec![0.0; nzu!(16000)], non_empty_vec![0.0; nzu!(16000)]];
//!
//! let stft = StftParams::new(nzu!(512), nzu!(256), WindowType::Hanning, true)?;
//! let params = SpectrogramParams::new(stft, 16000.0)?;
//!
//! // Create plan once, reuse for all signals
//! let planner = SpectrogramPlanner::new();
//! let mut plan = planner.linear_plan::<Power>(¶ms, None)?;
//!
//! for signal in &signals {
//! let spec = plan.compute(&signal)?;
//! // Process spec...
//! }
//! # Ok(())
//! # }
//! ```
// ============================================================================
// Domain-Specific Module Organization
// ============================================================================
/// Audio processing utilities (spectrograms, MFCC, chroma, etc.)
///
/// This module contains all audio-related functionality:
/// - Spectrogram computation (Linear, Mel, ERB, CQT)
/// - MFCC (Mel-Frequency Cepstral Coefficients)
/// - Chromagram (pitch class profiles)
/// - Window functions
///
/// # Examples
///
/// ```
/// use spectrograms::{nzu, audio::*};
/// use non_empty_slice::non_empty_vec;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let samples = non_empty_vec![0.0; nzu!(16000)];
/// let stft = StftParams::new(nzu!(512), nzu!(256), WindowType::Hanning, true)?;
/// let params = SpectrogramParams::new(stft, 16000.0)?;
/// let spec = LinearPowerSpectrogram::compute(&samples, ¶ms, None)?;
/// # Ok(())
/// # }
/// ```
/// Image processing utilities (convolution, filtering, etc.)
///
/// This module contains image processing operations using 2D FFTs:
/// - Convolution and correlation
/// - Spatial filtering (low-pass, high-pass, band-pass)
/// - Edge detection
/// - Sharpening and blurring
///
/// # Examples
///
/// ```
/// use spectrograms::image::*;
/// use spectrograms::nzu;
/// use ndarray::Array2;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let image = Array2::<f64>::zeros((128, 128));
/// let kernel = gaussian_kernel_2d(nzu!(5), 1.0)?;
/// let blurred = convolve_fft(&image.view(), &kernel.view())?;
/// # Ok(())
/// # }
/// ```
/// Core FFT operations (1D and 2D)
///
/// This module provides direct access to FFT functions:
/// - 1D FFT: `fft()`, `rfft()`, `irfft()`
/// - 2D FFT: `fft2d()`, `ifft2d()`
/// - STFT: `stft()`, `istft()`
/// - Power/magnitude spectra
///
/// # Examples
///
/// ```
/// use spectrograms::{nzu, fft::*};
/// use ndarray::Array2;
/// use non_empty_slice::non_empty_vec;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // 1D FFT
/// let signal = non_empty_vec![0.0; nzu!(1024)];
/// let spectrum = rfft(&signal, nzu!(1024))?;
///
/// // 2D FFT
/// let image = Array2::<f64>::zeros((128, 128));
/// let spectrum_2d = fft2d(&image.view())?;
/// # Ok(())
/// # }
/// ```
// Re-export everything at top level for backward compatibility
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use *;
pub use *;
pub use ;
pub use *;
pub use ;
compile_error!;
compile_error!;
pub use *;
pub use *;
/// Python module definition for `PyO3`.
///
/// This module is only available when the `python` feature is enabled.
use *;
pub