scirs2-fft 0.4.2

Fast Fourier Transform module for SciRS2 (scirs2-fft)
Documentation
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! OxiFFT Plan Cache for performance optimization
//!
//! Caches OxiFFT plans to avoid expensive plan creation on repeated FFT calls.
//! Plans are stored in global caches keyed by size (1D) or dimensions (2D).
//!
//! This replaces the FFTW plan cache while maintaining Pure Rust Policy compliance.

// Allow complex type definitions for cache types - intentional for performance
#![allow(clippy::type_complexity)]

use oxifft::{Complex, Direction, Flags, Plan, Plan2D, R2rPlan, RealPlan, RealPlan2D};
// Use the internal R2rKind which has FFTW-compatible naming (Redft10, Redft01, etc.)
use oxifft::rdft::solvers::R2rKind;
use std::collections::HashMap;
use std::sync::Mutex;

use crate::error::{FFTError, FFTResult};

// ========================================
// 1D PLAN CACHES
// ========================================

/// Global cache for R2C (real-to-complex) plans, keyed by input size
static R2C_CACHE: Mutex<Option<HashMap<usize, RealPlan<f64>>>> = Mutex::new(None);

/// Global cache for C2C forward plans, keyed by size
static C2C_FWD_CACHE: Mutex<Option<HashMap<usize, Plan<f64>>>> = Mutex::new(None);

/// Global cache for C2C backward plans, keyed by size
static C2C_BWD_CACHE: Mutex<Option<HashMap<usize, Plan<f64>>>> = Mutex::new(None);

/// Global cache for C2R (complex-to-real) plans, keyed by output size
static C2R_CACHE: Mutex<Option<HashMap<usize, RealPlan<f64>>>> = Mutex::new(None);

/// Global cache for R2R DCT-II plans
static DCT2_CACHE: Mutex<Option<HashMap<usize, R2rPlan<f64>>>> = Mutex::new(None);

/// Global cache for R2R IDCT-II (DCT-III) plans
static IDCT2_CACHE: Mutex<Option<HashMap<usize, R2rPlan<f64>>>> = Mutex::new(None);

/// Global cache for R2R DST-II plans
static DST2_CACHE: Mutex<Option<HashMap<usize, R2rPlan<f64>>>> = Mutex::new(None);

/// Global cache for R2R IDST-II (DST-III) plans
static IDST2_CACHE: Mutex<Option<HashMap<usize, R2rPlan<f64>>>> = Mutex::new(None);

// ========================================
// 2D PLAN CACHES
// ========================================

/// Global cache for 2D R2C plans, keyed by (rows, cols)
static R2C_2D_CACHE: Mutex<Option<HashMap<(usize, usize), RealPlan2D<f64>>>> = Mutex::new(None);

/// Global cache for 2D C2C forward plans
static C2C_2D_FWD_CACHE: Mutex<Option<HashMap<(usize, usize), Plan2D<f64>>>> = Mutex::new(None);

/// Global cache for 2D C2C backward plans
static C2C_2D_BWD_CACHE: Mutex<Option<HashMap<(usize, usize), Plan2D<f64>>>> = Mutex::new(None);

/// Global cache for 2D C2R plans
static C2R_2D_CACHE: Mutex<Option<HashMap<(usize, usize), RealPlan2D<f64>>>> = Mutex::new(None);

// ========================================
// 1D PLAN EXECUTION FUNCTIONS
// ========================================

/// Execute R2C FFT with cached plan
pub fn execute_r2c(input: &[f64], output: &mut [Complex<f64>]) -> FFTResult<()> {
    let n = input.len();

    let mut cache = R2C_CACHE
        .lock()
        .map_err(|e| FFTError::ComputationError(format!("Failed to lock R2C cache: {}", e)))?;

    // Initialize cache if needed
    if cache.is_none() {
        *cache = Some(HashMap::new());
    }

    let cache_map = cache
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    // Create plan if not cached
    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(n) {
        let plan = RealPlan::r2c_1d(n, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!("Failed to create R2C plan for size {}", n))
        })?;
        e.insert(plan);
    }

    // Execute with cached plan
    let plan = cache_map
        .get(&n)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute_r2c(input, output);
    Ok(())
}

/// Execute C2C FFT with cached plan
pub fn execute_c2c(
    input: &[Complex<f64>],
    output: &mut [Complex<f64>],
    direction: Direction,
) -> FFTResult<()> {
    let n = input.len();

    let cache = match direction {
        Direction::Forward => &C2C_FWD_CACHE,
        Direction::Backward => &C2C_BWD_CACHE,
    };

    let mut cache_guard = cache.lock().map_err(|e| {
        FFTError::ComputationError(format!("Failed to lock C2C {:?} cache: {}", direction, e))
    })?;

    if cache_guard.is_none() {
        *cache_guard = Some(HashMap::new());
    }

    let cache_map = cache_guard
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(n) {
        let plan = Plan::dft_1d(n, direction, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!(
                "Failed to create C2C {:?} plan for size {}",
                direction, n
            ))
        })?;
        e.insert(plan);
    }

    let plan = cache_map
        .get(&n)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute(input, output);
    Ok(())
}

