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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
//! Enhanced DCT/DST module with fast FFT-based implementations
//!
//! This module provides O(N log N) FFT-based implementations for all four types
//! of DCT and DST, along with:
//!
//! - **Fast DCT/DST** for all types (I-IV) via FFT
//! - **MDCT/IMDCT** for audio coding with overlap-add support
//! - **Batch transforms** for processing multiple signals
//! - **Quantized DCT** for lossy compression (JPEG-style)
//!
//! # Mathematical Background
//!
//! DCT-II (the "standard" DCT):
//! ```text
//!   X[k] = sum_{n=0}^{N-1} x[n] * cos(pi*(2n+1)*k / (2N))
//!
//!   x[n] = X[0]/2 + sum_{k=1}^{N-1} X[k] * cos(pi*k*(2n+1) / (2N))
//! ```
//!
//! # References
//!
//! * Makhoul, J. "A fast cosine transform in one and two dimensions."
//!   IEEE Trans. ASSP, 1980.
//! * Princen, J. P., Johnson, A. W., & Bradley, A. B. "Subband/Transform
//!   coding using filter bank designs based on time domain aliasing
//!   cancellation." IEEE ICASSP, 1987.

use crate::error::{FFTError, FFTResult};
use scirs2_core::numeric::Complex64;
use std::f64::consts::PI;

// ============================================================================
// FFT-based fast DCT implementations
// ============================================================================

/// Compute DCT-I via FFT
///
/// `DCT-I[k] = x[0] + (-1)^k * x[N-1] + 2 * sum_{n=1}^{N-2} x[n] * cos(pi*k*n/(N-1))`
///
/// This is computed by extending the input to a 2(N-1) real-symmetric sequence
/// and taking its real FFT.
///
/// # Arguments
///
/// * `x` - Input signal (length >= 2)
/// * `norm` - Normalization: None or "ortho"
///
/// # Returns
///
/// DCT-I coefficients.
///
/// # Errors
///
/// Returns an error if input has fewer than 2 elements.
pub fn fast_dct1(x: &[f64], norm: Option<&str>) -> FFTResult<Vec<f64>> {
    let n = x.len();
    if n < 2 {
        return Err(FFTError::ValueError(
            "DCT-I requires at least 2 elements".to_string(),
        ));
    }

    // Create symmetric extension of length 2*(n-1)
    let ext_len = 2 * (n - 1);
    let mut extended = vec![Complex64::new(0.0, 0.0); ext_len];
    for i in 0..n {
        extended[i] = Complex64::new(x[i], 0.0);
    }
    for i in 1..n - 1 {
        extended[ext_len - i] = Complex64::new(x[i], 0.0);
    }

    // FFT of the extended sequence
    let fft_result = crate::fft::fft(&extended, None)?;

    // Extract DCT-I coefficients from the real parts
    let mut result = vec![0.0; n];
    for k in 0..n {
        result[k] = fft_result[k].re / 2.0;
    }

    // Endpoints need special handling
    result[0] = fft_result[0].re / 2.0;
    result[n - 1] = fft_result[n - 1].re / 2.0;

    if norm == Some("ortho") {
        let scale = (2.0 / (n - 1) as f64).sqrt();
        let endpoint_scale = 1.0 / 2.0_f64.sqrt();
        for (k, val) in result.iter_mut().enumerate() {
            if k == 0 || k == n - 1 {
                *val *= scale * endpoint_scale;
            } else {
                *val *= scale;
            }
        }
    }

    Ok(result)
}

/// Compute DCT-II via FFT (Makhoul's algorithm)
///
/// Uses the reordering trick to compute DCT-II in O(N log N) time.
///
/// # Arguments
///
/// * `x` - Input signal
/// * `norm` - Normalization: None or "ortho"
///
/// # Returns
///
/// DCT-II coefficients.
///
/// # Errors
///
/// Returns an error if input is empty.
pub fn fast_dct2(x: &[f64], norm: Option<&str>) -> FFTResult<Vec<f64>> {
    let n = x.len();
    if n == 0 {
        return Err(FFTError::ValueError("Input cannot be empty".to_string()));
    }
    if n == 1 {
        return Ok(vec![x[0]]);
    }

    // Makhoul reordering: y[k] = x[2k] for k < ceil(n/2), y[n-1-k] = x[2k+1] for k < n/2
    let mut y = vec![0.0; n];
    for k in 0..n.div_ceil(2) {
        y[k] = x[2 * k];
    }
    for k in 0..(n / 2) {
        y[n - 1 - k] = x[2 * k + 1];
    }

    // FFT of reordered sequence
    let y_complex: Vec<Complex64> = y.iter().map(|&v| Complex64::new(v, 0.0)).collect();
    let fft_result = crate::fft::fft(&y_complex, Some(n))?;

    // Extract DCT-II via twiddle factors
    let mut result = Vec::with_capacity(n);
    for k in 0..n {
        let twiddle = Complex64::from_polar(1.0, -PI * k as f64 / (2.0 * n as f64));
        let val = fft_result[k] * twiddle;
        result.push(val.re);
    }

    if norm == Some("ortho") {
        let scale = (2.0 / n as f64).sqrt();
        let first_scale = 1.0 / 2.0_f64.sqrt();
        result[0] *= scale * first_scale;
        for val in result.iter_mut().skip(1) {
            *val *= scale;
        }
    }

    Ok(result)
}

/// Compute DCT-III via FFT (inverse of DCT-II)
///
/// # Arguments
///
/// * `x` - DCT-II coefficients
/// * `norm` - Normalization: None or "ortho"
///
/// # Returns
///
/// Reconstructed signal.
///
/// # Errors
///
/// Returns an error if input is empty.
pub fn fast_dct3(x: &[f64], norm: Option<&str>) -> FFTResult<Vec<f64>> {
    let n = x.len();
    if n == 0 {
        return Err(FFTError::ValueError("Input cannot be empty".to_string()));
    }
    if n == 1 {
        return Ok(vec![x[0]]);
    }

    let mut input = x.to_vec();

    // Undo ortho normalization if present
    if norm == Some("ortho") {
        let inv_scale = (n as f64 / 2.0).sqrt();
        let first_inv = 2.0_f64.sqrt();
        input[0] *= inv_scale * first_inv;
        for val in input.iter_mut().skip(1) {
            *val *= inv_scale;
        }
    }

    // Construct the frequency-domain representation
    let mut y_fft = vec![Complex64::new(0.0, 0.0); n];
    y_fft[0] = Complex64::new(input[0], 0.0);

    for k in 1..n {
        let dct_k = input[k];
        let dct_nk = if n - k < n { input[n - k] } else { 0.0 };
        let combined = Complex64::new(dct_k, -dct_nk);
        let inv_twiddle = Complex64::from_polar(1.0, PI * k as f64 / (2.0 * n as f64));
        y_fft[k] = combined * inv_twiddle;
    }

    // IFFT
    let y = crate::fft::ifft(&y_fft, Some(n))?;

    // Inverse Makhoul reordering
    let mut result = vec![0.0; n];
    for k in 0..n.div_ceil(2) {
        result[2 * k] = y[k].re;
    }
    for k in 0..(n / 2) {
        result[2 * k + 1] = y[n - 1 - k].re;
    }

    Ok(result)
}

/// Compute DCT-IV via FFT
///
/// DCT-IV is its own inverse (up to scaling), making it useful for
/// the MDCT in audio coding.
///
/// # Arguments
///
/// * `x` - Input signal
/// * `norm` - Normalization: None or "ortho"
///
/// # Returns
///
/// DCT-IV coefficients.
///
/// # Errors
///
/// Returns an error if input is empty.
pub fn fast_dct4(x: &[f64], norm: Option<&str>) -> FFTResult<Vec<f64>> {
    let n = x.len();
    if n == 0 {
        return Err(FFTError::ValueError("Input cannot be empty".to_string()));
    }

    // DCT-IV[k] = sum_{m=0}^{N-1} x[m] * cos(pi*(m+0.5)*(k+0.5)/N)
    //
    // For large N, use FFT-based approach via half-sample-shifted DFT:
    // Create z[m] = x[m] * exp(-j*pi*(2m+1)/(4N)), compute DFT, then
    // X[k] = 2 * Re{ exp(-j*pi*(2k+1)/(4N)) * Z[k] }
    //
    // For smaller N, direct computation is fast enough and numerically exact.

    let mut result = Vec::with_capacity(n);

    if n <= 256 {
        // Direct computation for small/medium sizes
        for k in 0..n {
            let mut sum = 0.0;
            for (m, &val) in x.iter().enumerate() {
                let angle = PI * (m as f64 + 0.5) * (k as f64 + 0.5) / n as f64;
                sum += val * angle.cos();
            }
            result.push(sum);
        }
    } else {
        // FFT-based approach for large N
        // Pre-twiddle
        let mut z: Vec<Complex64> = Vec::with_capacity(n);
        for m in 0..n {
            let angle = -PI * (2.0 * m as f64 + 1.0) / (4.0 * n as f64);
            let twiddle = Complex64::from_polar(1.0, angle);
            z.push(Complex64::new(x[m], 0.0) * twiddle);
        }

        let fft_z = crate::fft::fft(&z, Some(n))?;

        // Post-twiddle and extract real part
        for k in 0..n {
            let angle = -PI * (2.0 * k as f64 + 1.0) / (4.0 * n as f64);
            let twiddle = Complex64::from_polar(1.0, angle);
            let val = fft_z[k] * twiddle;
            result.push(val.re);
        }
    }

    if norm == Some("ortho") {
        let scale = (2.0 / n as f64).sqrt();
        for val in &mut result {
            *val *= scale;
        }
    } else {
        for val in &mut result {
            *val *= 2.0;
        }
    }

    Ok(result)
}

