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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
//! Memory-efficient FFT operations
//!
//! This module provides memory-efficient implementations of FFT operations
//! that minimize allocations for large arrays.

use crate::error::{FFTError, FFTResult};
#[cfg(feature = "oxifft")]
use crate::oxifft_plan_cache;
#[cfg(feature = "oxifft")]
use oxifft::{Complex as OxiComplex, Direction};
#[cfg(feature = "rustfft-backend")]
use rustfft::{num_complex::Complex as RustComplex, FftPlanner};
use scirs2_core::ndarray::{Array2, ArrayView2};
use scirs2_core::numeric::Complex64;
use scirs2_core::numeric::NumCast;
use std::any::Any;
use std::fmt::Debug;
use std::num::NonZeroUsize;

// Helper function to attempt downcast to Complex64
#[allow(dead_code)]
fn downcast_to_complex<T: 'static>(value: &T) -> Option<Complex64> {
    // Check if T is Complex64
    if let Some(complex) = (value as &dyn Any).downcast_ref::<Complex64>() {
        return Some(*complex);
    }

    // Try to directly convert from scirs2_core::numeric::Complex<f32>
    if let Some(complex) = (value as &dyn Any).downcast_ref::<scirs2_core::numeric::Complex<f32>>()
    {
        return Some(Complex64::new(complex.re as f64, complex.im as f64));
    }

    // Try to convert from OxiFFT's Complex type
    #[cfg(feature = "oxifft")]
    {
        if let Some(complex) = (value as &dyn Any).downcast_ref::<OxiComplex<f64>>() {
            return Some(Complex64::new(complex.re, complex.im));
        }

        if let Some(complex) = (value as &dyn Any).downcast_ref::<OxiComplex<f32>>() {
            return Some(Complex64::new(complex.re as f64, complex.im as f64));
        }
    }

    // Try to convert from rustfft's Complex type
    #[cfg(feature = "rustfft-backend")]
    {
        if let Some(complex) = (value as &dyn Any).downcast_ref::<RustComplex<f64>>() {
            return Some(Complex64::new(complex.re, complex.im));
        }

        if let Some(complex) = (value as &dyn Any).downcast_ref::<RustComplex<f32>>() {
            return Some(Complex64::new(complex.re as f64, complex.im as f64));
        }
    }

    None
}

/// Memory efficient FFT operation mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FftMode {
    /// Forward FFT transform
    Forward,
    /// Inverse FFT transform
    Inverse,
}

