Debian Codegraph @

.cargo/vendor/rustfft/src/test_utils.rs

raw
1 use num_complex::Complex;
2 use num_traits::{Float, One, Zero};
3
4 use rand::distributions::{uniform::SampleUniform, Distribution, Uniform};
5 use rand::{rngs::StdRng, SeedableRng};
6
7 use crate::{algorithm::Dft, Direction, FftNum, Length};
8 use crate::{Fft, FftDirection};
9
10 /// The seed for the random number generator used to generate
11 /// random signals. It's defined here so that we have deterministic
12 /// tests
13 const RNG_SEED: [u8; 32] = [
14 1, 9, 1, 0, 1, 1, 4, 3, 1, 4, 9, 8, 4, 1, 4, 8, 2, 8, 1, 2, 2, 2, 6, 1, 2, 3, 4, 5, 6, 7, 8, 9,
15 ];
16
17 pub fn random_signal<T: FftNum + SampleUniform>(length: usize) -> Vec<Complex<T>> {
18 let mut sig = Vec::with_capacity(length);
19 let normal_dist: Uniform<T> = Uniform::new(T::zero(), T::from_f32(10.0).unwrap());
20 let mut rng: StdRng = SeedableRng::from_seed(RNG_SEED);
21 for _ in 0..length {
22 sig.push(Complex {
23 re: normal_dist.sample(&mut rng),
24 im: normal_dist.sample(&mut rng),
25 });
26 }
27 return sig;
28 }
29
30 pub fn compare_vectors<T: FftNum + Float>(vec1: &[Complex<T>], vec2: &[Complex<T>]) -> bool {
31 assert_eq!(vec1.len(), vec2.len());
32 let mut error = T::zero();
33 for (&a, &b) in vec1.iter().zip(vec2.iter()) {
34 error = error + (a - b).norm();
35 }
36 return (error.to_f64().unwrap() / vec1.len() as f64) < 0.1f64;
37 }
38
39 #[allow(unused)]
40 fn transppose_diagnostic<T: FftNum + Float>(expected: &[Complex<T>], actual: &[Complex<T>]) {
41 for (i, (&e, &a)) in expected.iter().zip(actual.iter()).enumerate() {
42 if (e - a).norm().to_f32().unwrap() > 0.01 {
43 if let Some(found_index) = expected
44 .iter()
45 .position(|&ev| (ev - a).norm().to_f32().unwrap() < 0.01)
46 {
47 println!("{} incorrectly contained {}", i, found_index);
48 } else {
49 println!("{} X", i);
50 }
51 }
52 }
53 }
54
55 pub fn check_fft_algorithm<T: FftNum + Float + SampleUniform>(
56 fft: &dyn Fft<T>,
57 len: usize,
58 direction: FftDirection,
59 ) {
60 assert_eq!(
61 fft.len(),
62 len,
63 "Algorithm reported incorrect size. Expected {}, got {}",
64 len,
65 fft.len()
66 );
67 assert_eq!(
68 fft.fft_direction(),
69 direction,
70 "Algorithm reported incorrect FFT direction"
71 );
72
73 let n = 3;
74
75 //test the forward direction
76 let dft = Dft::new(len, direction);
77
78 let dirty_scratch_value = Complex::one() * T::from_i32(100).unwrap();
79
80 // set up buffers
81 let reference_input = random_signal(len * n);
82 let mut expected_output = reference_input.clone();
83 let mut dft_scratch = vec![Zero::zero(); dft.get_inplace_scratch_len()];
84 dft.process_with_scratch(&mut expected_output, &mut dft_scratch);
85
86 // test process()
87 {
88 let mut buffer = reference_input.clone();
89
90 fft.process(&mut buffer);
91
92 if !compare_vectors(&expected_output, &buffer) {
93 dbg!(&expected_output);
94 dbg!(&buffer);
95
96 panic!(
97 "process() failed, length = {}, direction = {}",
98 len, direction
99 );
100 }
101 }
102
103 // test process_with_scratch()
104 {
105 let mut buffer = reference_input.clone();
106 let mut scratch = vec![Zero::zero(); fft.get_inplace_scratch_len()];
107
108 fft.process_with_scratch(&mut buffer, &mut scratch);
109
110 assert!(
111 compare_vectors(&expected_output, &buffer),
112 "process_with_scratch() failed, length = {}, direction = {}",
113 len,
114 direction
115 );
116
117 // make sure this algorithm works correctly with dirty scratch
118 if scratch.len() > 0 {
119 for item in scratch.iter_mut() {
120 *item = dirty_scratch_value;
121 }
122 buffer.copy_from_slice(&reference_input);
123
124 fft.process_with_scratch(&mut buffer, &mut scratch);
125
126 assert!(compare_vectors(&expected_output, &buffer), "process_with_scratch() failed the 'dirty scratch' test, length = {}, direction = {}", len, direction);
127 }
128 }
129
130 // test process_outofplace_with_scratch()
131 {
132 let mut input = reference_input.clone();
133 let mut scratch = vec![Zero::zero(); fft.get_outofplace_scratch_len()];
134 let mut output = vec![Zero::zero(); n * len];
135
136 fft.process_outofplace_with_scratch(&mut input, &mut output, &mut scratch);
137
138 assert!(
139 compare_vectors(&expected_output, &output),
140 "process_outofplace_with_scratch() failed, length = {}, direction = {}",
141 len,
142 direction
143 );
144
145 // make sure this algorithm works correctly with dirty scratch
146 if scratch.len() > 0 {
147 for item in scratch.iter_mut() {
148 *item = dirty_scratch_value;
149 }
150 input.copy_from_slice(&reference_input);
151
152 fft.process_outofplace_with_scratch(&mut input, &mut output, &mut scratch);
153
154 assert!(
155 compare_vectors(&expected_output, &output),
156 "process_outofplace_with_scratch() failed the 'dirty scratch' test, length = {}, direction = {}",
157 len,
158 direction
159 );
160 }
161 }
162 }
163
164 // A fake FFT algorithm that requests much more scratch than it needs. You can use this as an inner FFT to other algorithms to test their scratch-supplying logic
165 #[derive(Debug)]
166 pub struct BigScratchAlgorithm {
167 pub len: usize,
168
169 pub inplace_scratch: usize,
170 pub outofplace_scratch: usize,
171
172 pub direction: FftDirection,
173 }
174 impl<T: FftNum> Fft<T> for BigScratchAlgorithm {
175 fn process_with_scratch(&self, _buffer: &mut [Complex<T>], scratch: &mut [Complex<T>]) {
176 assert!(
177 scratch.len() >= self.inplace_scratch,
178 "Not enough inplace scratch provided, self={:?}, provided scratch={}",
179 &self,
180 scratch.len()
181 );
182 }
183 fn process_outofplace_with_scratch(
184 &self,
185 _input: &mut [Complex<T>],
186 _output: &mut [Complex<T>],
187 scratch: &mut [Complex<T>],
188 ) {
189 assert!(
190 scratch.len() >= self.outofplace_scratch,
191 "Not enough OOP scratch provided, self={:?}, provided scratch={}",
192 &self,
193 scratch.len()
194 );
195 }
196 fn get_inplace_scratch_len(&self) -> usize {
197 self.inplace_scratch
198 }
199 fn get_outofplace_scratch_len(&self) -> usize {
200 self.outofplace_scratch
201 }
202 }
203 impl Length for BigScratchAlgorithm {
204 fn len(&self) -> usize {
205 self.len
206 }
207 }
208 impl Direction for BigScratchAlgorithm {
209 fn fft_direction(&self) -> FftDirection {
210 self.direction
211 }
212 }