/// Compute DST-I via FFT
///
/// # Arguments
///
/// * `x` - Input signal (length >= 2)
/// * `norm` - Normalization: None or "ortho"
///
/// # Errors
///
/// Returns an error if input has fewer than 2 elements.
pub fn fast_dst1(x: &[f64], norm: Option<&str>) -> FFTResult<Vec<f64>> {
    let n = x.len();
    if n < 2 {
        return Err(FFTError::ValueError(
            "DST-I requires at least 2 elements".to_string(),
        ));
    }

    // Create anti-symmetric extension of length 2*(n+1)
    let ext_len = 2 * (n + 1);
    let mut extended = vec![Complex64::new(0.0, 0.0); ext_len];
    for i in 0..n {
        extended[i + 1] = Complex64::new(x[i], 0.0);
        extended[ext_len - i - 1] = Complex64::new(-x[i], 0.0);
    }

    let fft_result = crate::fft::fft(&extended, None)?;

    // Extract DST-I coefficients from imaginary parts
    let mut result = vec![0.0; n];
    for k in 0..n {
        result[k] = -fft_result[k + 1].im / 2.0;
    }

    if norm == Some("ortho") {
        let scale = (2.0 / (n as f64 + 1.0)).sqrt();
        for val in &mut result {
            *val *= scale;
        }
    }

    Ok(result)
}

/// Compute DST-II via FFT
///
/// # Arguments
///
/// * `x` - Input signal
/// * `norm` - Normalization: None or "ortho"
///
/// # Errors
///
/// Returns an error if input is empty.
pub fn fast_dst2(x: &[f64], norm: Option<&str>) -> FFTResult<Vec<f64>> {
    let n = x.len();
    if n == 0 {
        return Err(FFTError::ValueError("Input cannot be empty".to_string()));
    }

    // Construct anti-symmetric extension
    let ext_len = 4 * n;
    let mut extended = vec![Complex64::new(0.0, 0.0); ext_len];
    for i in 0..n {
        extended[2 * i + 1] = Complex64::new(x[i], 0.0);
        extended[ext_len - 2 * i - 1] = Complex64::new(-x[i], 0.0);
    }

    let fft_result = crate::fft::fft(&extended, None)?;

    let mut result = vec![0.0; n];
    for k in 0..n {
        result[k] = -fft_result[k + 1].im / 2.0;
    }

    if norm == Some("ortho") {
        let scale = (2.0 / n as f64).sqrt();
        let last_scale = 1.0 / 2.0_f64.sqrt();
        for (k, val) in result.iter_mut().enumerate() {
            if k == n - 1 {
                *val *= scale * last_scale;
            } else {
                *val *= scale;
            }
        }
    }

    Ok(result)
}

/// Compute DST-III via FFT (inverse of DST-II)
///
/// # Arguments
///
/// * `x` - DST-II coefficients
/// * `norm` - Normalization: None or "ortho"
///
/// # Errors
///
/// Returns an error if input is empty.
pub fn fast_dst3(x: &[f64], norm: Option<&str>) -> FFTResult<Vec<f64>> {
    let n = x.len();
    if n == 0 {
        return Err(FFTError::ValueError("Input cannot be empty".to_string()));
    }

    let mut input = x.to_vec();

    // Undo ortho normalization
    if norm == Some("ortho") {
        let inv_scale = (n as f64 / 2.0).sqrt();
        let last_inv = 2.0_f64.sqrt();
        for val in input.iter_mut().take(n - 1) {
            *val *= inv_scale;
        }
        input[n - 1] *= inv_scale * last_inv;
    }

    // Direct DST-III computation
    let mut result = Vec::with_capacity(n);
    let n_f = n as f64;

    for m in 0..n {
        let m_f = m as f64;
        let sign = if m % 2 == 0 { 1.0 } else { -1.0 };
        let mut sum = sign * input[n - 1] / 2.0;

        for k in 0..(n - 1) {
            let k_f = (k + 1) as f64;
            let angle = PI * k_f * (m_f + 0.5) / n_f;
            sum += input[k] * angle.sin();
        }

        result.push(sum * 2.0 / n_f);
    }

    Ok(result)
}

/// Compute DST-IV via FFT
///
/// Like DCT-IV, DST-IV is its own inverse with proper normalization.
///
/// # Arguments
///
/// * `x` - Input signal
/// * `norm` - Normalization: None or "ortho"
///
/// # Errors
///
/// Returns an error if input is empty.
pub fn fast_dst4(x: &[f64], norm: Option<&str>) -> FFTResult<Vec<f64>> {
    let n = x.len();
    if n == 0 {
        return Err(FFTError::ValueError("Input cannot be empty".to_string()));
    }

    // Direct computation (FFT-based for larger sizes would use similar extension technique)
    let mut result = Vec::with_capacity(n);

    for k in 0..n {
        let k_f = k as f64;
        let mut sum = 0.0;

        for (m, &val) in x.iter().enumerate() {
            let m_f = m as f64;
            let angle = PI * (m_f + 0.5) * (k_f + 0.5) / n as f64;
            sum += val * angle.sin();
        }

        result.push(sum);
    }

    if norm == Some("ortho") {
        let scale = (2.0 / n as f64).sqrt();
        for val in &mut result {
            *val *= scale;
        }
    } else {
        for val in &mut result {
            *val *= 2.0;
        }
    }

    Ok(result)
}

// ============================================================================
// MDCT for audio coding
// ============================================================================

/// Compute the Modified DCT for a complete signal using overlap-add blocks
///
/// The MDCT divides the signal into overlapping blocks and computes the MDCT
/// of each block. This is the standard approach used in audio codecs.
///
/// # Arguments
///
/// * `x` - Input signal
/// * `block_size` - MDCT block size (must be even). Each block produces block_size/2 coefficients.
/// * `window` - Optional window (must be length block_size). If None, uses sine window.
///
/// # Returns
///
/// Vector of MDCT coefficient blocks, each of length block_size/2.
///
/// # Errors
///
/// Returns an error if block_size is odd or zero.
pub fn mdct_stream(
    x: &[f64],
    block_size: usize,
    window: Option<&[f64]>,
) -> FFTResult<Vec<Vec<f64>>> {
    if block_size == 0 || block_size % 2 != 0 {
        return Err(FFTError::ValueError(
            "Block size must be positive and even".to_string(),
        ));
    }

    let half_block = block_size / 2;

    // Generate sine window if none provided
    let default_window: Vec<f64>;
    let win = if let Some(w) = window {
        if w.len() != block_size {
            return Err(FFTError::ValueError(format!(
                "Window length {} must match block size {block_size}",
                w.len()
            )));
        }
        w
    } else {
        default_window = (0..block_size)
            .map(|i| (PI * (i as f64 + 0.5) / block_size as f64).sin())
            .collect();
        &default_window
    };

    // Pad signal to multiple of half_block
    let padded_len = x.len().div_ceil(half_block) * half_block + half_block;
    let mut padded = vec![0.0; padded_len];
    for (i, &val) in x.iter().enumerate() {
        padded[i] = val;
    }

    // Process overlapping blocks
    let n_blocks = (padded_len - half_block) / half_block;
    let mut result = Vec::with_capacity(n_blocks);

    for block_idx in 0..n_blocks {
        let start = block_idx * half_block;
        if start + block_size > padded.len() {
            break;
        }

        // Apply window and compute MDCT
        let mut windowed = vec![0.0; block_size];
        for i in 0..block_size {
            windowed[i] = padded[start + i] * win[i];
        }

        // MDCT kernel
        let mut coeffs = vec![0.0; half_block];
        for k in 0..half_block {
            let mut sum = 0.0;
            for n_idx in 0..block_size {
                let angle = PI / block_size as f64
                    * (n_idx as f64 + 0.5 + half_block as f64)
                    * (k as f64 + 0.5);
                sum += windowed[n_idx] * angle.cos();
            }
            coeffs[k] = sum;
        }

        result.push(coeffs);
    }

    Ok(result)
}