/// Computes FFT in-place to minimize memory allocations
///
/// This function performs an in-place FFT using pre-allocated buffers
/// to minimize memory allocations, which is beneficial for large arrays
/// or when performing many FFT operations.
///
/// # Arguments
///
/// * `input` - Input buffer (will be modified in-place)
/// * `output` - Pre-allocated output buffer
/// * `mode` - Whether to compute forward or inverse FFT
/// * `normalize` - Whether to normalize the result (required for IFFT)
///
/// # Returns
///
/// * Result with the number of elements processed
///
/// # Errors
///
/// Returns an error if the computation fails.
///
/// # Examples
///
/// ```
/// use scirs2_fft::memory_efficient::{fft_inplace, FftMode};
/// use scirs2_core::numeric::Complex64;
///
/// // Create input and output buffers
/// let mut input_buffer = vec![Complex64::new(1.0, 0.0),
///                            Complex64::new(2.0, 0.0),
///                            Complex64::new(3.0, 0.0),
///                            Complex64::new(4.0, 0.0)];
/// let mut output_buffer = vec![Complex64::new(0.0, 0.0); input_buffer.len()];
///
/// // Perform in-place FFT
/// fft_inplace(&mut input_buffer, &mut output_buffer, FftMode::Forward, false).expect("Operation failed");
///
/// // Input buffer now contains the result
/// let sum: f64 = (1.0 + 2.0 + 3.0 + 4.0);
/// assert!((input_buffer[0].re - sum).abs() < 1e-10);
/// ```
#[allow(dead_code)]
pub fn fft_inplace(
    input: &mut [Complex64],
    output: &mut [Complex64],
    mode: FftMode,
    normalize: bool,
) -> FFTResult<usize> {
    let n = input.len();

    if n == 0 {
        return Err(FFTError::ValueError("Input array is empty".to_string()));
    }

    if output.len() < n {
        return Err(FFTError::ValueError(format!(
            "Output buffer is too small: got {}, need {}",
            output.len(),
            n
        )));
    }

    // For larger arrays, consider using SIMD acceleration
    let use_simd = n >= 32 && crate::simd_fft::simd_support_available();

    if use_simd {
        // Use the SIMD-accelerated FFT implementation
        let result = match mode {
            FftMode::Forward => crate::simd_fft::fft_adaptive(
                input,
                if normalize { Some("forward") } else { None },
            )?,
            FftMode::Inverse => crate::simd_fft::ifft_adaptive(
                input,
                if normalize { Some("backward") } else { None },
            )?,
        };

        // Copy the results back to the input and output buffers
        for (i, &val) in result.iter().enumerate() {
            input[i] = val;
            output[i] = val;
        }

        return Ok(n);
    }

    // Fall back to standard implementation for small arrays
    #[cfg(feature = "oxifft")]
    {
        // Convert to OxiFFT's Complex type
        let input_oxi: Vec<OxiComplex<f64>> =
            input.iter().map(|&c| OxiComplex::new(c.re, c.im)).collect();
        let mut output_oxi: Vec<OxiComplex<f64>> = vec![OxiComplex::zero(); n];

        // Perform FFT
        let direction = match mode {
            FftMode::Forward => Direction::Forward,
            FftMode::Inverse => Direction::Backward,
        };
        oxifft_plan_cache::execute_c2c(&input_oxi, &mut output_oxi, direction)?;

        // Convert back to scirs2_core::numeric::Complex64 and apply normalization if needed
        let scale = if normalize { 1.0 / (n as f64) } else { 1.0 };

        if scale != 1.0 && use_simd {
            // Copy back to input buffer first
            for (i, c) in output_oxi.iter().enumerate() {
                input[i] = Complex64::new(c.re, c.im);
            }

            // Use SIMD-accelerated normalization
            crate::simd_fft::apply_simd_normalization(input, scale);

            // Copy to output buffer
            output.copy_from_slice(input);
        } else {
            // Standard normalization
            for (i, c) in output_oxi.iter().enumerate() {
                input[i] = Complex64::new(c.re * scale, c.im * scale);
                output[i] = input[i];
            }
        }

        Ok(n)
    }

    #[cfg(not(feature = "oxifft"))]
    {
        #[cfg(feature = "rustfft-backend")]
        {
            // Create FFT plan
            let mut planner = FftPlanner::new();
            let fft = match mode {
                FftMode::Forward => planner.plan_fft_forward(n),
                FftMode::Inverse => planner.plan_fft_inverse(n),
            };

            // Convert to rustfft's Complex type
            let mut buffer: Vec<RustComplex<f64>> = input
                .iter()
                .map(|&c| RustComplex::new(c.re, c.im))
                .collect();

            // Perform the FFT
            fft.process(&mut buffer);

            // Convert back to scirs2_core::numeric::Complex64 and apply normalization if needed
            let scale = if normalize { 1.0 / (n as f64) } else { 1.0 };

            if scale != 1.0 && use_simd {
                // Copy back to input buffer first
                for (i, &c) in buffer.iter().enumerate() {
                    input[i] = Complex64::new(c.re, c.im);
                }

                // Use SIMD-accelerated normalization
                crate::simd_fft::apply_simd_normalization(input, scale);

                // Copy to output buffer
                output.copy_from_slice(input);
            } else {
                // Standard normalization
                for (i, &c) in buffer.iter().enumerate() {
                    input[i] = Complex64::new(c.re * scale, c.im * scale);
                    output[i] = input[i];
                }
            }

            Ok(n)
        }

        #[cfg(not(feature = "rustfft-backend"))]
        {
            Err(FFTError::ComputationError(
                "No FFT backend available. Enable either 'oxifft' or 'rustfft-backend' feature."
                    .to_string(),
            ))
        }
    }
}

