korken89 / biquad-rs

A Rust library for digital second order IIR filtrers, also known as biquads
Apache License 2.0
48 stars 13 forks source link

How to set the freq range for a bandpass filter #12

Open annoybot opened 2 years ago

annoybot commented 2 years ago

Hi,

Thanks for making this library available.

I'm just starting to try it out. My goal is to use it to create bandpass filters.

I noticed however, that there is only one freq parameter in the Coefficients struct: f0.

How does one determine the lower and upper frequency ranges for a bandpass filter ?

For now I've set f0 to the middle of my desired frequency range. Any suggestions?

korken89 commented 2 years ago

HI,

The available tunings are based on the DSP filter book, and is linked in the documentation. Do note that those are only tunings for a single biquad, not cascaded biquads which is commonly used. If this does not suit you usecase you can manually set the coefficients of the biquad based on your needs from any filter design tool, e.g. octave or matlab.

annoybot commented 2 years ago

Hi,

We’ve exchanged emails before when I had a question about your crate ‘biquad-rs’.

I’ve since been working on a reimplementation of some of scipy’s iir filtering routines. (Butterworth filters only for now).

I also ported all the relevant tests from scipy, and added a few more of my own. One test filters a given input file and compares it to scipy’s results. They both produce identical results, for a small ϵ, so I’m pretty confident it works.

I haven’t made the repo public yet, but if you are interested I can add you as a collaborator so you can have a look.

I plan to publish it as a crate in the next few weeks.

NOTE: Some of the code in the file filter.rs http://filter.rs/ is inspired by your code.

Example use:

fn filter_foo() { let order = 5; let cutoff_low = 1.0; let cutoff_hi= 10.0; let fs = 81.0;

let zpk = butter(order,  FilterType::BandPass(cutoff_low, cutoff_hi),fs).expect("butter failed.");
let sos = zpk2sos(&zpk, None).expect("zpk2sos failed");

let mut dft2 = DirectForm2Transposed::new(sos);

let data:Vec<f64>  = vec![1.0, 2.0, 3.0];
let mut output:Vec<f64> = vec![];

for x in data.iter() {
    output.push( dft2.filter(*x) );
}

}