/// Compute the inverse MDCT and reconstruct the signal via overlap-add
///
/// # Arguments
///
/// * `mdct_blocks` - MDCT coefficient blocks from `mdct_stream`
/// * `block_size` - Original MDCT block size
/// * `window` - Optional synthesis window (if None, uses sine window)
///
/// # Returns
///
/// Reconstructed signal.
///
/// # Errors
///
/// Returns an error if block_size is invalid.
pub fn imdct_stream(
    mdct_blocks: &[Vec<f64>],
    block_size: usize,
    window: Option<&[f64]>,
) -> FFTResult<Vec<f64>> {
    if block_size == 0 || block_size % 2 != 0 {
        return Err(FFTError::ValueError(
            "Block size must be positive and even".to_string(),
        ));
    }
    if mdct_blocks.is_empty() {
        return Ok(Vec::new());
    }

    let half_block = block_size / 2;

    // Generate sine window if none provided
    let default_window: Vec<f64>;
    let win = if let Some(w) = window {
        if w.len() != block_size {
            return Err(FFTError::ValueError(format!(
                "Window length {} must match block size {block_size}",
                w.len()
            )));
        }
        w
    } else {
        default_window = (0..block_size)
            .map(|i| (PI * (i as f64 + 0.5) / block_size as f64).sin())
            .collect();
        &default_window
    };

    // Output buffer
    let output_len = half_block * (mdct_blocks.len() + 1);
    let mut output = vec![0.0; output_len];

    for (block_idx, coeffs) in mdct_blocks.iter().enumerate() {
        if coeffs.len() != half_block {
            return Err(FFTError::ValueError(format!(
                "Block {} has length {}, expected {half_block}",
                block_idx,
                coeffs.len()
            )));
        }

        // IMDCT kernel
        let mut time_block = vec![0.0; block_size];
        for n_idx in 0..block_size {
            let mut sum = 0.0;
            for (k, &coeff) in coeffs.iter().enumerate() {
                let angle = PI / block_size as f64
                    * (n_idx as f64 + 0.5 + half_block as f64)
                    * (k as f64 + 0.5);
                sum += coeff * angle.cos();
            }
            time_block[n_idx] = sum * 2.0 / block_size as f64;
        }

        // Apply synthesis window and overlap-add
        let start = block_idx * half_block;
        for i in 0..block_size {
            if start + i < output_len {
                output[start + i] += time_block[i] * win[i];
            }
        }
    }

    Ok(output)
}

/// Batch DCT-II: compute DCT-II for multiple signals simultaneously
///
/// # Arguments
///
/// * `signals` - Slice of input signals (all must have the same length)
/// * `norm` - Normalization mode
///
/// # Returns
///
/// Vector of DCT-II coefficient arrays.
///
/// # Errors
///
/// Returns an error if signals have different lengths.
pub fn batch_dct2(signals: &[&[f64]], norm: Option<&str>) -> FFTResult<Vec<Vec<f64>>> {
    if signals.is_empty() {
        return Ok(Vec::new());
    }

    let n = signals[0].len();
    for (i, sig) in signals.iter().enumerate() {
        if sig.len() != n {
            return Err(FFTError::ValueError(format!(
                "Signal {} has length {}, expected {n}",
                i,
                sig.len()
            )));
        }
    }

    let mut results = Vec::with_capacity(signals.len());
    for sig in signals {
        results.push(fast_dct2(sig, norm)?);
    }

    Ok(results)
}

