diff --git a/src/filters_and_windows/flat_top.rs b/src/filters_and_windows/flat_top.rs new file mode 100644 index 0000000..4afd570 --- /dev/null +++ b/src/filters_and_windows/flat_top.rs @@ -0,0 +1,18 @@ +/// Creates a flat top filter of size n +/// +/// +/// uses matlab constants, these constants will change the filter. +/// i - Offset add offset maybe other var to take in? +pub fn flat_top_window(offset:usize) -> [f32; N] { + const A_0: f32 =0.21557895; + const A_1: f32 =0.41663158; + const A_2: f32 =0.277263158; + const A_3: f32 =0.083578947; + const A_4: f32 =0.006947368; + let mut filter: [f32;N] = [0.0; N]; + for i in 0..N{ + let n = (i as f32) - (offset as f32); + filter[i] = A_0 - A_1 * f32::cos(((2 as f32 * std::f32::consts::PI * n as f32)/N as f32)) + A_2 * f32::cos(((4 as f32 * std::f32::consts::PI * n as f32)/N as f32)) - A_3 * f32::cos(((6 as f32 * std::f32::consts::PI * n as f32)/N as f32)) + A_4 * f32::cos(((8 as f32 * std::f32::consts::PI * n as f32)/N as f32)); + } + filter +} \ No newline at end of file diff --git a/src/filters_and_windows/mod.rs b/src/filters_and_windows/mod.rs index 33a6202..48f67f2 100644 --- a/src/filters_and_windows/mod.rs +++ b/src/filters_and_windows/mod.rs @@ -4,3 +4,4 @@ pub mod gaussian_window; pub mod hamming; pub mod triangle; pub mod sin; +pub mod flat_top; \ No newline at end of file diff --git a/tests/flat_top.rs b/tests/flat_top.rs new file mode 100644 index 0000000..9f8da91 --- /dev/null +++ b/tests/flat_top.rs @@ -0,0 +1,35 @@ +// Test Flat Top Window + +use superdsp::filters_and_windows::flat_top; + +const N_SHORT: usize = 16; + +#[test] +pub fn flat_top_window_short(){ + let expected = [ + -0.00042, + -0.00527, + -0.02687, + -0.0627, + -0.05474, + 0.10175, + 0.44414, + 0.82854, + 1.0, + 0.82854, + 0.44414, + 0.10175, + -0.05474, + -0.0627, + -0.02687, + -0.00527, + ]; + + let window = flat_top::flat_top_window::(0); + assert_eq!(window.len(), N_SHORT); + + // make sure the window is correct to 4 decimal places (floating point error) + for i in 0..N_SHORT { + assert!((window[i] - expected[i]).abs() < 0.0001); + } +} \ No newline at end of file