cute-dsp 0.0.40

A Rust library for audio and signal processing
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
//! Fast Fourier Transform implementation
//!
//! This module provides FFT implementations optimized for sizes that are products of 2^a * 3^b.
//! It includes both complex and real FFT implementations with various optimizations.

#![allow(unused_imports)]

#[cfg(feature = "std")]
use std::{f64::consts::PI, vec::Vec};


#[cfg(not(feature = "std"))]
use core::f64::consts::PI;

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::vec::Vec;

use num_complex::Complex;
use num_traits::Float;
use num_traits::FromPrimitive;

use crate::perf;

/// Helper functions for complex multiplication and data interleaving
mod helpers {
    use super::*;

    /// Complex multiplication
    pub fn complex_mul<T: Float>(
        a: &mut [Complex<T>],
        b: &[Complex<T>],
        c: &[Complex<T>],
        size: usize,
    ) {
        for i in 0..size {
            let bi = b[i];
            let ci = c[i];
            a[i] = Complex::new(
                bi.re * ci.re - bi.im * ci.im,
                bi.im * ci.re + bi.re * ci.im,
            );
        }
    }

    /// Complex multiplication with conjugate of second argument
    pub fn complex_mul_conj<T: Float>(
        a: &mut [Complex<T>],
        b: &[Complex<T>],
        c: &[Complex<T>],
        size: usize,
    ) {
        for i in 0..size {
            let bi = b[i];
            let ci = c[i];
            a[i] = Complex::new(
                bi.re * ci.re + bi.im * ci.im,
                bi.im * ci.re - bi.re * ci.im,
            );
        }
    }

    /// Complex multiplication with split complex representation
    pub fn complex_mul_split<T: Float>(
        ar: &mut [T],
        ai: &mut [T],
        br: &[T],
        bi: &[T],
        cr: &[T],
        ci: &[T],
        size: usize,
    ) {
        for i in 0..size {
            let rr = br[i] * cr[i] - bi[i] * ci[i];
            let ri = br[i] * ci[i] + bi[i] * cr[i];
            ar[i] = rr;
            ai[i] = ri;
        }
    }

    /// Complex multiplication with conjugate and split complex representation
    pub fn complex_mul_conj_split<T: Float>(
        ar: &mut [T],
        ai: &mut [T],
        br: &[T],
        bi: &[T],
        cr: &[T],
        ci: &[T],
        size: usize,
    ) {
        for i in 0..size {
            let rr = cr[i] * br[i] + ci[i] * bi[i];
            let ri = cr[i] * bi[i] - ci[i] * br[i];
            ar[i] = rr;
            ai[i] = ri;
        }
    }

    /// Interleave copy with fixed stride
    pub fn interleave_copy<T: Copy>(a: &[T], b: &mut [T], a_stride: usize, b_stride: usize) {
        for bi in 0..b_stride {
            for ai in 0..a_stride {
                b[bi + ai * b_stride] = a[bi * a_stride + ai];
            }
        }
    }

    /// Interleave copy with split complex representation
    pub fn interleave_copy_split<T: Copy>(
        a_real: &[T],
        a_imag: &[T],
        b_real: &mut [T],
        b_imag: &mut [T],
        a_stride: usize,
        b_stride: usize,
    ) {
        for bi in 0..b_stride {
            for ai in 0..a_stride {
                b_real[bi + ai * b_stride] = a_real[bi * a_stride + ai];
                b_imag[bi + ai * b_stride] = a_imag[bi * a_stride + ai];
            }
        }
    }
}

/// A simple and portable power-of-2 FFT implementation
pub struct SimpleFFT<T: Float> {
    twiddles: Vec<Complex<T>>,
    working: Vec<Complex<T>>,
}

impl<T: Float + FromPrimitive> SimpleFFT<T> {
    /// Create a new FFT with the specified size
    pub fn new(size: usize) -> Self {
        let mut result = Self {
            twiddles: Vec::new(),
            working: Vec::new(),
        };
        result.resize(size);
        result
    }

    /// Resize the FFT to handle a different size
    pub fn resize(&mut self, size: usize) {
        self.twiddles.resize(size * 3 / 4, Complex::new(T::zero(), T::zero()));
        for i in 0..self.twiddles.len() {
            let twiddle_phase = -T::from_f64(2.0).unwrap() * T::from_f64(PI as f64).unwrap() * T::from_f64(i as f64).unwrap() / T::from_f64(size as f64).unwrap();
            self.twiddles[i] = Complex::new(
                twiddle_phase.cos(),
                twiddle_phase.sin(),
            );
        }
        self.working.resize(size, Complex::new(T::zero(), T::zero()));
    }