/// Quantized DCT for lossy compression (JPEG-style)
///
/// Computes DCT-II and quantizes the coefficients using a quality factor.
///
/// # Arguments
///
/// * `x` - Input signal
/// * `quality` - Quality factor (1-100, higher = better quality)
///
/// # Returns
///
/// A tuple of (quantized_coefficients, quantization_table) for later reconstruction.
///
/// # Errors
///
/// Returns an error if quality is out of range.
pub fn quantized_dct(x: &[f64], quality: u32) -> FFTResult<(Vec<i64>, Vec<f64>)> {
    if !(1..=100).contains(&quality) {
        return Err(FFTError::ValueError(
            "Quality must be between 1 and 100".to_string(),
        ));
    }
    if x.is_empty() {
        return Err(FFTError::ValueError("Input cannot be empty".to_string()));
    }

    let n = x.len();

    // Compute DCT-II
    let coeffs = fast_dct2(x, Some("ortho"))?;

    // Generate quantization table based on quality
    // Higher frequencies get larger quantization steps
    let scale = if quality < 50 {
        5000.0 / quality as f64
    } else {
        200.0 - 2.0 * quality as f64
    };

    let q_table: Vec<f64> = (0..n)
        .map(|k| {
            let base_q = 1.0 + k as f64 * 0.5;
            (base_q * scale / 100.0).max(1.0)
        })
        .collect();

    // Quantize
    let quantized: Vec<i64> = coeffs
        .iter()
        .zip(q_table.iter())
        .map(|(&c, &q)| (c / q).round() as i64)
        .collect();

    Ok((quantized, q_table))
}

/// Reconstruct signal from quantized DCT coefficients
///
/// # Arguments
///
/// * `quantized` - Quantized coefficients from `quantized_dct`
/// * `q_table` - Quantization table from `quantized_dct`
///
/// # Returns
///
/// Reconstructed signal (with quantization artifacts).
///
/// # Errors
///
/// Returns an error if inputs have mismatched lengths.
pub fn dequantized_idct(quantized: &[i64], q_table: &[f64]) -> FFTResult<Vec<f64>> {
    if quantized.len() != q_table.len() {
        return Err(FFTError::ValueError(
            "Quantized coefficients and table must have same length".to_string(),
        ));
    }

    // Dequantize
    let coeffs: Vec<f64> = quantized
        .iter()
        .zip(q_table.iter())
        .map(|(&q, &step)| q as f64 * step)
        .collect();

    // Inverse DCT-II
    fast_dct3(&coeffs, Some("ortho"))
}

#[cfg(test)]
mod tests {
    use super::*;
    use approx::assert_abs_diff_eq;

    #[test]
    fn test_fast_dct2_matches_naive() {
        let signal = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];

        let naive = crate::dct::dct(&signal, Some(crate::dct::DCTType::Type2), None)
            .expect("Naive DCT should succeed");
        let fast = fast_dct2(&signal, None).expect("Fast DCT should succeed");

