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
use num_complex::Complex;
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Transform {
Fft,
Ifft,
UnscaledIfft,
SqrtScaledFft,
SqrtScaledIfft,
}
impl Transform {
pub fn is_forward(&self) -> bool {
match self {
Self::Fft | Self::SqrtScaledFft => true,
Self::Ifft | Self::UnscaledIfft | Self::SqrtScaledIfft => false,
}
}
pub fn inverse(&self) -> Option<Self> {
match self {
Self::Fft => Some(Self::Ifft),
Self::Ifft => Some(Self::Fft),
Self::SqrtScaledFft => Some(Self::SqrtScaledIfft),
Self::SqrtScaledIfft => Some(Self::SqrtScaledFft),
Self::UnscaledIfft => None,
}
}
}
pub trait Fft {
type Real: Copy;
fn size(&self) -> usize;
fn transform_in_place(&self, input: &mut [Complex<Self::Real>], transform: Transform);
fn transform(
&self,
input: &[Complex<Self::Real>],
output: &mut [Complex<Self::Real>],
transform: Transform,
) {
assert_eq!(input.len(), self.size());
assert_eq!(output.len(), self.size());
output.copy_from_slice(input);
self.transform_in_place(output, transform);
}
fn fft_in_place(&self, input: &mut [Complex<Self::Real>]) {
self.transform_in_place(input, Transform::Fft);
}
fn ifft_in_place(&self, input: &mut [Complex<Self::Real>]) {
self.transform_in_place(input, Transform::Ifft);
}
fn fft(&self, input: &[Complex<Self::Real>], output: &mut [Complex<Self::Real>]) {
self.transform(input, output, Transform::Fft);
}
fn ifft(&self, input: &[Complex<Self::Real>], output: &mut [Complex<Self::Real>]) {
self.transform(input, output, Transform::Ifft);
}
}