/// Execute C2R (inverse real) FFT with cached plan
pub fn execute_c2r(input: &[Complex<f64>], output: &mut [f64], n: usize) -> FFTResult<()> {
    let mut cache = C2R_CACHE
        .lock()
        .map_err(|e| FFTError::ComputationError(format!("Failed to lock C2R cache: {}", e)))?;

    if cache.is_none() {
        *cache = Some(HashMap::new());
    }

    let cache_map = cache
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(n) {
        let plan = RealPlan::c2r_1d(n, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!("Failed to create C2R plan for size {}", n))
        })?;
        e.insert(plan);
    }

    let plan = cache_map
        .get(&n)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute_c2r_unnormalized(input, output);
    Ok(())
}

/// Execute DCT-II with cached plan
pub fn execute_dct2(input: &[f64], output: &mut [f64]) -> FFTResult<()> {
    let n = input.len();

    let mut cache = DCT2_CACHE
        .lock()
        .map_err(|e| FFTError::ComputationError(format!("Failed to lock DCT2 cache: {}", e)))?;

    if cache.is_none() {
        *cache = Some(HashMap::new());
    }

    let cache_map = cache
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(n) {
        let plan = R2rPlan::r2r_1d(n, R2rKind::Redft10, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!("Failed to create DCT2 plan for size {}", n))
        })?;
        e.insert(plan);
    }

    let plan = cache_map
        .get(&n)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute(input, output);
    Ok(())
}

/// Execute IDCT-II (DCT-III) with cached plan
pub fn execute_idct2(input: &[f64], output: &mut [f64]) -> FFTResult<()> {
    let n = input.len();

    let mut cache = IDCT2_CACHE
        .lock()
        .map_err(|e| FFTError::ComputationError(format!("Failed to lock IDCT2 cache: {}", e)))?;

    if cache.is_none() {
        *cache = Some(HashMap::new());
    }

    let cache_map = cache
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(n) {
        let plan = R2rPlan::r2r_1d(n, R2rKind::Redft01, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!("Failed to create IDCT2 plan for size {}", n))
        })?;
        e.insert(plan);
    }

    let plan = cache_map
        .get(&n)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute(input, output);
    Ok(())
}

/// Execute DST-II with cached plan
pub fn execute_dst2(input: &[f64], output: &mut [f64]) -> FFTResult<()> {
    let n = input.len();

    let mut cache = DST2_CACHE
        .lock()
        .map_err(|e| FFTError::ComputationError(format!("Failed to lock DST2 cache: {}", e)))?;

    if cache.is_none() {
        *cache = Some(HashMap::new());
    }

    let cache_map = cache
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(n) {
        let plan = R2rPlan::r2r_1d(n, R2rKind::Rodft10, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!("Failed to create DST2 plan for size {}", n))
        })?;
        e.insert(plan);
    }

    let plan = cache_map
        .get(&n)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute(input, output);
    Ok(())
}

/// Execute IDST-II (DST-III) with cached plan
pub fn execute_idst2(input: &[f64], output: &mut [f64]) -> FFTResult<()> {
    let n = input.len();

    let mut cache = IDST2_CACHE
        .lock()
        .map_err(|e| FFTError::ComputationError(format!("Failed to lock IDST2 cache: {}", e)))?;

    if cache.is_none() {
        *cache = Some(HashMap::new());
    }

    let cache_map = cache
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(n) {
        let plan = R2rPlan::r2r_1d(n, R2rKind::Rodft01, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!("Failed to create IDST2 plan for size {}", n))
        })?;
        e.insert(plan);
    }

    let plan = cache_map
        .get(&n)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute(input, output);
    Ok(())
}

// ========================================
// 2D PLAN EXECUTION FUNCTIONS
// ========================================

/// Execute 2D R2C FFT with cached plan
pub fn execute_r2c_2d(
    input: &[f64],
    output: &mut [Complex<f64>],
    rows: usize,
    cols: usize,
) -> FFTResult<()> {
    let key = (rows, cols);

    let mut cache = R2C_2D_CACHE
        .lock()
        .map_err(|e| FFTError::ComputationError(format!("Failed to lock 2D R2C cache: {}", e)))?;

    if cache.is_none() {
        *cache = Some(HashMap::new());
    }

    let cache_map = cache
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(key) {
        let plan = RealPlan2D::r2c(rows, cols, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!(
                "Failed to create 2D R2C plan for size {}x{}",
                rows, cols
            ))
        })?;
        e.insert(plan);
    }

    let plan = cache_map
        .get(&key)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute_r2c(input, output);
    Ok(())
}

/// Execute 2D C2C FFT with cached plan
pub fn execute_c2c_2d(
    input: &[Complex<f64>],
    output: &mut [Complex<f64>],
    rows: usize,
    cols: usize,
    direction: Direction,
) -> FFTResult<()> {
    let key = (rows, cols);

    let cache = match direction {
        Direction::Forward => &C2C_2D_FWD_CACHE,
        Direction::Backward => &C2C_2D_BWD_CACHE,
    };

    let mut cache_guard = cache.lock().map_err(|e| {
        FFTError::ComputationError(format!(
            "Failed to lock 2D C2C {:?} cache: {}",
            direction, e
        ))
    })?;

    if cache_guard.is_none() {
        *cache_guard = Some(HashMap::new());
    }

    let cache_map = cache_guard
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(key) {
        let plan = Plan2D::new(rows, cols, direction, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!(
                "Failed to create 2D C2C {:?} plan for size {}x{}",
                direction, rows, cols
            ))
        })?;
        e.insert(plan);
    }

    let plan = cache_map
        .get(&key)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute(input, output);
    Ok(())
}

/// Execute 2D C2R FFT with cached plan
pub fn execute_c2r_2d(
    input: &[Complex<f64>],
    output: &mut [f64],
    rows: usize,
    cols: usize,
) -> FFTResult<()> {
    let key = (rows, cols);

    let mut cache = C2R_2D_CACHE
        .lock()
        .map_err(|e| FFTError::ComputationError(format!("Failed to lock 2D C2R cache: {}", e)))?;

    if cache.is_none() {
        *cache = Some(HashMap::new());
    }

    let cache_map = cache
        .as_mut()
        .ok_or_else(|| FFTError::ComputationError("Cache initialization failed".to_string()))?;

    if let std::collections::hash_map::Entry::Vacant(e) = cache_map.entry(key) {
        let plan = RealPlan2D::c2r(rows, cols, Flags::ESTIMATE).ok_or_else(|| {
            FFTError::ComputationError(format!(
                "Failed to create 2D C2R plan for size {}x{}",
                rows, cols
            ))
        })?;
        e.insert(plan);
    }

    let plan = cache_map
        .get(&key)
        .ok_or_else(|| FFTError::ComputationError("Failed to get cached plan".to_string()))?;
    plan.execute_c2r(input, output);
    Ok(())
}

// ========================================
// CACHE MANAGEMENT
// ========================================

/// Clear all cached plans (useful for testing or memory management)
pub fn clear_all_caches() {
    if let Ok(mut cache) = R2C_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = C2C_FWD_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = C2C_BWD_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = C2R_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = DCT2_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = IDCT2_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = DST2_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = IDST2_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = R2C_2D_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = C2C_2D_FWD_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = C2C_2D_BWD_CACHE.lock() {
        *cache = None;
    }
    if let Ok(mut cache) = C2R_2D_CACHE.lock() {
        *cache = None;
    }
}

/// Get cache statistics for debugging/monitoring
#[derive(Debug, Clone, Default)]
pub struct CacheStats {
    pub r2c_count: usize,
    pub c2c_fwd_count: usize,
    pub c2c_bwd_count: usize,
    pub c2r_count: usize,
    pub dct2_count: usize,
    pub idct2_count: usize,
    pub dst2_count: usize,
    pub idst2_count: usize,
    pub r2c_2d_count: usize,
    pub c2c_2d_fwd_count: usize,
    pub c2c_2d_bwd_count: usize,
    pub c2r_2d_count: usize,
}

/// Get current cache statistics
pub fn get_cache_stats() -> CacheStats {
    let mut stats = CacheStats::default();

    if let Ok(cache) = R2C_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.r2c_count = map.len();
        }
    }
    if let Ok(cache) = C2C_FWD_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.c2c_fwd_count = map.len();
        }
    }
    if let Ok(cache) = C2C_BWD_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.c2c_bwd_count = map.len();
        }
    }
    if let Ok(cache) = C2R_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.c2r_count = map.len();
        }
    }
    if let Ok(cache) = DCT2_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.dct2_count = map.len();
        }
    }
    if let Ok(cache) = IDCT2_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.idct2_count = map.len();
        }
    }
    if let Ok(cache) = DST2_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.dst2_count = map.len();
        }
    }
    if let Ok(cache) = IDST2_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.idst2_count = map.len();
        }
    }
    if let Ok(cache) = R2C_2D_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.r2c_2d_count = map.len();
        }
    }
    if let Ok(cache) = C2C_2D_FWD_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.c2c_2d_fwd_count = map.len();
        }
    }
    if let Ok(cache) = C2C_2D_BWD_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.c2c_2d_bwd_count = map.len();
        }
    }
    if let Ok(cache) = C2R_2D_CACHE.lock() {
        if let Some(ref map) = *cache {
            stats.c2r_2d_count = map.len();
        }
    }

    stats
}