]>
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 => { | |
16 | let param; | |
17 | if half { | |
18 | param = consts::PI / ((2 * size) as f32); | |
19 | } else { | |
20 | param = consts::PI / (size as f32); | |
21 | } | |
22 | for n in 0..size { | |
23 | dst[n] = (((n as f32) + 0.5) * param).sin() * scale; | |
24 | } | |
25 | }, | |
9669f269 KS |
26 | WindowType::KaiserBessel(alpha) => { |
27 | let dlen = if half { size as f32 } else { (size as f32) * 0.5 }; | |
28 | let alpha2 = ((alpha * consts::PI / dlen) * (alpha * consts::PI / dlen)) as f64; | |
29 | ||
30 | let mut kb: Vec<f64> = Vec::with_capacity(size); | |
31 | let mut sum = 0.0; | |
32 | for n in 0..size { | |
33 | let b = bessel_i0(((n * (size - n)) as f64) * alpha2); | |
34 | sum += b; | |
35 | kb.push(sum); | |
36 | } | |
37 | sum += 1.0; | |
38 | for n in 0..size { | |
39 | dst[n] = (kb[n] / sum).sqrt() as f32; | |
40 | } | |
e35062e7 KS |
41 | }, |
42 | }; | |
43 | } | |
9669f269 KS |
44 | |
45 | fn bessel_i0(inval: f64) -> f64 { | |
46 | let mut val: f64 = 1.0; | |
47 | for n in (1..64).rev() { | |
48 | val *= inval / ((n * n) as f64); | |
49 | val += 1.0; | |
50 | } | |
51 | val | |
52 | } |