-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Petr Horacek <[email protected]>
- Loading branch information
Showing
5 changed files
with
56 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//! Simple one-pole low-pass filter. | ||
//! | ||
//! Based on <https://www.earlevel.com/main/2012/12/15/a-one-pole-filter/>, | ||
//! this filter may be useful for attribute smoothening. | ||
use core::f32::consts::PI; | ||
|
||
use libm::expf; | ||
|
||
#[derive(Default, Debug)] | ||
#[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
pub struct OnePoleFilter { | ||
y_m1: f32, | ||
a0: f32, | ||
b1: f32, | ||
} | ||
|
||
impl OnePoleFilter { | ||
// Cutoff is normalized frequency. 10.0 / sample_rate would be 10 Hz. | ||
pub fn new(sample_rate: f32, cutoff: f32) -> Self { | ||
let normalized_frequency = cutoff / sample_rate; | ||
let b1 = expf(-2.0 * PI * normalized_frequency); | ||
let a0 = 1.0 - b1; | ||
Self { y_m1: 0.0, a0, b1 } | ||
} | ||
|
||
pub fn tick(&mut self, x: f32) -> f32 { | ||
self.y_m1 = x * self.a0 + self.y_m1 * self.b1; | ||
self.y_m1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters