Added High-pass filter. (#384)

This commit is contained in:
r4nd0m1z3r 2022-01-31 20:00:57 +03:00 committed by GitHub
parent 1fb86e656d
commit 846c643cbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 0 deletions

View file

@ -21,6 +21,21 @@ where
}
}
pub fn high_pass<I>(input: I, freq: u32) -> BltFilter<I>
where
I: Source<Item = f32>
{
BltFilter {
input,
formula: BltFormula::HighPass { freq, q: 0.5 },
applier: None,
x_n1: 0.0,
x_n2: 0.0,
y_n1: 0.0,
y_n2: 0.0
}
}
#[derive(Clone, Debug)]
pub struct BltFilter<I> {
input: I,
@ -39,6 +54,12 @@ impl<I> BltFilter<I> {
self.applier = None;
}
/// Modifies this filter so that it becomes a high-pass filter
pub fn to_high_pass(&mut self, freq: u32) {
self.formula = BltFormula::HighPass { freq, q: 0.5 };
self.applier = None;
}
/// Returns a reference to the inner source.
#[inline]
pub fn inner(&self) -> &I {
@ -131,6 +152,7 @@ where
#[derive(Clone, Debug)]
enum BltFormula {
LowPass { freq: u32, q: f32 },
HighPass { freq: u32, q: f32},
}
impl BltFormula {
@ -147,6 +169,26 @@ impl BltFormula {
let a1 = -2.0 * w0.cos();
let a2 = 1.0 - alpha;
BltApplier {
b0: b0 / a0,
b1: b1 / a0,
b2: b2 / a0,
a1: a1 / a0,
a2: a2 / a0,
}
}
BltFormula::HighPass { freq, q } => {
let w0 = 2.0 * PI * freq as f32 / sampling_frequency as f32;
let cos_w0 = w0.cos();
let alpha = w0.sin() / (2.0 * q);
let b0 = (1.0 + cos_w0) / 2.0;
let b1 = -1.0 - cos_w0;
let b2 = b0;
let a0 = 1.0 + alpha;
let a1 = -2.0 * cos_w0;
let a2 = 1.0 - alpha;
BltApplier {
b0: b0 / a0,
b1: b1 / a0,

View file

@ -327,6 +327,12 @@ where
{
blt::low_pass(self, freq)
}
#[inline]
fn high_pass(self, freq: u32) -> BltFilter<Self>
where
Self: Sized,
Self: Source<Item = f32>, { blt::high_pass(self, freq) }
}
impl<S> Source for Box<dyn Source<Item = S>>