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 crate::{common::FftNum, FftDirection};
use num_complex::Complex;
pub fn compute_twiddle<T: FftNum>(
index: usize,
fft_len: usize,
direction: FftDirection,
) -> Complex<T> {
let constant = -2f64 * std::f64::consts::PI / fft_len as f64;
let angle = constant * index as f64;
let result = Complex {
re: T::from_f64(angle.cos()).unwrap(),
im: T::from_f64(angle.sin()).unwrap(),
};
match direction {
FftDirection::Forward => result,
FftDirection::Inverse => result.conj(),
}
}
pub fn compute_twiddle_floatindex<T: FftNum>(
index: f64,
fft_len: usize,
direction: FftDirection,
) -> Complex<T> {
let constant = -2f64 * std::f64::consts::PI / fft_len as f64;
let angle = constant * index;
let result = Complex {
re: T::from_f64(angle.cos()).unwrap(),
im: T::from_f64(angle.sin()).unwrap(),
};
match direction {
FftDirection::Forward => result,
FftDirection::Inverse => result.conj(),
}
}
pub fn rotate_90<T: FftNum>(value: Complex<T>, direction: FftDirection) -> Complex<T> {
match direction {
FftDirection::Forward => Complex {
re: value.im,
im: -value.re,
},
FftDirection::Inverse => Complex {
re: -value.im,
im: value.re,
},
}
}
#[cfg(test)]
mod unit_tests {
use super::*;
#[test]
fn test_rotate() {
let value = Complex { re: 9.1, im: 2.2 };
let rotated_forward = rotate_90(value, FftDirection::Forward);
let twiddled_forward = value * compute_twiddle(1, 4, FftDirection::Forward);
assert_eq!(value.re, -rotated_forward.im);
assert_eq!(value.im, rotated_forward.re);
assert!(value.re + twiddled_forward.im < 0.0001);
assert!(value.im - rotated_forward.re < 0.0001);
let rotated_forward = rotate_90(value, FftDirection::Inverse);
let twiddled_forward = value * compute_twiddle(1, 4, FftDirection::Inverse);
assert_eq!(value.re, rotated_forward.im);
assert_eq!(value.im, -rotated_forward.re);
assert!(value.re - twiddled_forward.im < 0.0001);
assert!(value.im + rotated_forward.re < 0.0001);
}
}