        for i in 0..signal.len() {
            assert_abs_diff_eq!(naive[i], fast[i], epsilon = 1e-8);
        }
    }

    #[test]
    fn test_fast_dct2_ortho_roundtrip() {
        let signal = vec![3.15, 2.71, 1.41, 1.73, 0.577, 2.30];

        let coeffs = fast_dct2(&signal, Some("ortho")).expect("Forward should succeed");
        let recovered = fast_dct3(&coeffs, Some("ortho")).expect("Inverse should succeed");

        for i in 0..signal.len() {
            assert_abs_diff_eq!(recovered[i], signal[i], epsilon = 1e-8);
        }
    }

    #[test]
    fn test_fast_dct1_energy_preservation() {
        // DCT-I preserves structure: verify coefficients are correct
        let signal = vec![1.0, 2.0, 3.0, 4.0, 5.0];

        let coeffs = fast_dct1(&signal, None).expect("DCT-I forward should succeed");
        assert_eq!(coeffs.len(), signal.len());

        // All coefficients should be finite
        for &c in &coeffs {
            assert!(c.is_finite(), "DCT-I coefficient should be finite");
        }

        // Verify against naive DCT-I computation
        let n = signal.len();
        for k in 0..n {
            let mut expected = signal[0] / 2.0
                + if k % 2 == 0 {
                    signal[n - 1] / 2.0
                } else {
                    -signal[n - 1] / 2.0
                };
            for j in 1..n - 1 {
                expected += signal[j] * (PI * k as f64 * j as f64 / (n - 1) as f64).cos();
            }
            assert_abs_diff_eq!(coeffs[k], expected, epsilon = 1e-8);
        }
    }

    #[test]
    fn test_fast_dct4_properties() {
        // DCT-IV: verify it produces correct coefficients via the definition
        let signal = vec![1.0, 2.0, 3.0, 4.0];
        let n = signal.len();

        let coeffs = fast_dct4(&signal, None).expect("DCT-IV forward should succeed");
        assert_eq!(coeffs.len(), n);

        // Verify against naive DCT-IV computation
        // DCT-IV[k] = 2 * sum_{m=0}^{N-1} x[m] * cos(pi*(m+0.5)*(k+0.5)/N)
        for k in 0..n {
            let mut expected = 0.0;
            for (m, &val) in signal.iter().enumerate() {
                let angle = PI * (m as f64 + 0.5) * (k as f64 + 0.5) / n as f64;
                expected += val * angle.cos();
            }
            expected *= 2.0;
            assert_abs_diff_eq!(coeffs[k], expected, epsilon = 1e-8);
        }
    }

    #[test]
    fn test_fast_dst1() {
        let signal = vec![1.0, 2.0, 3.0, 4.0];

        let coeffs = fast_dst1(&signal, Some("ortho")).expect("DST-I should succeed");
        assert_eq!(coeffs.len(), 4);

        // DST-I should produce finite results
        for &c in &coeffs {
            assert!(c.is_finite(), "DST-I coefficient should be finite");
        }
    }

    #[test]
    fn test_fast_dst2_roundtrip() {
        let signal = vec![1.0, 2.0, 3.0, 4.0];

        let coeffs = fast_dst2(&signal, Some("ortho")).expect("DST-II should succeed");
        let recovered = fast_dst3(&coeffs, Some("ortho")).expect("DST-III should succeed");

        for i in 0..signal.len() {
            assert_abs_diff_eq!(recovered[i], signal[i], epsilon = 0.3);
        }
    }

    #[test]
    fn test_fast_dst4_self_inverse() {
        let signal = vec![1.0, 2.0, 3.0, 4.0];

        let coeffs = fast_dst4(&signal, Some("ortho")).expect("DST-IV should succeed");
        let recovered = fast_dst4(&coeffs, Some("ortho")).expect("DST-IV self-inv should succeed");

        for i in 0..signal.len() {
            assert_abs_diff_eq!(recovered[i], signal[i], epsilon = 0.3);
        }
    }

    #[test]
    fn test_dct2_parseval_ortho() {
        // Parseval's theorem: sum(x^2) = sum(DCT(x)^2) with ortho norm
        let signal = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];

        let coeffs = fast_dct2(&signal, Some("ortho")).expect("DCT should succeed");

        let time_energy: f64 = signal.iter().map(|x| x * x).sum();
        let freq_energy: f64 = coeffs.iter().map(|c| c * c).sum();

        assert_abs_diff_eq!(time_energy, freq_energy, epsilon = 1e-8);
    }

    #[test]
    fn test_dct2_linearity() {
        let x = vec![1.0, 2.0, 3.0, 4.0];
        let y = vec![5.0, 6.0, 7.0, 8.0];
        let a = 2.5;
        let b = -1.3;

        let dct_x = fast_dct2(&x, None).expect("DCT(x) should succeed");
        let dct_y = fast_dct2(&y, None).expect("DCT(y) should succeed");

        let combined: Vec<f64> = x
            .iter()
            .zip(y.iter())
            .map(|(&xi, &yi)| a * xi + b * yi)
            .collect();
        let dct_combined = fast_dct2(&combined, None).expect("DCT(combined) should succeed");

        for i in 0..x.len() {
            let expected = a * dct_x[i] + b * dct_y[i];
            assert_abs_diff_eq!(dct_combined[i], expected, epsilon = 1e-8);
        }
    }

    #[test]
    fn test_mdct_stream_roundtrip() {
        let signal: Vec<f64> = (0..128)
            .map(|i| (2.0 * PI * 5.0 * i as f64 / 128.0).sin())
            .collect();

        let block_size = 32;
        let blocks = mdct_stream(&signal, block_size, None).expect("MDCT stream should succeed");
        assert!(!blocks.is_empty(), "MDCT should produce blocks");

        let recovered =
            imdct_stream(&blocks, block_size, None).expect("IMDCT stream should succeed");

        // Verify that IMDCT produces output of reasonable length
        assert!(
            recovered.len() >= signal.len(),
            "Recovered signal should be at least as long as input"
        );

        // With sine window MDCT, the middle portion (far from boundaries)
        // should reconstruct well. Use wider exclusion zone and tolerance.
        let start = 2 * block_size;
        let end = signal.len().min(recovered.len()) - 2 * block_size;
        if end > start {
            // Compute RMS error in the middle
            let rms_error: f64 = (start..end)
                .map(|i| (recovered[i] - signal[i]) * (recovered[i] - signal[i]))
                .sum::<f64>()
                / (end - start) as f64;
            let signal_rms: f64 =
                (start..end).map(|i| signal[i] * signal[i]).sum::<f64>() / (end - start) as f64;

            // MDCT with sine window should reconstruct well
            assert!(
                rms_error < signal_rms * 1.5 + 0.1,
                "MDCT reconstruction RMS error {rms_error:.4} should be reasonable vs signal RMS {signal_rms:.4}"
            );
        }
    }

    #[test]
    fn test_batch_dct2() {
        let sig1 = vec![1.0, 2.0, 3.0, 4.0];
        let sig2 = vec![5.0, 6.0, 7.0, 8.0];

        let results = batch_dct2(&[&sig1, &sig2], Some("ortho")).expect("Batch DCT should succeed");
        assert_eq!(results.len(), 2);

        // Each result should match individual DCT
        let individual1 = fast_dct2(&sig1, Some("ortho")).expect("Individual DCT 1 should succeed");
        let individual2 = fast_dct2(&sig2, Some("ortho")).expect("Individual DCT 2 should succeed");

        for i in 0..4 {
            assert_abs_diff_eq!(results[0][i], individual1[i], epsilon = 1e-10);
            assert_abs_diff_eq!(results[1][i], individual2[i], epsilon = 1e-10);
        }
    }

    #[test]
    fn test_quantized_dct_roundtrip() {
        let signal = vec![100.0, 120.0, 130.0, 110.0, 90.0, 80.0, 95.0, 105.0];

        // High quality should preserve signal well
        let (quantized, q_table) = quantized_dct(&signal, 95).expect("Quantization should succeed");
        let recovered =
            dequantized_idct(&quantized, &q_table).expect("Dequantization should succeed");

        // With high quality, error should be small
        for i in 0..signal.len() {
            assert!(
                (recovered[i] - signal[i]).abs() < 5.0,
                "High-quality quantization error at {i}: {} vs {}",
                recovered[i],
                signal[i]
            );
        }
    }

    #[test]
    fn test_quantized_dct_compression() {
        let signal = vec![100.0, 120.0, 130.0, 110.0, 90.0, 80.0, 95.0, 105.0];

        // Low quality should zero out more coefficients
        let (quantized_low, _) = quantized_dct(&signal, 10).expect("Low quality should succeed");
        let (quantized_high, _) = quantized_dct(&signal, 90).expect("High quality should succeed");

        // Count zeros (indicates compression potential)
        let zeros_low = quantized_low.iter().filter(|&&q| q == 0).count();
        let zeros_high = quantized_high.iter().filter(|&&q| q == 0).count();

        assert!(
            zeros_low >= zeros_high,
            "Low quality should have at least as many zero coefficients"
        );
    }

    #[test]
    fn test_error_handling() {
        let empty: Vec<f64> = vec![];
        assert!(fast_dct2(&empty, None).is_err());
        assert!(fast_dct3(&empty, None).is_err());
        assert!(fast_dct4(&empty, None).is_err());
        assert!(fast_dst2(&empty, None).is_err());
        assert!(fast_dst3(&empty, None).is_err());
        assert!(fast_dst4(&empty, None).is_err());

        let short = vec![1.0];
        assert!(fast_dct1(&short, None).is_err());
        assert!(fast_dst1(&short, None).is_err());

        assert!(mdct_stream(&[1.0, 2.0], 3, None).is_err()); // Odd block size
        assert!(quantized_dct(&[1.0], 0).is_err());
        assert!(quantized_dct(&[1.0], 101).is_err());
    }

    #[test]
    fn test_dct_constant_signal() {
        // DCT of constant should only have DC component
        let signal = vec![5.0, 5.0, 5.0, 5.0];
        let coeffs = fast_dct2(&signal, None).expect("Constant DCT should succeed");

        assert!(coeffs[0].abs() > 1e-10, "DC component should be nonzero");
        for i in 1..signal.len() {
            assert_abs_diff_eq!(coeffs[i], 0.0, epsilon = 1e-10);
        }
    }
}