    /// Perform a forward FFT
    pub fn fft(&mut self, time: &[Complex<T>], freq: &mut [Complex<T>]) {
        let size = self.working.len();
        if size <= 1 {
            if size == 1 {
                freq[0] = time[0];
            }
            return;
        }
        let working_size = self.working.len();
            let working_mut = &mut self.working;
            Self::fft_pass::<false>(
                working_size,
                &self.twiddles,
                size, 
                1, 
                time, 
                freq, 
                working_mut);
    }

    /// Perform an inverse FFT
    pub fn ifft(&mut self, freq: &[Complex<T>], time: &mut [Complex<T>]) {
        let size = self.working.len();
        if size <= 1 {
            if size == 1 {
                time[0] = freq[0];
            }
            return;
        }
        let working_size = self.working.len();
        let working_mut = &mut self.working;
        Self::fft_pass::<true>(working_size,&self.twiddles,size, 1, freq, time, working_mut);
    }

    /// Perform a forward FFT with split complex representation
    pub fn fft_split(&self, in_r: &[T], in_i: &[T], out_r: &mut [T], out_i: &mut [T]) {
        let size = self.working.len();
        if size <= 1 {
            if size == 1 {
                out_r[0] = in_r[0];
                out_i[0] = in_i[0];
            }
            return;
        }
        
        // Create temporary buffers for working space
        let mut working_r = vec![T::zero(); size];
        let mut working_i = vec![T::zero(); size];
        
        self.fft_pass_split::<false>(size, 1, in_r, in_i, out_r, out_i, &mut working_r, &mut working_i);
    }

    /// Perform an inverse FFT with split complex representation
    pub fn ifft_split(&self, in_r: &[T], in_i: &[T], out_r: &mut [T], out_i: &mut [T]) {
        let size = self.working.len();
        if size <= 1 {
            if size == 1 {
                out_r[0] = in_r[0];
                out_i[0] = in_i[0];
            }
            return;
        }
        
        // Create temporary buffers for working space
        let mut working_r = vec![T::zero(); size];
        let mut working_i = vec![T::zero(); size];
        
        self.fft_pass_split::<true>(size, 1, in_r, in_i, out_r, out_i, &mut working_r, &mut working_i);
    }

    // Internal implementation of FFT pass
     fn fft_pass<const INVERSE: bool>(
        orignal_working_size:usize,
        twiddles: &[Complex<T>],
        size: usize,
        stride: usize,
        input: &[Complex<T>],
        output: &mut [Complex<T>],
        working: &mut [Complex<T>],
    ) {
        if size / 4 > 1 {
            // Calculate four quarter-size FFTs
            Self::fft_pass::<INVERSE>(orignal_working_size,twiddles,size / 4, stride * 4, input, working, output);
            Self::combine4::<INVERSE>(orignal_working_size,twiddles,size, stride, working, output);
        } else if size == 4 {
            Self::combine4::<INVERSE>(orignal_working_size,twiddles,4, stride, input, output);
        } else {
            // 2-point FFT
            for s in 0..stride {
                let b = input[s + stride];
                let a = input[s];
                output[s] = a + b;
                output[s + stride] = a - b;
            }
        }
    }

    // Internal implementation of FFT pass with split complex representation
    fn fft_pass_split<const INVERSE: bool>(
        &self,
        size: usize,
        stride: usize,
        in_r: &[T],
        in_i: &[T],
        out_r: &mut [T],
        out_i: &mut [T],
        working_r: &mut [T],
        working_i: &mut [T],
    ) {
        if size / 4 > 1 {
            // Calculate four quarter-size FFTs
            self.fft_pass_split::<INVERSE>(
                size / 4,
                stride * 4,
                in_r,
                in_i,
                working_r,
                working_i,
                out_r,
                out_i,
            );
            self.combine4_split::<INVERSE>(size, stride, working_r, working_i, out_r, out_i);
        } else if size == 4 {
            self.combine4_split::<INVERSE>(4, stride, in_r, in_i, out_r, out_i);
        } else {
            // 2-point FFT
            for s in 0..stride {
                let ar = in_r[s];
                let ai = in_i[s];
                let br = in_r[s + stride];
                let bi = in_i[s + stride];
                out_r[s] = ar + br;
                out_i[s] = ai + bi;
                out_r[s + stride] = ar - br;
                out_i[s + stride] = ai - bi;
            }
        }
    }