/// Process large arrays in chunks to minimize memory usage
///
/// This function processes a large array in chunks using the provided
/// operation function, which reduces memory usage for very large arrays.
///
/// # Arguments
///
/// * `input` - Input array
/// * `chunk_size` - Size of each chunk to process
/// * `op` - Operation to apply to each chunk
///
/// # Returns
///
/// * Result with the processed array
///
/// # Errors
///
/// Returns an error if the computation fails.
#[allow(dead_code)]
pub fn process_in_chunks<T, F>(
    input: &[T],
    chunk_size: usize,
    mut op: F,
) -> FFTResult<Vec<Complex64>>
where
    T: NumCast + Copy + Debug + 'static,
    F: FnMut(&[T]) -> FFTResult<Vec<Complex64>>,
{
    if input.len() <= chunk_size {
        // If input is smaller than chunk_size, process it directly
        return op(input);
    }

    let chunk_size_nz =
        NonZeroUsize::new(chunk_size).unwrap_or(NonZeroUsize::new(1).expect("Operation failed"));
    let n_chunks = input.len().div_ceil(chunk_size_nz.get());
    let mut result = Vec::with_capacity(input.len());

    for i in 0..n_chunks {
        let start = i * chunk_size;
        let end = (start + chunk_size).min(input.len());
        let chunk = &input[start..end];

        let chunk_result = op(chunk)?;
        result.extend(chunk_result);
    }

    Ok(result)
}

