iir1
|
An infinite impulse response (IIR) filter library for Linux, Mac OSX and Windows which implements Butterworth, RBJ, Chebychev filters and can easily import coefficients generated by Python (scipy).
The filter processes the data sample by sample for realtime processing.
It uses templates to allocate the required memory so that it can run without any malloc / new commands. Memory is allocated at compile time so that there is never the risk of memory leaks.
Add the following include statement to your code:
The general coding approach is that first the filter is instantiated specifying its order, then the parameters are set with the function setup
and then it's ready to be used for sample by sample realtime filtering.
All filters are available as lowpass, highpass, bandpass and bandstop/notch filters. Butterworth / Chebyshev offer also low/high/band-shelves with specified passband gain and 0dB gain in the stopband.
The frequencies can either be analogue ones against the sampling rate or normalised ones between 0..1/2 where 1/2 is the Nyquist frequency. Note that normalised frequencies are simply f = F/Fs and are in units of 1/samples. Internally the library uses normalised frequencies and the setup commands simply divide by the sampling rate if given. Choose between:
setup
: sampling rate and the analogue cutoff frequenciessetupN
: normalised frequencies in 1/samples between f = 0..1/2 where 1/2 = Nyquist.See the header files in \iir
or the documentation for the arguments of the setup
commands.
The examples below are for lowpass filters:
Butterworth.h
Standard filter suitable for most applications. Monotonic response. or specify a normalised frequency between 0..1/2:
ChebyshevI.h
With permissible passband ripple in dB. or specify a normalised frequency between 0..1/2:
ChebyshevII.h
With worst permissible stopband rejection in dB. or specify a normalised frequency between 0..1/2:
RBJ.h
2nd order filters with cutoff and Q factor. or specify a normalised frequency between 0..1/2:
Custom.h
Samples are processed one by one. In the example below a sample x
is processed with the filter
command and then saved in y
. The types of x
and y
can either be float or double (integer is also allowed but is still processed internally as floating point):
This is then repeated for every incoming sample in a loop or event handler.
Invalid values provided to setup()
will throw an exception. Parameters provided to setup()
which result in coefficients being NAN will also throw an exception.
If you use cmake as your build system then just add to your CMakeLists.txt
the following lines for the dynamic library:
or for the static one:
Link it against the dynamic library (Unix/Mac: -liir
, Windows: iir.lib
) or the static library (Unix/Mac: libiir_static.a
, Windows: libiir_static.lib
).
If you have Ubuntu's LTS distros xenial, bionic or focal then install it as a pre-compiled package:
It's available for 32,64 bit PC and 32,64 bit ARM (Raspberry PI etc). The documentation and the example programs are in:
Make sure you have the homebrew package manager installed: https://brew.sh/
Add the homebrew tap:
and then install the iir filter package with:
The build tool is cmake
which generates the make- or project files for the different platforms. cmake
is available for Linux, Windows and Mac. It also compiles directly on a Raspberry PI.
Run
which generates the Makefile. Then run:
which installs it under /usr/local/lib
and /usr/local/include
.
Both gcc and clang have been tested.
See cmake
for the different build-options. Above is for a 64 bit build. Then start Visual C++ and open the solution. This will create the DLL and the LIB files. Under Windows it's highly recommended to use the static library and link it into the application program.
Run unit tests by typing make test
or just ctest
. These test if after a delta pulse all filters relax to zero and that their outputs never become NaN.
The easiest way to learn is from the examples which are in the demo
directory. A delta pulse as a test signal is sent into the different filters and saved in a file. With the Python script plot_impulse_fresponse.py
you can then plot the frequency responses.
Also the directory containing the unit tests provides examples for every filter type.
A PDF of all classes, methods and in particular setup
functions is in the docs/pdf
directory.
The online documentation is here: http://berndporr.github.io/iir1
These responses have been generated by iirdemo.cpp
in the /demo/
directory and then plotted with plot_impulse_fresponse.py
.
This library has been further developed from Vinnie Falco's great original work which can be found here:
https://github.com/vinniefalco/DSPFilters
While the original library processes audio arrays this library has been adapted to do fast realtime processing sample by sample. The setup
command won't require the filter order and instead remembers it from the template argument. The class structure has been simplified and all functions documented for doxygen. Instead of having assert() statements this libary throws exceptions in case a parameter is wrong. Any filter design requiring optimisation (for example Ellipic filters) has been removed and instead a function has been added which can import easily coefficients from scipy.
Enjoy!
Bernd Porr – http://www.berndporr.me.uk