    // Combine interleaved results into a single spectrum
    fn combine4<const INVERSE: bool>(
        working_buf_len:usize,
        twiddles: &[Complex<T>],
        size: usize,
        stride: usize,
        input: &[Complex<T>],
        output: &mut [Complex<T>],
    ) {
        let twiddle_step = working_buf_len / size;
        
        for i in 0..size / 4 {
            let twiddle_b = twiddles[i * twiddle_step];
            let twiddle_c = twiddles[i * 2 * twiddle_step];
            let twiddle_d = twiddles[i * 3 * twiddle_step];
            
            let input_a = &input[4 * i * stride..];
            let input_b = &input[(4 * i + 1) * stride..];
            let input_c = &input[(4 * i + 2) * stride..];
            let input_d = &input[(4 * i + 3) * stride..];
            
            let (output_first_half, output_second_half) = output.split_at_mut((size / 4 * 2) * stride);
            let (output_a_chunk, output_b_chunk) = output_first_half.split_at_mut((size / 4) * stride);
            let (output_c_chunk, output_d_chunk) = output_second_half.split_at_mut((size / 4) * stride);

            let output_a = &mut output_a_chunk[i * stride..];
            let output_b = &mut output_b_chunk[i * stride..];
            let output_c = &mut output_c_chunk[i * stride..];
            let output_d = &mut output_d_chunk[i * stride..];
            
            for s in 0..stride {
                let a = input_a[s];
                let b = if INVERSE {
                    Complex::new(
                        input_b[s].re * twiddle_b.re + input_b[s].im * twiddle_b.im,
                        input_b[s].im * twiddle_b.re - input_b[s].re * twiddle_b.im,
                    )
                } else {
                    Complex::new(
                        input_b[s].re * twiddle_b.re - input_b[s].im * twiddle_b.im,
                        input_b[s].im * twiddle_b.re + input_b[s].re * twiddle_b.im,
                    )
                };
                
                let c = if INVERSE {
                    Complex::new(
                        input_c[s].re * twiddle_c.re + input_c[s].im * twiddle_c.im,
                        input_c[s].im * twiddle_c.re - input_c[s].re * twiddle_c.im,
                    )
                } else {
                    Complex::new(
                        input_c[s].re * twiddle_c.re - input_c[s].im * twiddle_c.im,
                        input_c[s].im * twiddle_c.re + input_c[s].re * twiddle_c.im,
                    )
                };
                
                let d = if INVERSE {
                    Complex::new(
                        input_d[s].re * twiddle_d.re + input_d[s].im * twiddle_d.im,
                        input_d[s].im * twiddle_d.re - input_d[s].re * twiddle_d.im,
                    )
                } else {
                    Complex::new(
                        input_d[s].re * twiddle_d.re - input_d[s].im * twiddle_d.im,
                        input_d[s].im * twiddle_d.re + input_d[s].re * twiddle_d.im,
                    )
                };
                
                let ac0 = a + c;
                let ac1 = a - c;
                let bd0 = b + d;
                let bd1 = if INVERSE { b - d } else { d - b };
                let bd1i = Complex::new(-bd1.im, bd1.re);
                
                output_a[s] = ac0 + bd0;
                output_b[s] = ac1 + bd1i;
                output_c[s] = ac0 - bd0;
                output_d[s] = ac1 - bd1i;
            }
        }
    }

