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
use crate::cf32;
use crate::vecops::VecOps;

/// Scaling Policy for Transforms
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Scale {
    /// Scale with 1 (no scaling)
    None,
    /// Multiplies with 1/sqrt(N)
    /// with ```N```: transform length
    /// Commonly used for symmetric spectra
    SN,
    /// Multiplies with 1/N
    /// with ```N```: transform length
    N,
    /// Multiplies with given scaling factor X
    X(f32),
}

impl Scale {
    pub fn scale(self, data: &mut [cf32]) {
        match self {
            Scale::None => (),
            Scale::SN => {
                let s = (data.len() as f32).sqrt().recip();
                data.vec_scale(s);
            }
            Scale::N => {
                let s = (data.len() as f32).recip();
                data.vec_scale(s);
            }
            Scale::X(s) => {
                data.vec_scale(s);
            }
        }
    }
}

/// Wrapper to be implemented for different fft implementations
/// For example for use in VecOps
/// FFT and input must be the same length
#[allow(clippy::len_without_is_empty)]
pub trait Fft {
    /// FFT (Forward) from ```input``` to ```output```  
    /// Does not modify contents of ```input```
    fn fwd(&mut self, input: &[cf32], output: &mut [cf32], s: Scale);

    /// iFFT (Backward) from ```input``` to ```output```  
    /// Does not modify contents of ```input```
    fn bwd(&mut self, input: &[cf32], output: &mut [cf32], s: Scale);

    /// In-place FFT (Forward)  
    /// Overwrites the ```input``` with the output of the transform
    fn ifwd(&mut self, input: &mut [cf32], s: Scale);

    /// In-place iFFT (Backward)  
    /// Overwrites the input with the output of the transform
    fn ibwd(&mut self, input: &mut [cf32], s: Scale);

    /// Retrieve the (fixed) size (number of bins) this is generated for
    fn len(&self) -> usize;
}

#[cfg(feature = "fft_chfft")]
/// Complex fft
pub use self::ch::Cfft;

#[cfg(feature = "fft_chfft")]
mod ch {
    extern crate chfft;
    use super::{Fft, Scale};

    use chfft::CFft1D;
    use crate::cf32;
    use crate::vecops::VecOps;

    pub struct Cfft {
        fft: CFft1D<f32>,
        tmp: Vec<cf32>,
        len: usize,
    }

    impl Cfft {
        pub fn with_len(len: usize) -> Cfft {
            Cfft {
                fft: CFft1D::<f32>::with_len(len),
                // TODO: use vec_align
                tmp: vec![cf32::default(); len],
                len,
            }
        }
    }

    impl Fft for Cfft {
        fn fwd(&mut self, input: &[cf32], output: &mut [cf32], s: Scale) {
            assert_eq!(
                self.len,
                input.len(),
                "Input and FFT must be the same length"
            );
            self.tmp.vec_clone(&input);
            self.fft.forward0i(&mut self.tmp);
            // OPT: optimize by scaling as its copied over so we do not have to read this stuff twice
            output.vec_clone(&self.tmp);
            s.scale(output);
        }

        fn bwd(&mut self, input: &[cf32], output: &mut [cf32], s: Scale) {
            assert_eq!(
                self.len,
                input.len(),
                "Input and FFT must be the same length"
            );
            self.tmp.vec_clone(&input);
            self.fft.backward0i(&mut self.tmp);
            output.vec_clone(&self.tmp);
            // OPT: optimize by scaling as its copied over so we do not have to read this stuff twice
            s.scale(output);
        }

        fn ifwd(&mut self, input: &mut [cf32], s: Scale) {
            assert_eq!(
                self.len,
                input.len(),
                "Input and FFT must be the same length"
            );
            self.fft.forward0i(input);
            s.scale(input);
        }

        fn ibwd(&mut self, input: &mut [cf32], s: Scale) {
            assert_eq!(
                self.len,
                input.len(),
                "Input and FFT must be the same length"
            );
            self.fft.backward0i(input);
            s.scale(input);
        }

        fn len(&self) -> usize {
            self.len
        }
    }

}

#[cfg(test)]
mod test {
    use super::Scale;
    use crate::cf32;

    #[test]
    fn scale() {
        let input = vec![cf32::new(4.0, 0.0); 4];

        let no = Scale::None;
        let mut nos = input.clone();
        no.scale(&mut nos);
        assert_evm!(nos, &input, 80.0);

        let sn = Scale::SN;
        let snc = vec![cf32::new(2.0, 0.0); 4];
        let mut sns = input.clone();
        sn.scale(&mut sns);
        assert_evm!(sns, snc, 80.0);

        let n = Scale::N;
        let nc = vec![cf32::new(1.0, 0.0); 4];
        let mut ns = input.clone();
        n.scale(&mut ns);
        assert_evm!(ns, nc, 80.0);

        let x = Scale::X(2.0);
        let xc = vec![cf32::new(8.0, 0.0); 4];
        let mut xs = input.clone();
        x.scale(&mut xs);
        assert_evm!(xs, xc, 80.0);
    }
}