]> git.nihav.org Git - nihav.git/blame - nihav-codec-support/src/dsp/window.rs
RealVideo 4 encoder
[nihav.git] / nihav-codec-support / src / dsp / window.rs
CommitLineData
4e034a32 1//! Window generating functions.
e35062e7
KS
2use std::f32::consts;
3
4e034a32 4/// Known window types.
e35062e7
KS
5#[derive(Debug,Clone,Copy,PartialEq)]
6pub enum WindowType {
4e034a32 7 /// Simple square window.
e35062e7 8 Square,
4e034a32 9 /// Simple sine window.
e35062e7 10 Sine,
4e034a32 11 /// Kaiser-Bessel derived window.
9669f269 12 KaiserBessel(f32),
e35062e7
KS
13}
14
4e034a32
KS
15/// Calculates window coefficients for the requested window type and size.
16///
17/// Set `half` flag to calculate only the first half of the window.
e35062e7
KS
18pub fn generate_window(mode: WindowType, scale: f32, size: usize, half: bool, dst: &mut [f32]) {
19 match mode {
20 WindowType::Square => {
21 for n in 0..size { dst[n] = scale; }
22 },
23 WindowType::Sine => {
fdb4b2fb
KS
24 let param = if half {
25 consts::PI / ((2 * size) as f32)
26 } else {
27 consts::PI / (size as f32)
28 };
e35062e7
KS
29 for n in 0..size {
30 dst[n] = (((n as f32) + 0.5) * param).sin() * scale;
31 }
32 },
9669f269
KS
33 WindowType::KaiserBessel(alpha) => {
34 let dlen = if half { size as f32 } else { (size as f32) * 0.5 };
fdb4b2fb 35 let alpha2 = f64::from((alpha * consts::PI / dlen) * (alpha * consts::PI / dlen));
9669f269
KS
36
37 let mut kb: Vec<f64> = Vec::with_capacity(size);
38 let mut sum = 0.0;
39 for n in 0..size {
40 let b = bessel_i0(((n * (size - n)) as f64) * alpha2);
41 sum += b;
42 kb.push(sum);
43 }
44 sum += 1.0;
45 for n in 0..size {
46 dst[n] = (kb[n] / sum).sqrt() as f32;
47 }
e35062e7
KS
48 },
49 };
50}
9669f269
KS
51
52fn bessel_i0(inval: f64) -> f64 {
53 let mut val: f64 = 1.0;
54 for n in (1..64).rev() {
fdb4b2fb 55 val *= inval / f64::from(n * n);
9669f269
KS
56 val += 1.0;
57 }
58 val
59}