    // Combine interleaved results into a single spectrum with split complex representation
    fn combine4_split<const INVERSE: bool>(
        &self,
        size: usize,
        stride: usize,
        input_r: &[T],
        input_i: &[T],
        output_r: &mut [T],
        output_i: &mut [T],
    ) {
        let twiddle_step = self.working.len() / size;
        
        for i in 0..size / 4 {
            let twiddle_b = self.twiddles[i * twiddle_step];
            let twiddle_c = self.twiddles[i * 2 * twiddle_step];
            let twiddle_d = self.twiddles[i * 3 * twiddle_step];
            
            for s in 0..stride {
                // Get input values
                let a_r = input_r[4 * i * stride + s];
                let a_i = input_i[4 * i * stride + s];
                
                let b_r = input_r[(4 * i + 1) * stride + s];
                let b_i = input_i[(4 * i + 1) * stride + s];
                
                let c_r = input_r[(4 * i + 2) * stride + s];
                let c_i = input_i[(4 * i + 2) * stride + s];
                
                let d_r = input_r[(4 * i + 3) * stride + s];
                let d_i = input_i[(4 * i + 3) * stride + s];
                
                // Apply twiddle factors
                let (b_r_tw, b_i_tw) = if INVERSE {
                    (
                        b_r * twiddle_b.re + b_i * twiddle_b.im,
                        b_i * twiddle_b.re - b_r * twiddle_b.im,
                    )
                } else {
                    (
                        b_r * twiddle_b.re - b_i * twiddle_b.im,
                        b_i * twiddle_b.re + b_r * twiddle_b.im,
                    )
                };
                
                let (c_r_tw, c_i_tw) = if INVERSE {
                    (
                        c_r * twiddle_c.re + c_i * twiddle_c.im,
                        c_i * twiddle_c.re - c_r * twiddle_c.im,
                    )
                } else {
                    (
                        c_r * twiddle_c.re - c_i * twiddle_c.im,
                        c_i * twiddle_c.re + c_r * twiddle_c.im,
                    )
                };
                
                let (d_r_tw, d_i_tw) = if INVERSE {
                    (
                        d_r * twiddle_d.re + d_i * twiddle_d.im,
                        d_i * twiddle_d.re - d_r * twiddle_d.im,
                    )
                } else {
                    (
                        d_r * twiddle_d.re - d_i * twiddle_d.im,
                        d_i * twiddle_d.re + d_r * twiddle_d.im,
                    )
                };
                
                // Butterfly calculations
                let ac0_r = a_r + c_r_tw;
                let ac0_i = a_i + c_i_tw;
                let ac1_r = a_r - c_r_tw;
                let ac1_i = a_i - c_i_tw;
                
                let bd0_r = b_r_tw + d_r_tw;
                let bd0_i = b_i_tw + d_i_tw;
                
                let (bd1_r, bd1_i) = if INVERSE {
                    (b_r_tw - d_r_tw, b_i_tw - d_i_tw)
                } else {
                    (d_r_tw - b_r_tw, d_i_tw - b_i_tw)
                };
                
                let bd1i_r = -bd1_i;
                let bd1i_i = bd1_r;
                
                // Store results
                output_r[i * stride + s] = ac0_r + bd0_r;
                output_i[i * stride + s] = ac0_i + bd0_i;
                
                output_r[(i + size / 4) * stride + s] = ac1_r + bd1i_r;
                output_i[(i + size / 4) * stride + s] = ac1_i + bd1i_i;
                
                output_r[(i + size / 4 * 2) * stride + s] = ac0_r - bd0_r;
                output_i[(i + size / 4 * 2) * stride + s] = ac0_i - bd0_i;
                
                output_r[(i + size / 4 * 3) * stride + s] = ac1_r - bd1i_r;
                output_i[(i + size / 4 * 3) * stride + s] = ac1_i - bd1i_i;
            }
        }
    }
}

/// A wrapper for complex FFT to handle real data
pub struct SimpleRealFFT<T: Float> {
    complex_fft: SimpleFFT<T>,
    tmp_time: Vec<Complex<T>>,
    tmp_freq: Vec<Complex<T>>,
}

impl<T: Float + num_traits::FromPrimitive> SimpleRealFFT<T> {
    /// Create a new real FFT with the specified size
    pub fn new(size: usize) -> Self {
        let mut result = Self {
            complex_fft: SimpleFFT::new(size),
            tmp_time: Vec::new(),
            tmp_freq: Vec::new(),
        };
        result.resize(size);
        result
    }

    /// Resize the FFT to handle a different size
    pub fn resize(&mut self, size: usize) {
        self.complex_fft.resize(size);
        self.tmp_time.resize(size, Complex::new(T::zero(), T::zero()));
        self.tmp_freq.resize(size, Complex::new(T::zero(), T::zero()));
    }

    /// Perform a forward FFT on real data
    pub fn fft(&mut self, time: &[T], freq: &mut [Complex<T>]) {
        let size = self.tmp_time.len();

        // Copy real data to complex buffer
        for i in 0..size {
            self.tmp_time[i] = Complex::new(time[i], T::zero());
        }

        // Perform complex FFT
        self.complex_fft.fft(&self.tmp_time, &mut self.tmp_freq);

        // Corrected output handling:
        // DC component (real only)
        freq[0] = Complex::new(self.tmp_freq[0].re, T::zero());
        // Positive frequencies
        for i in 1..size/2 {
            freq[i] = self.tmp_freq[i];
        }
        // Nyquist component (real only)
        freq[size/2] = Complex::new(self.tmp_freq[size/2].re, T::zero());
    }