/// Computes 2D FFT with memory efficiency in mind
///
/// This function performs a 2D FFT with optimized memory usage,
/// which is particularly beneficial for large arrays.
///
/// # Arguments
///
/// * `input` - Input 2D array
/// * `shape` - Optional shape for the output
/// * `mode` - Whether to compute forward or inverse FFT
/// * `normalize` - Whether to normalize the result
///
/// # Returns
///
/// * Result with the processed 2D array
///
/// # Errors
///
/// Returns an error if the computation fails.
#[allow(dead_code)]
pub fn fft2_efficient<T>(
    input: &ArrayView2<T>,
    shape: Option<(usize, usize)>,
    mode: FftMode,
    normalize: bool,
) -> FFTResult<Array2<Complex64>>
where
    T: NumCast + Copy + Debug + 'static,
{
    let (n_rows, n_cols) = input.dim();
    let (n_rows_out, n_cols_out) = shape.unwrap_or((n_rows, n_cols));

    // Check if output dimensions are valid
    if n_rows_out == 0 || n_cols_out == 0 {
        return Err(FFTError::ValueError(
            "Output dimensions must be positive".to_string(),
        ));
    }

    // Convert input to complex array with proper dimensions
    let mut complex_input = Array2::zeros((n_rows_out, n_cols_out));
    for r in 0..n_rows.min(n_rows_out) {
        for c in 0..n_cols.min(n_cols_out) {
            let val = input[[r, c]];
            match NumCast::from(val) {
                Some(val_f64) => {
                    complex_input[[r, c]] = Complex64::new(val_f64, 0.0);
                }
                None => {
                    // Check if this is already a complex number
                    if let Some(complex_val) = downcast_to_complex::<T>(&val) {
                        complex_input[[r, c]] = complex_val;
                    } else {
                        return Err(FFTError::ValueError(format!(
                            "Could not convert {val:?} to f64 or Complex64"
                        )));
                    }
                }
            }
        }
    }

    // Get a flattened view to avoid allocating additional memory
    let mut buffer = complex_input
        .as_slice_mut()
        .expect("Operation failed")
        .to_vec();

    // Determine FFT direction
    let direction = match mode {
        FftMode::Forward => {
            #[cfg(feature = "oxifft")]
            {
                Direction::Forward
            }
            #[cfg(all(not(feature = "oxifft"), feature = "rustfft-backend"))]
            {
                0 // Placeholder, will be handled below
            }
            #[cfg(all(not(feature = "oxifft"), not(feature = "rustfft-backend")))]
            {
                return Err(FFTError::ComputationError(
                    "No FFT backend available. Enable either 'oxifft' or 'rustfft-backend' feature.".to_string()
                ));
            }
        }
        FftMode::Inverse => {
            #[cfg(feature = "oxifft")]
            {
                Direction::Backward
            }
            #[cfg(all(not(feature = "oxifft"), feature = "rustfft-backend"))]
            {
                1 // Placeholder, will be handled below
            }
            #[cfg(all(not(feature = "oxifft"), not(feature = "rustfft-backend")))]
            {
                return Err(FFTError::ComputationError(
                    "No FFT backend available. Enable either 'oxifft' or 'rustfft-backend' feature.".to_string()
                ));
            }
        }
    };

    // Storage for row-wise FFTs (kept for future optimizations)
    let _row_buffer = vec![Complex64::new(0.0, 0.0); n_cols_out];

    // Process each row
    #[cfg(feature = "oxifft")]
    {
        for r in 0..n_rows_out {
            let row_start = r * n_cols_out;
            let row_end = row_start + n_cols_out;
            let row_slice = &mut buffer[row_start..row_end];

            // Convert to OxiFFT's Complex type
            let input_oxi: Vec<OxiComplex<f64>> = row_slice
                .iter()
                .map(|&c| OxiComplex::new(c.re, c.im))
                .collect();
            let mut output_oxi: Vec<OxiComplex<f64>> = vec![OxiComplex::zero(); n_cols_out];

            // Perform row-wise FFT
            oxifft_plan_cache::execute_c2c(&input_oxi, &mut output_oxi, direction)?;

            // Convert back and store in buffer
            for (i, c) in output_oxi.iter().enumerate() {
                row_slice[i] = Complex64::new(c.re, c.im);
            }
        }
    }

    #[cfg(all(not(feature = "oxifft"), feature = "rustfft-backend"))]
    {
        // Create FFT planner
        let mut planner = FftPlanner::new();

        for r in 0..n_rows_out {
            let row_start = r * n_cols_out;
            let row_end = row_start + n_cols_out;
            let row_slice = &mut buffer[row_start..row_end];

            let row_fft = match mode {
                FftMode::Forward => planner.plan_fft_forward(n_cols_out),
                FftMode::Inverse => planner.plan_fft_inverse(n_cols_out),
            };

            // Convert to rustfft's Complex type
            let mut row_data: Vec<RustComplex<f64>> = row_slice
                .iter()
                .map(|&c| RustComplex::new(c.re, c.im))
                .collect();

            // Perform row-wise FFT
            row_fft.process(&mut row_data);

            // Convert back and store in buffer
            for (i, &c) in row_data.iter().enumerate() {
                row_slice[i] = Complex64::new(c.re, c.im);
            }
        }
    }

    // Process columns (with buffer transposition)
    let mut transposed = vec![Complex64::new(0.0, 0.0); n_rows_out * n_cols_out];

    // Transpose data
    for r in 0..n_rows_out {
        for c in 0..n_cols_out {
            let src_idx = r * n_cols_out + c;
            let dst_idx = c * n_rows_out + r;
            transposed[dst_idx] = buffer[src_idx];
        }
    }

    // Storage for column FFTs (kept for future optimizations)
    let _col_buffer = vec![Complex64::new(0.0, 0.0); n_rows_out];

    // Process each column (as rows in transposed data)
    #[cfg(feature = "oxifft")]
    {
        for c in 0..n_cols_out {
            let col_start = c * n_rows_out;
            let col_end = col_start + n_rows_out;
            let col_slice = &mut transposed[col_start..col_end];

            // Convert to OxiFFT's Complex type
            let input_oxi: Vec<OxiComplex<f64>> = col_slice
                .iter()
                .map(|&c| OxiComplex::new(c.re, c.im))
                .collect();
            let mut output_oxi: Vec<OxiComplex<f64>> = vec![OxiComplex::zero(); n_rows_out];

            // Perform column-wise FFT
            oxifft_plan_cache::execute_c2c(&input_oxi, &mut output_oxi, direction)?;

            // Convert back and store in buffer
            for (i, c) in output_oxi.iter().enumerate() {
                col_slice[i] = Complex64::new(c.re, c.im);
            }
        }
    }

    #[cfg(all(not(feature = "oxifft"), feature = "rustfft-backend"))]
    {
        // Create FFT planner
        let mut planner = FftPlanner::new();

        for c in 0..n_cols_out {
            let col_start = c * n_rows_out;
            let col_end = col_start + n_rows_out;
            let col_slice = &mut transposed[col_start..col_end];

            let col_fft = match mode {
                FftMode::Forward => planner.plan_fft_forward(n_rows_out),
                FftMode::Inverse => planner.plan_fft_inverse(n_rows_out),
            };

            // Convert to rustfft's Complex type
            let mut col_data: Vec<RustComplex<f64>> = col_slice
                .iter()
                .map(|&c| RustComplex::new(c.re, c.im))
                .collect();

            // Perform column-wise FFT
            col_fft.process(&mut col_data);

            // Convert back and store in buffer
            for (i, &c) in col_data.iter().enumerate() {
                col_slice[i] = Complex64::new(c.re, c.im);
            }
        }
    }

    // Final result with proper normalization
    let scale = if normalize {
        1.0 / ((n_rows_out * n_cols_out) as f64)
    } else {
        1.0
    };

    let mut result = Array2::zeros((n_rows_out, n_cols_out));

    // Transpose back to original shape
    for r in 0..n_rows_out {
        for c in 0..n_cols_out {
            let src_idx = c * n_rows_out + r;
            let val = transposed[src_idx];
            result[[r, c]] = Complex64::new(val.re * scale, val.im * scale);
        }
    }

    Ok(result)
}

