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;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Scale {
None,
SN,
N,
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);
}
}
}
}
#[allow(clippy::len_without_is_empty)]
pub trait Fft {
fn fwd(&mut self, input: &[cf32], output: &mut [cf32], s: Scale);
fn bwd(&mut self, input: &[cf32], output: &mut [cf32], s: Scale);
fn ifwd(&mut self, input: &mut [cf32], s: Scale);
fn ibwd(&mut self, input: &mut [cf32], s: Scale);
fn len(&self) -> usize;
}
#[cfg(feature = "fft_chfft")]
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),
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);
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);
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);
}
}