# How to Calculate FIR Filters

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? #### By Kasper Kiis Jensen

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!

### What is a FIR Filter

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:

• FIR filters are always stable.
• Linear phase filters can be guaranteed.
• Finite precision errors are not as servere as IIR filters.

If you want to read in depth about FIR filter theory I would suggest you read Discrete-Time Signal Processing or dive into embedded implementation with Real-Time Digital Signal Processing.

A FIR filter impulse response could look like the one below.

There are four types of FIR filters:

• Type 1 is symmetric and has an odd number of taps.
• Type 2 is symmetric and has an even number of taps.
• Type 3 is not symmetric and has odd number of taps.
• Type 4 is not symmetric and has even number of taps.

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.

$h[n]=\frac{sin[\omega_c (n-M/2)]}{\pi (n-M/2)}w[n]$

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.

### Design FIR Filters

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 Δω:

$N=\frac{A-8}{2.285\Delta \omega}$

And beta can be calculated as follows

$\beta =\begin{cases} 0.1102(A-8.7), & A>50\\ 0.5842(A-21)^{0.4}+0.07886(A-21) & 21 \leq A \leq 50 \\ 0.0, & A<21 \end{cases}$

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.

### How To Get The Code

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.

If you liked this blog post then you might also like: How to calculate room acoustic parameters or How to calculate octave band filters

## Heat Pump COP

Understand Heat Pump COP By Kasper Kiis Jensen Heat pump COP stands for Coefficient of Performance and is a ratio for the heating or cooling energy a heat pump can…

## Ventilation System Testing using Modbus

Ventilation System Testing Using Modbus Modbus is one of the oldest and most popular automation protocols in process automation, especially within the heating ventilation and air conditioning industry (HVAC), where…

## How to calculate Mirror Image Method

Learn about Mirror Image Method and try it out yourself.

## How to calculate FIR filters

Learn about FIR filter basics and try it out yourself.