You are on page 1of 3

FIR Filter Design

Filter is one of the most common in signals and systems and transformation. Therefore, you need to
know, what is filter, and what is application of using different filters, and principally why we use filter?

To calculate the output, we need to evaluate the convolution integral, as following structures:

Similarly, to calculate the output of a DT system that has a DT input signal, we use the convolution sum.
As the general form of the convolution sum is: ∞
[ ] = ∫ ℎ[ ] [ − ]
−∞

then, due to causality of LTI system, the convolution sum becomes: ∞


[ ] = ∫ ℎ[ ] [ − ]
0

For an FIR system, the filter coefficients are the individual terms that make up the impulse response of
the system. These FIR filter coefficients are commonly called the b
coefficients. In MATLAB, when all of the b coefficients are
formed into a vector, it is called the B vector. Making this
substitution (b for h) and remembering that an FIR filter of order
N has N +1 coefficients, the convolution sum takes on the
general form of the FIR difference equation, namely,
[ ] = ∑ b[ ] [ − ]
=0
This equation tells us that, in order to calculate a value for the current output, (0), we must perform the dot product of . , where
B = {b[0], b[1], . . . , b[N]} and X represents the current and past values of the input, X = {x[0], x[−1], . . . ,x[−N]}. That is,
[0] = ∑ [ ] [− ] =0
= [0] [0] + [1] [−1] + · · · + [ ] [− ]

The block diagram associated with implementing the FIR difference equation is shown in Figure 1.

MATLAB Implementation
MATLAB has a number of ways of performing the filtering operation. In this lab, we will only discuss two
of them. The first is the built-in filter function, and the second is to build your own routine to perform the
FIR filtering operation. The built-in function allows us to filter signals almost immediately.

Built-In Approach
As mentioned previously, MATLAB has a built-in function called filter.m. This function can be used to
implement both an FIR filter (using only the numerator (B) coefficients) and an IIR filter (using both the
denominator (A) and the numerator (B) coefficients). Use MATLAB Help to understand more about the
filter function.
Notice that in the difference equation discussion of the MATLAB filter command, the A and B coefficient
vector indices start at 1 instead of at 0. MATLAB does not allow for an index value equal to zero. While this
may seem like only a minor inconvenience, improper vector indices account for a significant number

1
FIR Filter Design

of the errors that occur during MATLAB algorithm development. In our lab, we typically can create
another vector, say n, which is composed of integers with the first element equal to zero (i.e., n=0:15
creates n = {0, 1, 2, 3, . . . , 15}), and use this n vector to “fool” MATLAB into counting from zero
for things such as plot axes. See the code given below for an example of this technique.

The MATLAB code shown above will filter the input vector x using the FIR filter coefficients in vector
B. Notice that the input vector x is zero padded (line 5) to flush the filter. This technique differs slightly
from the direct implementation of the MATLAB filter command in which for M input values there will
be M output values. This technique assumes that the input vector is both preceded and followed by a large
number of zeros. This implies that the filter is initially at rest (no initial conditions) and will relax or flush
any remaining values at the end of the filtering operation.
Lab exercise-1: Run this code and see the Stem plot from the example above.

Explain your outcomes?


The FIR filter coefficients associated with this filter were B = [0.25 0.25 0.25 0.25]. Since there are four
coefficients for the filter, this is a third-order filter (i.e., N = 3). The filtering effect in this case is an
averaging of the most recent 4 input samples (i.e., the current sample and the previous three samples). This
type of filter is called a moving average (MA) filter and is one type of lowpass FIR filter.

A Real-World Filtering Example


There are an unlimited number of data sets or processes that can be filtered. For example, what if we
wanted to know the 4-day or 32-day average value of a stock market’s closing value? If the closing
values are filtered, much of the day-to-day market variations can be removed. The cutoff frequency of the
filter used to process the closing values would control the amount of the remaining variations. Download
“dailynasdaq2001.xls” available on canvas and do an Moving average filter, the cutoff frequency is

2
FIR Filter Design

inversely related to the filter order. Illustrate filtered and unfiltered closing values of the NASDAQ
composite index for calendar year 2001.
Lab exercise-2: Demonstrate two plots where using the MATLAB function “filter” and “filtfilt”
functions. Compare your results with the raw data where the results of filtering the raw data
with both a 4-term (3rd-order) and a 32-term (31st-order) of moving average filter.
For additional information on the MATLAB function filtfilt, type “help filtfilt” from the
MATLAB command prompt.

Expect to see such a result.


Routinely, FIR filters use a considerably higher order than the fourth-order filter discussed in the previous
section. Most filters also require more than a few digits of numerical precision to accurately specify the B
coefficients. These facts make manual entry of the B coefficients very inconvenient.

Lab exercises-3: FIR LPF (Low Pass Filter) design


In this section, we are going to design various FIR filters of LPF, HPF, using the built-in MATLAB function “fir1”. Design a FIR LPF in discrete time with the specs, N=32 and = 14 where N is an order of FIR filter and n is a cut-off
frequency. You can design a FIR filter using the built-in function “fir1”. The “fir1” functions will return N+1 coefficients. Note that you are designing a discrete filter, which means that the n can be any values between
− and . However, the “fir1” is accepting a normalized frequency from 0 to 1, where 1 is corresponding to . Once you obtained the filter coefficients, draw the frequency response
on a Figure. It can be obtained by the built-in function, “freqz”. For x-axis, use to check the specs easily.

Notice that “fir1” give you -6 dB (i.e., not –3dB) cut-off frequency. You can check it by typing “help fir1” on the
command window.

You might also like