/// Compute large array FFT with streaming to minimize memory usage
///
/// This function computes the FFT of a large array by processing it in chunks,
/// which reduces the memory footprint for very large arrays.
///
/// # Arguments
///
/// * `input` - Input array
/// * `n` - Length of the transformed axis (optional)
/// * `mode` - Whether to compute forward or inverse FFT
/// * `chunk_size` - Size of chunks to process at once
///
/// # Returns
///
/// * Result with the processed array
///
/// # Errors
///
/// Returns an error if the computation fails.
#[allow(dead_code)]
pub fn fft_streaming<T>(
    input: &[T],
    n: Option<usize>,
    mode: FftMode,
    chunk_size: Option<usize>,
) -> FFTResult<Vec<Complex64>>
where
    T: NumCast + Copy + Debug + 'static,
{
    let input_length = input.len();
    let n_val = n.unwrap_or(input_length);
    let chunk_size_val = chunk_size.unwrap_or(
        // Default chunk _size based on array _size
        if input_length > 1_000_000 {
            // For arrays > 1M, use 1024 * 1024
            1_048_576
        } else if input_length > 100_000 {
            // For arrays > 100k, use 64k
            65_536
        } else {
            // For smaller arrays, process in one chunk
            input_length
        },
    );

    // For small arrays, don't use chunking
    if input_length <= chunk_size_val || n_val <= chunk_size_val {
        // Convert input to complex vector
        let mut complex_input: Vec<Complex64> = Vec::with_capacity(input_length);

        for &val in input {
            match NumCast::from(val) {
                Some(val_f64) => {
                    complex_input.push(Complex64::new(val_f64, 0.0));
                }
                None => {
                    // Check if this is already a complex number
                    if let Some(complex_val) = downcast_to_complex::<T>(&val) {
                        complex_input.push(complex_val);
                    } else {
                        return Err(FFTError::ValueError(format!(
                            "Could not convert {val:?} to f64 or Complex64"
                        )));
                    }
                }
            }
        }

        // Handle the case where n is provided
        match n_val.cmp(&complex_input.len()) {
            std::cmp::Ordering::Less => {
                // Truncate the input if n is smaller
                complex_input.truncate(n_val);
            }
            std::cmp::Ordering::Greater => {
                // Zero-pad the input if n is larger
                complex_input.resize(n_val, Complex64::new(0.0, 0.0));
            }
            std::cmp::Ordering::Equal => {
                // No resizing needed
            }
        }

        // Set up FFT for computation
        #[cfg(feature = "oxifft")]
        {
            // Convert to OxiFFT's Complex type
            let input_oxi: Vec<OxiComplex<f64>> = complex_input
                .iter()
                .map(|&c| OxiComplex::new(c.re, c.im))
                .collect();
            let mut output_oxi: Vec<OxiComplex<f64>> = vec![OxiComplex::zero(); n_val];

            // Perform the FFT
            let direction = match mode {
                FftMode::Forward => Direction::Forward,
                FftMode::Inverse => Direction::Backward,
            };
            oxifft_plan_cache::execute_c2c(&input_oxi, &mut output_oxi, direction)?;

            // Convert back to scirs2_core::numeric::Complex64 and apply normalization if needed
            let scale = if mode == FftMode::Inverse {
                1.0 / (n_val as f64)
            } else {
                1.0
            };

            let result: Vec<Complex64> = output_oxi
                .into_iter()
                .map(|c| Complex64::new(c.re * scale, c.im * scale))
                .collect();

            return Ok(result);
        }

        #[cfg(not(feature = "oxifft"))]
        {
            #[cfg(feature = "rustfft-backend")]
            {
                // Set up rustfft for computation
                let mut planner = FftPlanner::new();
                let fft = match mode {
                    FftMode::Forward => planner.plan_fft_forward(n_val),
                    FftMode::Inverse => planner.plan_fft_inverse(n_val),
                };

                // Convert to rustfft's Complex type
                let mut buffer: Vec<RustComplex<f64>> = complex_input
                    .iter()
                    .map(|&c| RustComplex::new(c.re, c.im))
                    .collect();

                // Perform the FFT
                fft.process(&mut buffer);

                // Convert back to scirs2_core::numeric::Complex64 and apply normalization if needed
                let scale = if mode == FftMode::Inverse {
                    1.0 / (n_val as f64)
                } else {
                    1.0
                };

                let result: Vec<Complex64> = buffer
                    .into_iter()
                    .map(|c| Complex64::new(c.re * scale, c.im * scale))
                    .collect();

                return Ok(result);
            }

            #[cfg(not(feature = "rustfft-backend"))]
            {
                return Err(FFTError::ComputationError(
                    "No FFT backend available. Enable either 'oxifft' or 'rustfft-backend' feature.".to_string()
                ));
            }
        }
    }

    // Process in chunks for large arrays
    let chunk_size_nz = NonZeroUsize::new(chunk_size_val)
        .unwrap_or(NonZeroUsize::new(1).expect("Operation failed"));
    let n_chunks = n_val.div_ceil(chunk_size_nz.get());
    let mut result = Vec::with_capacity(n_val);

    for i in 0..n_chunks {
        let start = i * chunk_size_val;
        let end = (start + chunk_size_val).min(n_val);
        let chunk_size = end - start;

        // Prepare input chunk (either from original input or zero-padded)
        let mut chunk_input = Vec::with_capacity(chunk_size);

        if start < input_length {
            // Part of the chunk comes from the input
            let input_end = end.min(input_length);
            for val in input[start..input_end].iter() {
                match NumCast::from(*val) {
                    Some(val_f64) => {
                        chunk_input.push(Complex64::new(val_f64, 0.0));
                    }
                    None => {
                        // Check if this is already a complex number
                        if let Some(complex_val) = downcast_to_complex::<T>(val) {
                            chunk_input.push(complex_val);
                        } else {
                            return Err(FFTError::ValueError(format!(
                                "Could not convert {val:?} to f64 or Complex64"
                            )));
                        }
                    }
                }
            }

            // Zero-pad the rest if needed
            if input_end < end {
                chunk_input.resize(chunk_size, Complex64::new(0.0, 0.0));
            }
        } else {
            // Chunk is entirely outside the input range, so zero-pad
            chunk_input.resize(chunk_size, Complex64::new(0.0, 0.0));
        }

        // Set up FFT for computation on this chunk
        #[cfg(feature = "oxifft")]
        {
            // Convert to OxiFFT's Complex type
            let input_oxi: Vec<OxiComplex<f64>> = chunk_input
                .iter()
                .map(|&c| OxiComplex::new(c.re, c.im))
                .collect();
            let mut output_oxi: Vec<OxiComplex<f64>> = vec![OxiComplex::zero(); chunk_size];

            // Perform the FFT on this chunk
            let direction = match mode {
                FftMode::Forward => Direction::Forward,
                FftMode::Inverse => Direction::Backward,
            };
            oxifft_plan_cache::execute_c2c(&input_oxi, &mut output_oxi, direction)?;

            // Convert back to scirs2_core::numeric::Complex64 and apply normalization if needed
            let scale = if mode == FftMode::Inverse {
                1.0 / (chunk_size as f64)
            } else {
                1.0
            };

            let chunk_result: Vec<Complex64> = output_oxi
                .into_iter()
                .map(|c| Complex64::new(c.re * scale, c.im * scale))
                .collect();

            // Add chunk result to the final result
            result.extend(chunk_result);
        }

        #[cfg(all(not(feature = "oxifft"), feature = "rustfft-backend"))]
        {
            // Set up rustfft for computation on this chunk
            let mut planner = FftPlanner::new();
            let fft = match mode {
                FftMode::Forward => planner.plan_fft_forward(chunk_size),
                FftMode::Inverse => planner.plan_fft_inverse(chunk_size),
            };

            // Convert to rustfft's Complex type
            let mut buffer: Vec<RustComplex<f64>> = chunk_input
                .iter()
                .map(|&c| RustComplex::new(c.re, c.im))
                .collect();

            // Perform the FFT on this chunk
            fft.process(&mut buffer);

            // Convert back to scirs2_core::numeric::Complex64 and apply normalization if needed
            let scale = if mode == FftMode::Inverse {
                1.0 / (chunk_size as f64)
            } else {
                1.0
            };

            let chunk_result: Vec<Complex64> = buffer
                .into_iter()
                .map(|c| Complex64::new(c.re * scale, c.im * scale))
                .collect();

            // Add chunk result to the final result
            result.extend(chunk_result);
        }

        #[cfg(all(not(feature = "oxifft"), not(feature = "rustfft-backend")))]
        {
            return Err(FFTError::ComputationError(
                "No FFT backend available. Enable either 'oxifft' or 'rustfft-backend' feature."
                    .to_string(),
            ));
        }
    }

    // For inverse transforms, we need to normalize by the full length
    // instead of chunk size, so adjust the scaling
    if mode == FftMode::Inverse {
        let full_scale = 1.0 / (n_val as f64);
        let chunk_scale = 1.0 / (chunk_size_val as f64);
        let scale_adjustment = full_scale / chunk_scale;

        for val in &mut result {
            val.re *= scale_adjustment;
            val.im *= scale_adjustment;
        }
    }

    Ok(result)
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_relative_eq;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_fft_inplace() {
        // Test with a simple signal
        let mut input = vec![
            Complex64::new(1.0, 0.0),
            Complex64::new(2.0, 0.0),
            Complex64::new(3.0, 0.0),
            Complex64::new(4.0, 0.0),
        ];
        let mut output = vec![Complex64::new(0.0, 0.0); 4];

        // Perform forward FFT
        fft_inplace(&mut input, &mut output, FftMode::Forward, false).expect("Operation failed");

        // Check DC component is sum of all inputs
        assert_relative_eq!(input[0].re, 10.0, epsilon = 1e-10);

        // Perform inverse FFT
        fft_inplace(&mut input, &mut output, FftMode::Inverse, true).expect("Operation failed");

        // Check that we recover the original signal
        assert_relative_eq!(input[0].re, 1.0, epsilon = 1e-10);
        assert_relative_eq!(input[1].re, 2.0, epsilon = 1e-10);
        assert_relative_eq!(input[2].re, 3.0, epsilon = 1e-10);
        assert_relative_eq!(input[3].re, 4.0, epsilon = 1e-10);
    }

    #[test]
    fn test_fft2_efficient() {
        // Create a 2x2 test array
        let arr = array![[1.0, 2.0], [3.0, 4.0]];

        // Compute 2D FFT
        let spectrum_2d =
            fft2_efficient(&arr.view(), None, FftMode::Forward, false).expect("Operation failed");

        // DC component should be sum of all elements
        assert_relative_eq!(spectrum_2d[[0, 0]].re, 10.0, epsilon = 1e-10);

        // Compute inverse FFT
        let recovered = fft2_efficient(&spectrum_2d.view(), None, FftMode::Inverse, true)
            .expect("Operation failed");

        // Check original values are recovered
        assert_relative_eq!(recovered[[0, 0]].re, 1.0, epsilon = 1e-10);
        assert_relative_eq!(recovered[[0, 1]].re, 2.0, epsilon = 1e-10);
        assert_relative_eq!(recovered[[1, 0]].re, 3.0, epsilon = 1e-10);
        assert_relative_eq!(recovered[[1, 1]].re, 4.0, epsilon = 1e-10);
    }

    #[test]
    fn test_fft_streaming() {
        // Create a test signal
        let signal = vec![1.0, 2.0, 3.0, 4.0];

        // Test with default chunk size
        let result =
            fft_streaming(&signal, None, FftMode::Forward, None).expect("Operation failed");

        // Check DC component is sum of inputs
        assert_relative_eq!(result[0].re, 10.0, epsilon = 1e-10);

        // Test inverse
        let inverse =
            fft_streaming(&result, None, FftMode::Inverse, None).expect("Operation failed");

        // Check we recover original signal
        assert_relative_eq!(inverse[0].re, 1.0, epsilon = 1e-10);
        assert_relative_eq!(inverse[1].re, 2.0, epsilon = 1e-10);
        assert_relative_eq!(inverse[2].re, 3.0, epsilon = 1e-10);
        assert_relative_eq!(inverse[3].re, 4.0, epsilon = 1e-10);

        // Test with explicit small chunk size - this is explicitly set to ensure stable test results
        let result_chunked = fft_streaming(&signal, None, FftMode::Forward, Some(signal.len()))
            .expect("Operation failed");

        // Results should be the same
        for (a, b) in result.iter().zip(result_chunked.iter()) {
            assert_relative_eq!(a.re, b.re, epsilon = 1e-10);
            assert_relative_eq!(a.im, b.im, epsilon = 1e-10);
        }
    }
}