FIR filters or finite impulse response filters are widely used for thier great qualities and is an invaluable tool for the acoustician. FIR filters can however be tricky to design and calculate relative to IIR filters! I have designed two FIR design functions in Python code that I think would be helpful to you. So how to calculate FIR filters?
FIR filters are finite impulse responses that can be desgined in almost any shape or form. This blogpost will focus on highpass and lowpass FIR filters. If you do not want to read my explanation of how to calculate FIR filters but just want that awesome Python code to design and use, then skip to the bottom!
A FIR filter is defined as a finite impulse response, meaning there is no feedback as with infinite impulse response (IIR) filters. This comes with some advantages:
A FIR filter impulse response could look like the one below.
There are four types of FIR filters:
Symmetric filters, type 1 and 2, are linear phase filters which is defined as a sinc function where M is the length, ω is the angular cutoff frequency and w[n] is a window e.g. Rectangle or Hanning.
Linear phase filters do not distort the phase of a signal but only delays it, which can be very desirable in some areas of signal processing. You also know the filters delay is precisely N taps. The disadvantage is that there can be large delays in such filters because of their length.
The length of FIR filters is dependent on how low frequencies you want to filter and the sample rate. The longer away from the sample rate you get, the longer the filters becomes.
When designing filters in general it is very common to have passband and stopband specifications. For example a passband with maximum 1 dB ripple from 4000 Hz and below and a stopband of minimum 60 dB attenuation from 6000 Hz and above.
When designing FIR filters (using the window method) in for example Python or Matlab it is very common that you yourself need to specify the number of taps, the cutoff frequency and the transition width between passband and stopband. This however can be annoying as a lot of trial and error goes into finding the shortest filter possible.
The Kaiser window method however, solves this problem. The Kaiser window is a window which is customisable based on β:
By using this window you can find a filter length within +/- 2 filter taps of your passband and stopband specifications. This makes designing FIR filters much easier than making your own trial and error.
The number of filter taps M is calculated using the minimum stopband attenuation A and the transition width in Hz between the passband and stopband Δω:
And beta can be calculated as follows
With the number of taps and the beta of the Kaiser window, the first equation and the sinc function can be multiplied with the found Kaiser Window to get a linear phase filter which will approximate your passband and stopband specifications within +/- 2 taps.
The code contains 2 functions and an example of how to use the functions:
FIRDesign: designs a FIR filter based on taps, cutoff frequency and transistion width.
kaiserDesign: designs a FIR filter using the Kaiser window method based on passband and stopband specifications.
To download the Python code to design and use the FIR filters, insert your contact information below and you will receive an email with the code.