    /// Perform a forward FFT on real data with split output
    pub fn fft_split(&self, in_r: &[T], out_r: &mut [T], out_i: &mut [T]) {
        let size = self.tmp_time.len();
        
        // Create temporary zero buffer for imaginary part
        let tmp_i = vec![T::zero(); size];
        
        // Perform complex FFT with split representation
        self.complex_fft.fft_split(in_r, &tmp_i, out_r, out_i);
        
        // Special case for Nyquist frequency
        out_i[0] = out_r[size / 2];
    }

    /// Perform an inverse FFT to real data
    pub fn ifft(&mut self, freq: &[Complex<T>], time: &mut [T]) {
        let size = self.tmp_freq.len();

        // DC component
        self.tmp_freq[0] = Complex::new(freq[0].re, T::zero());
        // Nyquist component
        self.tmp_freq[size/2] = Complex::new(freq[size/2].re, T::zero());

        // Fill the rest of the spectrum using conjugate symmetry
        for i in 1..size/2 {
            self.tmp_freq[i] = freq[i];
            self.tmp_freq[size - i] = freq[i].conj();
        }

        // Perform inverse complex FFT
        self.complex_fft.ifft(&self.tmp_freq, &mut self.tmp_time);

        // Extract real part
        for i in 0..size {
            time[i] = self.tmp_time[i].re;
        }
    }

    /// Perform an inverse FFT from split complex to real data
    pub fn ifft_split(&self, in_r: &[T], in_i: &[T], out_r: &mut [T]) {
        let size = self.tmp_freq.len();

        // Create temporary buffers for the full spectrum
        let mut tmp_freq_r = vec![T::zero(); size];
        let mut tmp_freq_i = vec![T::zero(); size];

        // DC component
        tmp_freq_r[0] = in_r[0];
        tmp_freq_i[0] = T::zero();

        // Nyquist component
        tmp_freq_r[size / 2] = in_i[0];
        tmp_freq_i[size / 2] = T::zero();

        // Fill the rest of the spectrum using conjugate symmetry
        for i in 1..size / 2 {
            tmp_freq_r[i] = in_r[i];
            tmp_freq_i[i] = in_i[i];
            tmp_freq_r[size - i] = in_r[i];
            tmp_freq_i[size - i] = -in_i[i];
        }

        // Create temporary buffer for imaginary output (will be discarded)
        let mut tmp_out_i = vec![T::zero(); size];

        // Perform inverse complex FFT
        self.complex_fft.ifft_split(&tmp_freq_r, &tmp_freq_i, out_r, &mut tmp_out_i);
    }
}

/// A power-of-2 FFT implementation that can be specialized for different platforms
pub struct Pow2FFT<T: Float> {
    simple_fft: SimpleFFT<T>,
    tmp: Vec<Complex<T>>,
}

impl<T: Float+ FromPrimitive> Pow2FFT<T> {
    /// Whether this FFT implementation is faster when given split-complex inputs
    pub const PREFERS_SPLIT: bool = true;

    /// Create a new FFT with the specified size
    pub fn new(size: usize) -> Self {
        let mut result = Self {
            simple_fft: SimpleFFT::new(size),
            tmp: Vec::new(),
        };
        result.resize(size);
        result
    }

    /// Resize the FFT to handle a different size
    pub fn resize(&mut self, size: usize) {
        self.simple_fft.resize(size);
        self.tmp.resize(size, Complex::new(T::zero(), T::zero()));
    }

    /// Perform a forward FFT
    pub fn fft(&mut self, time: &[Complex<T>], freq: &mut [Complex<T>]) {
        self.simple_fft.fft(time, freq);
    }

    /// Perform a forward FFT with split complex representation
    pub fn fft_split(&self, in_r: &[T], in_i: &[T], out_r: &mut [T], out_i: &mut [T]) {
        self.simple_fft.fft_split(in_r, in_i, out_r, out_i);
    }

    /// Perform an inverse FFT
    pub fn ifft(&mut self, freq: &[Complex<T>], time: &mut [Complex<T>]) {
        self.simple_fft.ifft(freq, time);
    }

    /// Perform an inverse FFT with split complex representation
    pub fn ifft_split(&self, in_r: &[T], in_i: &[T], out_r: &mut [T], out_i: &mut [T]) {
        self.simple_fft.ifft_split(in_r, in_i, out_r, out_i);
    }
}

