blob: 5c1424663f993abda9abfa395b0cc71210f37a89 (
plain)
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
|
use num_complex::Complex;
use rustc_hash::FxHashSet;
#[inline(always)]
pub fn retain_top_n_magnitudes(spectrum: &mut [Complex<f32>], n: usize, complement: bool) {
// you'd be keeping the entire signal
if n >= spectrum.len() && !complement {
return;
}
if n == 0 && complement {
return;
}
// keeping nothing at all
if n == 0 && !complement {
for phasor in spectrum {
*phasor = Complex::ZERO;
}
return;
}
if n >= spectrum.len() && complement {
for phasor in spectrum {
*phasor = Complex::ZERO;
}
return;
}
let mut indexed = spectrum
.iter()
.copied()
.enumerate()
.collect::<Vec<(usize, Complex<f32>)>>();
let target = spectrum.len() - n;
indexed.select_nth_unstable_by(target, |(_, c0), (_, c1)| c0.norm().total_cmp(&c1.norm()));
let top_indices = indexed[target..]
.iter()
.map(|&(i, _)| i)
.collect::<FxHashSet<usize>>();
for (i, phasor) in spectrum.iter_mut().enumerate() {
if top_indices.contains(&i) && complement {
*phasor = Complex::ZERO;
}
if !top_indices.contains(&i) && !complement {
*phasor = Complex::ZERO;
}
}
}
|