]>
Commit | Line | Data |
---|---|---|
e35062e7 KS |
1 | use std::f32::consts; |
2 | ||
3 | #[derive(Debug,Clone,Copy,PartialEq)] | |
4 | pub enum WindowType { | |
5 | Square, | |
6 | Sine, | |
9669f269 | 7 | KaiserBessel(f32), |
e35062e7 KS |
8 | } |
9 | ||
10 | pub fn generate_window(mode: WindowType, scale: f32, size: usize, half: bool, dst: &mut [f32]) { | |
11 | match mode { | |
12 | WindowType::Square => { | |
13 | for n in 0..size { dst[n] = scale; } | |
14 | }, | |
15 | WindowType::Sine => { | |
fdb4b2fb KS |
16 | let param = if half { |
17 | consts::PI / ((2 * size) as f32) | |
18 | } else { | |
19 | consts::PI / (size as f32) | |
20 | }; | |
e35062e7 KS |
21 | for n in 0..size { |
22 | dst[n] = (((n as f32) + 0.5) * param).sin() * scale; | |
23 | } | |
24 | }, | |
9669f269 KS |
25 | WindowType::KaiserBessel(alpha) => { |
26 | let dlen = if half { size as f32 } else { (size as f32) * 0.5 }; | |
fdb4b2fb | 27 | let alpha2 = f64::from((alpha * consts::PI / dlen) * (alpha * consts::PI / dlen)); |
9669f269 KS |
28 | |
29 | let mut kb: Vec<f64> = Vec::with_capacity(size); | |
30 | let mut sum = 0.0; | |
31 | for n in 0..size { | |
32 | let b = bessel_i0(((n * (size - n)) as f64) * alpha2); | |
33 | sum += b; | |
34 | kb.push(sum); | |
35 | } | |
36 | sum += 1.0; | |
37 | for n in 0..size { | |
38 | dst[n] = (kb[n] / sum).sqrt() as f32; | |
39 | } | |
e35062e7 KS |
40 | }, |
41 | }; | |
42 | } | |
9669f269 KS |
43 | |
44 | fn bessel_i0(inval: f64) -> f64 { | |
45 | let mut val: f64 = 1.0; | |
46 | for n in (1..64).rev() { | |
fdb4b2fb | 47 | val *= inval / f64::from(n * n); |
9669f269 KS |
48 | val += 1.0; |
49 | } | |
50 | val | |
51 | } |