FIR Filters Design
FIR Filters Design
INSTRUCTIONS
• This is an individual programming exercise. Any clarifications or questions must be
directed to the instructor or student assistant.
• Save your main code in an M-file with the file name CoE121_PE11_<LastName>.m.
Submit your code together with the following files.
o Documentation containing all the answers for each question. Save it as a pdf
file named CoE121_PE11_<LastName>_docu.pdf”.
o The required functions and images.
o Note: A missing file will incur a deduction of 10.
• The deadline of this exercise is on April 15, 11:55 PM. A 10-point deduction per day
will be incurred for late submissions.
Suppose we have L samples Hd[k] of the desired frequency response. Here, k represents
a digital frequency 0 ≤ ωk < 2ϖ where a sample of the frequency response was taken.
The impulse response h[n] of the FIR filter is determined by using the Inverse Discrete
Fourier Transform (IDFT) equation:
𝐿−1
1 2𝜋
ℎ[𝑛] = ∑ 𝐻𝑑 [𝑘]𝑒 𝑗 𝐿 𝑘𝑛 , 𝑛 = 0,1, … , 𝐿 − 1
𝐿
𝑘=0
The samples Hd[k] are generally complex numbers, containing information about the
desired amplitude and angle response. For an FIR filter with a linear phase response, the
desired frequency response samples are described by the equations below.
𝐻𝑑 [𝑘] = 𝐴𝑑 [𝑘]𝑒 𝑗Ψd [𝑘]
𝐴𝑑 (𝑒 𝑗0 ) 𝑘=0
𝐴𝑑 [𝑘] = { 𝑗2𝜋(𝐿−𝑘)
𝐴𝑑 (𝑒 𝐿 ) 𝑘 = 1,2, … , 𝐿 − 1
𝜋 𝐿 − 1 2𝜋
± 𝑞− 𝑘 𝑘 = 0, 1, … , 𝑄
Ψ𝑑 [𝑘] = { 2 2 𝐿
𝜋 𝐿 − 1 2𝜋
∓ 𝑞+ (𝐿 − 𝑘) 𝑘 = 𝑄 + 1, … , 𝐿 − 1
2 2 𝐿
Here Ad[k] and Ψd[k] represent the samples of the desired amplitude and angle
response, respectively. The parameter q is 0 for Type I and II FIR filters, and 1 for type
III and IV FIR filters. The parameter Q is floor((L-1)/2).
1. Let’s design a 20-th order, FIR Type I, lowpass filter with a cut-off frequency at ωc =
0.3ϖ. First, we generate the frequency vector where we will define the samples of the
desired frequency response. Note that since the filter order is M = 20, our FIR filter
will have L = M + 1 = 21 samples. This is executed by the line of code below. Type it
in your main M-file for this exercise.
CoE 121 Programming Exercise 11: Design of FIR Filters by Frequency Sampling
2nd Semester 2018-2019
CoE 121: Introduction to Digital Signal Processing
Electrical and Electronics Engineering Institute
University of the Philippines, Diliman
>> M = 20;
>> L = M + 1;
>> wkvec = (0:(L-1))*2*pi/L; % frequency vector
2. We will now define the samples of the desired amplitude and angle response. Since
we want a lowpass filter, the amplitude response must be equal to 1 at 0 ≤ ωk ≤ ωc,
and 0 at ωc < ωk ≤ ϖ. For ϖ < ωk ≤ 2ϖ, the set of samples are just a mirror image of
the set in 0 ≤ ωk ≤ ϖ. The samples of the angle response just obtained using the
equation above. These are accomplished by the lines of code below. Append them to
your existing M-file code.
>> wc = 0.3*pi;
>> Ad = zeros(size(wkvec));
>> Ad(wkvec < wc) = 1; Ad(wkvec > (2*pi-wc)) = 1;
>> alpha = (L-1)/2; Q = floor(alpha)
>> Psid = -alpha*(2*pi/L)*[(0:Q),-(L-(Q+1:M))];
3. Calculate the impulse response samples of your FIR filter using the IDFT. In
MATLAB/Octave, this is done by using the ifft() function. The use of this function is
demonstrated in the following lines of code. Add them to your existing M-file code.
>> Hd = Ad.*exp(1i*Psid);
>> hn = real(ifft(Hd));
4. Verify that your filter will work. Plot the frequency response of your FIR filter from
(3) and superimpose the samples of the desired frequency response from (2). You
should get the plot shown in the figure below.
Observe that for frequency values where we defined the desired response, there is
no difference between the desired and the FIR’s magnitude response values. The
error occurs on frequency values where the amplitude response was not defined.
CoE 121 Programming Exercise 11: Design of FIR Filters by Frequency Sampling
2nd Semester 2018-2019
CoE 121: Introduction to Digital Signal Processing
Electrical and Electronics Engineering Institute
University of the Philippines, Diliman
5. Try to vary the filter order M and observe its effect on the magnitude response.
Generate and superimpose the plots of the log-magnitude response of the filter for M
= 20, 60 and 100. What is the effect of the filter order on the
A. Passband ripple
B. Stopband attenuation
C. Transition band
One way of doing this is instead of defining samples with reference only to the cut-off
frequency ωc, we will now define samples with reference to the passband and stopband
edge frequencies (ωp and ωs respectively), which define the transition band of the FIR
filter. The amplitude response samples falling within this band can be computed using
either a straight-line roll-off approach (first equation below) or a raised-cosine roll-off
approach (second equation below).
𝜔𝑠 − 𝜔
𝐴𝑑 (𝑒 𝑗𝜔 ) = , 𝜔𝑝 < 𝜔 < 𝜔𝑠
𝜔𝑠 − 𝜔𝑝
𝜔𝑠 − 𝜔
𝐴𝑑 (𝑒 𝑗𝜔 ) = 0.5 + 0.5 cos [𝜋 ], 𝜔𝑝 < 𝜔 < 𝜔𝑠
𝜔𝑠 − 𝜔𝑝
6. Redefine the samples of the amplitude response in (2) to allocate samples in the
transition band. Use the straight-line roll-off approach. This can be accomplished by
replacing the lines of code in (2) with the following lines of code.
7. Calculate the impulse response samples of your new FIR filter. Plot the frequency
response of your FIR filter and superimpose the samples of the desired frequency
response from (6). You should get the plot shown in the figure below.
8. Try to vary the filter order M and observe its effect on the magnitude response.
Generate and superimpose the plots of the log-magnitude response of the filter for M
= 20, 60 and 100. What is the effect of the filter order on the
A. Passband ripple
B. Stopband attenuation
C. Transition band
9. Repeat (6) to (8) but this time, using the raised-cosine approach.
CoE 121 Programming Exercise 11: Design of FIR Filters by Frequency Sampling
2nd Semester 2018-2019
CoE 121: Introduction to Digital Signal Processing
Electrical and Electronics Engineering Institute
University of the Philippines, Diliman
Just like in the design of FIR filters via windowing, nonrectangular windows can also be
used to change the characteristics of the filter. Once the impulse response is generated
via IDFT, just multiply it with the window of the same length.
10. Use a Hamming window to change the characteristics of the 20-th order filter
generated in (6). Generate and superimpose the plots of the log-magnitude response
of the filter that used and did not use the Hamming window. What is the effect of
using the nonrectangular window on the
A. Passband ripple
B. Stopband attenuation
C. Transition band
D. Error with respect to the desired magnitude response
CoE 121 Programming Exercise 11: Design of FIR Filters by Frequency Sampling
2nd Semester 2018-2019