Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas-Ball committed Sep 18, 2024
2 parents 1d7b309 + 16b4e9a commit 5ad6dbc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/filters_and_windows/flat_top.rs
Original file line number Diff line number Diff line change
@@ -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<const N: usize>(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
}
1 change: 1 addition & 0 deletions src/filters_and_windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod gaussian_window;
pub mod hamming;
pub mod triangle;
pub mod sin;
pub mod flat_top;
35 changes: 35 additions & 0 deletions tests/flat_top.rs
Original file line number Diff line number Diff line change
@@ -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::<N_SHORT>(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);
}
}

0 comments on commit 5ad6dbc

Please sign in to comment.