summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKostya Shishkov <[email protected]>2018-10-27 18:57:11 +0200
committerKostya Shishkov <[email protected]>2018-10-27 18:57:11 +0200
commit9669f269bf8c09420b64dfe1347581bbfdf12e4a (patch)
tree2e17d4bac329a4b45ccf047a03fe7a2bb45326a0 /src
parent8cb5b63bf7f41d836106022735bfc1db7f8a4057 (diff)
dsp: implement Kaiser-Bessel derived window generation
Diffstat (limited to 'src')
-rw-r--r--src/dsp/window.rs28
1 files changed, 25 insertions, 3 deletions
diff --git a/src/dsp/window.rs b/src/dsp/window.rs
index d59a005..42b6b6d 100644
--- a/src/dsp/window.rs
+++ b/src/dsp/window.rs
@@ -4,7 +4,7 @@ use std::f32::consts;
pub enum WindowType {
Square,
Sine,
- KaiserBessel,
+ KaiserBessel(f32),
}
pub fn generate_window(mode: WindowType, scale: f32, size: usize, half: bool, dst: &mut [f32]) {
@@ -23,8 +23,30 @@ pub fn generate_window(mode: WindowType, scale: f32, size: usize, half: bool, ds
dst[n] = (((n as f32) + 0.5) * param).sin() * scale;
}
},
- WindowType::KaiserBessel => {
-unimplemented!();
+ WindowType::KaiserBessel(alpha) => {
+ let dlen = if half { size as f32 } else { (size as f32) * 0.5 };
+ let alpha2 = ((alpha * consts::PI / dlen) * (alpha * consts::PI / dlen)) as f64;
+
+ let mut kb: Vec<f64> = Vec::with_capacity(size);
+ let mut sum = 0.0;
+ for n in 0..size {
+ let b = bessel_i0(((n * (size - n)) as f64) * alpha2);
+ sum += b;
+ kb.push(sum);
+ }
+ sum += 1.0;
+ for n in 0..size {
+ dst[n] = (kb[n] / sum).sqrt() as f32;
+ }
},
};
}
+
+fn bessel_i0(inval: f64) -> f64 {
+ let mut val: f64 = 1.0;
+ for n in (1..64).rev() {
+ val *= inval / ((n * n) as f64);
+ val += 1.0;
+ }
+ val
+}