## 15.2 FIR filters

FIR is the abbreviated notation for the finite impulse response digital filters. The basic properties of FIR filters are:

- relatively high order,
- linearity of phase,
- stability.

Design of a FIR filter starts by selecting Filter Designer Tool from the main menu of the mikroBasic compiler for dsPIC. The option FIR parameters on the left-hand side of the menu is selected. Then, the form shown in Fig. 15-3 appears.

**Fig. 15-3 Entering FIR filter parameters**

In the part

**Device setup** appear the clock and designation of the used microcontroller.
In the part

**Input Signal**, two parameters, sampling frequency and input channel of the AD converter, should be selected. The sampling frequency has to be at least twice as high as the maximum frequency of the input signal. If, e.g. the input signal is in the range 100Hz – 4400Hz, the minimum sampling frequency is 8800Hz. Of course, one never selects the minimum frequency, thus in this case the sampling frequency of at least 10kHz should be selected.
In the part Filter settings the type of filter is selected first:

- Lowpass filter,
- Highpass filter,
- Bandpass filter,
- Bandstop filter.

Then, the order of the filter is selected. This does not apply if Kaiser window function is applied, when on the basis of the selected parameters the order of filter is selected automatically. As the order of the filter increases, the selectivity of the filter will be higher (narrower transition zone), but the complexity of the filter will increase as a consequence of the more memory required for storing the samples and longer processing. The maximum sampling frequency depends on the length of processing (filter order) and selected clock.

**Fig. 15-4 Type of FIR filter**

After the type and order of the FIR filter are selected, the window function to be used can be selected. The bandstop attenuation and selectivity of the filter will depend on the window function. These two characteristics are mutually in conflict. A higher bandstop attenuation corresponds to a lower selectivity and vice versa. Kaiser window function is an optimum window function which gives the maximum attenuation for a given selectivity.

**Fig. 15-5 Selecting window function**

There remains only one parameter to be selected and that is the boundary frequency of the filter.

**Fig. 15-6 Boundary frequency of the FIR filter**

When all FIR filter parameters have been entered, design of the filter is carried out simply by selecting one of the options on the left-hand side of Filter Designer Tool (FIR Window, FIR Frequency, mikroBasic, mikroBasic, mikroC).
In the part FIR Window the shape of the window function and pulse response of the designed filter can be observed. If a lowpass filter of the order 20 is selected, Hamming window function having the boundary frequency 4000Hz will result in the pulse response shown in Fig.15-7.

**Fig. 15-7 Window function and pulse response of the designed filter**

In the part FIR Frequency one can see the shape of the frequency characteristic of the window function and the frequency characteristic of the designed digital filter. If a lowpass filter of the order 20 is selected, Hamming window function having the boundary frequency 4000Hz will result in the pulse response shown in Fig.15-8.

**Fig. 15-8 Frequency characteristics of the window function (upper) and designed FIR filter (lower)**

In the frequency characteristic of the FIR filter one can observe the deviation due to the finite wordlength (the coefficients are kept as 16-bit values). For the designed filter this deviation is negligible, but it can become significant for a higher order filter.
The final product is the code in Pascal, Basic or C which could be accessed by selecting mikroPascal, mikroBasic or mikroC in the left-hand part of

**Filter Designer Tool**. The corresponding parts of the code are shown in Figs. 15-9, 15-10. and 15-11.

**Fig. 15-9 Program code for designing FIR filter (mikroBasic)**

**Fig. 15-10 Program code for designing FIR filter (mikroBasic)**

**Fig. 15-11 Program code for designing FIR filter (mikroC)**