Last active
November 26, 2021 10:08
-
-
Save xd009642/ae5c18faad2dd21beb953f2e72340c8a to your computer and use it in GitHub Desktop.
Tom RustDSP
This file contains hidden or 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
#[derive(Debug, Clone)] | |
pub struct Biquad{ | |
/* https://ccrma.stanford.edu/~jos/fp/Direct_Form_II.html */ | |
b0: f64, | |
b1: f64, | |
b2: f64, | |
a1: f64, | |
a2: f64, | |
} | |
#[derive(Debug, Clone, Default)] | |
pub struct BiquadState { | |
vn: f64, | |
vn_1: f64, //previous input sample | |
vn_2: f64, //previous+1 input sample | |
} | |
impl BiquadState { | |
fn update_vn(&mut self, vn: f64) { | |
self.vn_2 = self.vn_1; | |
self.vn_1 = self.vn; | |
self.vn = vn; | |
} | |
} | |
fn main() { | |
println!("Hello, world!"); | |
let b = (0.29287490, 0.58574979, 0.29287490); | |
let a = (-7.17336609e-17, 0.17149959); | |
let filt1 = Biquad::new(b, a); | |
let mut state = BiquadState::default(); | |
let values = vec![0.5, 0.5, 0.5, 0.5, 0.5, 0.0, 0.0, 0.0, 0.0, 0.0]; | |
for sample in values{ | |
let out = filt1.update(sample, &mut state); | |
println!("value {}", out.output(&state)); | |
} | |
} | |
impl Biquad { | |
fn new(b: (f64, f64, f64), a: (f64, f64))->Biquad { | |
Biquad{b0: b.0, b1: b.1, b2: b.2, a1: a.0, a2: a.1} | |
} | |
fn push_sample(&self, xn: f64, state: &mut BiquadState) { | |
//pass a new value into the filter. vn_2 takes value of vn_1 | |
state.update_vn(n-self.a1*state.vn_1-self.a2*state.vn_2); | |
} | |
fn output(&self, state: &BiquadState) -> f64 { | |
self.b0*state.vn + self.b1*state.vn_1+self.b2*state.vn_2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment