fili
A digital filter library for JavaScript.
Installation
$ npm install fili
Usage
Node
var Fili = ; var iirCalculator = ;
Browser
-
Copy
./dist/fili.min.js
into your working directory -
Load script in your index.html
- Use
Fili
in your application
var iirCalculator = ;// ...
API
Generate IIR Filters with bilinear transform
IIR filters are composed of n Biquad filters. The Biquad filters need a and b coefficients to work. So, they have a backward and a forward path. Possible filters are:
-
lowpass
-
highpass
-
bandpass
-
bandstop
-
peak
-
lowshelf
-
highshelf
-
aweighting
Possible characteristics are:
-
bessel
-
butterworth
Note: for peak, lowshelf and highshelf a gain attribute must be defined when generating the coefficients. Gain can be positive or negative and represents the dB value for the peak or dip.
// Instance of a filter coefficient calculatorvar iirCalculator = ; // get available filtersvar availableFilters = iirCalculator; // calculate filter coefficientsvar iirFilterCoeffs = iirCalculator; // create a filter instance from the calculated coeffsvar iirFilter = filterCoeffs;
Generate IIR Filters with matched-z transform
IIR filters are composed of n Biquad filters. The Biquad filters need only a coefficients to work. So, they have a backward but no forward path. Possible filters are:
- lowpass
Possible characteristics are:
-
bessel
-
butterworth
-
allpass
-
tschebyscheff05
-
tschebyscheff1
-
tschebyscheff2
-
tschebyscheff3
Note: The number behind tschebyscheff defines the passband ripple.
// calculate filter coefficientsvar iirFilterCoeffs = iirCalculator;
Generate FIR Filters
FIR filter calculation is done with a windowed sinc function Possible filters are:
- lowpass
- highpass
- bandpass
- bandstop
// Instance of a filter coefficient calculatorvar firCalculator = ; // calculate filter coefficientsvar firFilterCoeffs = firCalculator; // filter coefficients by Kaiser-Bessel windowvar firFilterCoeffsK = firCalculator; // create a filter instance from the calculated coeffsvar firFilter = filterCoeffs;
Run Filter
// run the filter with 10 samples from a ramp// returns single valuefor var cnt = 0; cnt < 10; cnt++ console; // run the filter from input array// returns arrayconsole; // simulate the filter// does not change the internal state// returns arrayconsole;
Evaluate Filter
// get the filter impact on magnitude, phase, unwrapped phase, phase delay and group delay// returns array of n objects// Fs = 1000 n = 100, so the array represents 0Hz, 10Hz, 20Hz....// returns array of objects// {dBmagnitude: -4, groupDelay: 2, magnitude: 0, phase: -7, phaseDelay: 12, unwrappedPhase: 7}var response = filter; // get the filter impact on magnitude, phase, unwrapped phase, phase delay and group delay// for a defined frequency// returns one objectvar responsePoint = filter;
Evaluate stability
// initialize filter for testing// note: changes internal state of filter -> create a new filter from// the calculated coefficients for evaluationvar filterTester = testFilter; // check if filter is stable for the specified input range// returns true for stable filtervar stable = filterTester;
Calculate FFT
An FFT is always useful to evaluate filter responses. The algorithm uses precalculated twiddle factors and a lookup table for sine and cosine values. It also reuses all calculation buffers and precalculated window functions. This minimizes garbage collection and improves calculation speed.
Generate a new FFT calculator:
// Fft radix must be 2^nvar fft = 8192;
Frequency<--->Time Domain:
var buffer = ;for var cnt = 0; cnt < 8192; cnt++ buffer; // Supported window functions are// none, hanning, hamming, rectangular// tukery, cosine, lanczos,// triangular, bartlett, gaussian,// bartlettHanning, blackman, blackmanHarris,// nuttall3, nuttall3a, nuttall3b,// nuttall4, nuttall4a, nuttall4b, nuttall4c// sft3f, sft4f, sft5f, sft3m, sft4m, sft5m// nift, hpft, srft, hft70, hft95, hft90d// hft116d, hft144d, hft196d, hft223d, hft248d // get available window functionsvar availableWindows = fft; // buffer.length must be greater or equal fft radixvar fftResult = fft; // fftResult = {re: [], im: []}. The array length equals the FFT radix var magnitude = fft; // magnitudevar dB = fft; // magnitude in dBvar phase = fft; // phase // Note: magnitude, dB and phase are arrays.// The length equals the FFT radix.// For exact phase evaluation, the phase must be unwrapped. var originalBuffer = fft;
Test
$ make test
TODO
- add travis
- add wavelet transform
- add Parks-McClellan FIR algorithm
- add iir filters other than biquad
- add stability evaluation for fix-point arithmetic
License
MIT