aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-core/src/dsp/window.rs
diff options
context:
space:
mode:
authorKostya Shishkov <kostya.shishkov@gmail.com>2019-01-17 12:25:49 +0100
committerKostya Shishkov <kostya.shishkov@gmail.com>2019-01-17 12:25:49 +0100
commit5641dccfbf2a70d589cf094a0d4ed5a10f919f00 (patch)
treeab444f3e91b46723187546b1b2820924fb332513 /nihav-core/src/dsp/window.rs
parentb74ff9fac35d41737d71d97227fad233aa4a4b49 (diff)
downloadnihav-5641dccfbf2a70d589cf094a0d4ed5a10f919f00.tar.gz
split NihAV into subcrates
Diffstat (limited to 'nihav-core/src/dsp/window.rs')
-rw-r--r--nihav-core/src/dsp/window.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/nihav-core/src/dsp/window.rs b/nihav-core/src/dsp/window.rs
new file mode 100644
index 0000000..42b6b6d
--- /dev/null
+++ b/nihav-core/src/dsp/window.rs
@@ -0,0 +1,52 @@
+use std::f32::consts;
+
+#[derive(Debug,Clone,Copy,PartialEq)]
+pub enum WindowType {
+ Square,
+ Sine,
+ KaiserBessel(f32),
+}
+
+pub fn generate_window(mode: WindowType, scale: f32, size: usize, half: bool, dst: &mut [f32]) {
+ match mode {
+ WindowType::Square => {
+ for n in 0..size { dst[n] = scale; }
+ },
+ WindowType::Sine => {
+ let param;
+ if half {
+ param = consts::PI / ((2 * size) as f32);
+ } else {
+ param = consts::PI / (size as f32);
+ }
+ for n in 0..size {
+ dst[n] = (((n as f32) + 0.5) * param).sin() * scale;
+ }
+ },
+ 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
+}