/// A power-of-2 real FFT implementation
pub struct Pow2RealFFT<T: Float> {
    simple_real_fft: SimpleRealFFT<T>,
}

impl<T: Float + FromPrimitive> Pow2RealFFT<T> {
    /// Whether this FFT implementation is faster when given split-complex inputs
    pub const PREFERS_SPLIT: bool = Pow2FFT::<T>::PREFERS_SPLIT;

    /// Create a new real FFT with the specified size
    pub fn new(size: usize) -> Self {
        Self {
            simple_real_fft: SimpleRealFFT::new(size),
        }
    }

    /// Resize the FFT to handle a different size
    pub fn resize(&mut self, size: usize) {
        self.simple_real_fft.resize(size);
    }

    /// Perform a forward FFT on real data
    pub fn fft(&mut self, time: &[T], freq: &mut [Complex<T>]) {
        self.simple_real_fft.fft(time, freq);
    }

    /// Perform a forward FFT on real data with split output
    pub fn fft_split(&self, in_r: &[T], out_r: &mut [T], out_i: &mut [T]) {
        self.simple_real_fft.fft_split(in_r, out_r, out_i);
    }

    /// Perform an inverse FFT to real data
    pub fn ifft(&mut self, freq: &[Complex<T>], time: &mut [T]) {
        self.simple_real_fft.ifft(freq, time);
    }

    /// Perform an inverse FFT from split complex to real data
    pub fn ifft_split(&self, in_r: &[T], in_i: &[T], out_r: &mut [T]) {
        self.simple_real_fft.ifft_split(in_r, in_i, out_r);
    }
}

#[cfg(test)]
mod tests {
    use num_complex::ComplexFloat;
    use super::*;
    
    #[test]
    fn test_simple_fft() {
        // Create a 4-point FFT
        let mut fft = SimpleFFT::<f32>::new(4);
        
        // Create input and output buffers
        let input = vec![
            Complex::new(1.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(0.0, 0.0),
        ];
        let mut output = vec![Complex::new(0.0, 0.0); 4];
        
        // Perform forward FFT
        fft.fft(&input, &mut output);
        
        // All values should be 1.0 for a delta function input
        for i in 0..4 {
            assert!((output[i].re - 1.0).abs() < 1e-10);
            assert!(output[i].im.abs() < 1e-10);
        }
        
        // Create a new input with a sine wave
        let input = vec![
            Complex::new(0.0, 0.0),
            Complex::new(1.0, 0.0),
            Complex::new(0.0, 0.0),
            Complex::new(-1.0, 0.0),
        ];
        
        // Perform forward FFT
        fft.fft(&input, &mut output);
        
        // For this input, we should have energy at frequency bin 1
        assert!(output[0].abs() < 1e-10);
        assert!((output[1].im + 2.0).abs() < 1e-10);
        assert!(output[2].abs() < 1e-10);
        assert!((output[3].im - 2.0).abs() < 1e-10);
        
        // Test inverse FFT
        let mut inverse_output = vec![Complex::new(0.0, 0.0); 4];
        fft.ifft(&output, &mut inverse_output);
        
        // Scale by 1/N
        for i in 0..4 {
            inverse_output[i] = inverse_output[i] / 4.0;
        }
        
        // Should recover the original signal
        for i in 0..4 {
            assert!((inverse_output[i].re - input[i].re).abs() < 1e-10);
            assert!((inverse_output[i].im - input[i].im).abs() < 1e-10);
        }
    }
    
    #[test]
    fn test_real_fft() {
        // Create an 8-point real FFT
        let mut real_fft = SimpleRealFFT::<f32>::new(8);
        
        // Create input and output buffers
        let input = vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0];
        let mut output = vec![Complex::new(0.0, 0.0); 5]; // Only need N/2+1 for real FFT
        
        // Perform forward FFT
        real_fft.fft(&input, &mut output);
        
        // All values should be 1.0 for a delta function input
        for i in 0..5 {
            assert!((output[i].re - 1.0).abs() < 1e-10);
            assert!(output[i].im.abs() < 1e-10);
        }
        
        // Test inverse FFT
        let mut inverse_output = vec![0.0; 8];
        real_fft.ifft(&output, &mut inverse_output);
        
        // Scale by 1/N
        for i in 0..8 {
            inverse_output[i] /= 8.0;
        }
        
        // Should recover the original signal
        for i in 0..8 {
            assert!((inverse_output[i] - input[i]).abs() < 1e-10);
        }
    }
}