Git Product home page Git Product logo

butterworth-filter-design's Introduction

Butterworth Filter Design

Butterworth Filter Design is a collection of C++ classes and an accompanying suite of unit tests for designing high order Butterworth IIR and EQ filters using the bilinear transform.

The generated filter coefficients are split out into cascaded biquad sections, for easy use in any biquad or second-order section (SOS) implementation.

Features

  • Lowpass, Highpass, Bandpass and Bandstop IIR & EQ filter design
  • Low and High Shelving filter design
  • High order Parametric boost/cut EQ filter design *Biquad and Biquad Chain implementations (for filtering audio buffers with cascaded biquad sections)
  • Compact, readable and well-commented codebase

Unit tests

As with any good audio signal processing toolkit, there are unit tests that provide basic proof of correctness. There are currently 6 primary test cases that check 113 different conditions.

Unit tests live in main.cpp and are written using the compact Catch test framework for C++.

Prerequisites

  • SCONS as a cross-platform build system to build, test and run examples.
    • On MacOS use Homebrew: $brew install scons or MacPorts port install scons
    • On Linux: apt-get install scons
  • libsndfile: brew install libsndfile

Usage

The unit tests are a good place to start for a survey of usage.

For example, to design an 8-tap Butterworth lowpass filter with a cutoff @ 500Hz (which will generate coefficients for 4 biquad filters), running @ 44100Hz, with unity gain (1.0) execute the following:

vector <Biquad> coeffs;  // array of biquad filters (for this case, array size = 4 )
Butterworth butterworth;
bool designedCorrectly = butterworth.loPass(44100,  // fs
					    500,    // freq1
					    0,      // freq2. N/A for lowpass
					    8, 	    // filter order,
					    coeffs, // coefficient array being filled
					    1.0);   // overall gain

To generate the same set of coefficients in MATLAB (R14) as a comparison, to double-check our work, execute the following MATLAB commands:

[z, p, k] = butter(8, 500, 's');		% designs a 8-tap lowpass s-domain filter
[Zd, Pd, Kd] = bilinear(z, p, k, 44100);	% analog-to-digital filter conversion
[sos, g] = zpk2sos(Zd, Pd, Kd)			% zero-pole-gain form to second-order sections (SOS)

Other filter design repos on GitHub

Terms and Conditions

For my Master's thesis, and in the course of work writing audio plugins and music apps, I've implemented a few different IIR and EQ filter design classes (e.g. RBJ's EQ Cookbook, Cheby and Butter IIRs). These classes are a fresh rewrite and should be considered unoptimized reference code, with an emphasis on clarity (and for pedagogical reasons, being able to refer to the textbook, see the math and understand the code). For this reason, in the hope they can be useful, these classes are provided under GPL v3.

Butterworth Filter Design - Copyright © 2013   iroro orife

Source code is provided under GPL v3

butterworth-filter-design's People

Contributors

ruohoruotsi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

butterworth-filter-design's Issues

conditional statement possible error

In Butterworth.cpp , in this piece of code (line 201):

    // correct the overall gain
    if(filter == kLoPass || filter == kBandPass){ // pre-blt is okay for S-plane
        ba[0] = preBLTgain * (preBLTgain / gain); // 2nd term is how much BLT boosts,
    }
    else if(filter == kHiPass || kBandStop){ // HF gain != DC gain
        ba[0] = 1 / ba[0];
    }

My compiler complains about the line: else if(filter == kHiPass || kBandStop){ // HF gain != DC gain

error: converting the enum constant to a boolean [-Werror,-Wint-in-bool-context]
    else if(filter == kHiPass || kBandStop){ // HF gain != DC gain

I am using: icpx -Wall -Wextra -pedantic -Werror -Wno-unused-parameter

Why a1, a2 of SOS are always inverse sign compare to matlab results?

Hi ruohoruotsi, Thanks for your work on this library.

I found the Butterworth class's output a1 and a2 of SOS are awayls change sign compare to matlab results.
Such as if c++ output a1=0.1,a2=-0.2 then matlab's a1=-0.1, a2=0.2.

Is there something that I missing? If the output of Butterworth.bandPass() gives digital filter Biquad coefficients then is it should be the
same as matlab's results?

Thanks for your help.

C++ Test code like below:

int filterOrder = 4;
double gain = 1.0;
double Fs = 100;
double fp1 = 0.5;
double fs1 = 10;

vector <Biquad> coeffs;  // second-order sections (sos)
Butterworth butterworth;

bool designedCorrectly = butterworth.bandPass(Fs,             // fs
                                            fp1,            // freq1
                                            fs1,            // freq2. N/A for lowpass
                                            filterOrder,
                                            coeffs,
                                            gain);

Matlab test code like:

  order = 4; 
  Fs=100;     
  fp1=0.5;    
  fs1=10;     
  % design digital Butter-Worth-Filter in SOS form
  [z,p,k]=butter(order,[fp1*2/Fs,fs1*2/Fs],'bandpass');
  [bsos,bk] = zp2sos(z,p,k);

Then you can see coeffs[i].a1 == -bsos[i+1, 5] and coeffs[i].a2 == -bsos[i+1, 6]. i from 0 to length of coeffs.

Gain needs to be controlled

Hi, thanks for your very useful code. I found that the gain of the filter is unpredictable. I'd like the gain at the center frequency to be 1, i.e., no change between input and output. But in my code below, a gain gets applied that varies depending on the parameters chosen. How do I normalize things so that no gain gets applied?

// Design the filter
double overallGain = 1.0;
bool designedCorrectly = butterworth.bandPass(
    args.dSamplerate,               // sample rate (Hz)
    args.dLow,                      // lo cutoff (Hz)
    args.dHigh,                     // hi cutoff (Hz)
    args.nOrder,                    // filter order
    coeffs,                         // vector to hold coefficients
    overallGain);                   // gain (how to choose?)
if (!designedCorrectly) {
    throw std::runtime_error("Filter design failed");
}

// Implement the filter
int stages = (int) coeffs.size();
BiquadChain chain(stages);
chain.processBiquad(input, output, 1, (int)nItems, &coeffs[0]);

Android app

Hi bro
Um working on an android app to do DSP in real-time from mic to speaker , but um using a superpowered SDK and unfortunately it doesn't allow to increase the order of filter so can i import your library in my project and replace the old filter , so Can i use your library in real-time ?? and is there an example on how to use low-pass filter and band-pass ?
Thanks in advance

No initialization variable error

your project In BiquadChain::reset()function,can't init variable xn3 and xn4 to zero,the can generate error audio data at first a certain time.

Butterworth odd orders look incorrect

For odd filter orders a pair of real poles get added rather than a single pole, don't think that is right. In Butterworth::prototypeAnalogLowPass(int filterOrder) rather than

for(uint32_t k = 0; k < (filterOrder + 1) / 2; k++){
double theta = (double)(2 * k + 1) * M_PI / (2 * filterOrder);
double real = -sin(theta);
double imag = cos(theta);
poles.push_back(complex_double(real, imag));
poles.push_back(complex_double(real, -imag)); // conjugate
}

might need something like

for(uint32_t k = 0; k < filterOrder / 2; k++){
double theta = (double)(2 * k + 1) * M_PI / (2 * filterOrder);
double real = -sin(theta);
double imag = cos(theta);
poles.push_back(complex_double(real, imag));
poles.push_back(complex_double(real, -imag)); // conjugate
}
if (filterOrder % 2 == 1){
poles.push_back(complex_double(-1, 0);
}
`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.