Вы находитесь на странице: 1из 13

ECE 515 FL

Digital Signal Processing Laboratory Activity 7: FIR Filter Design

Activity No. 7
FIR Filter Design
I. OBJECTIVES

At the end of the activity, the student must be able to:


1. Design FIR filters using windowed-sinc
2. Perform digital filtering using FIR filters

II. BACKGROUND / ACTIVITIES

A finite impulse response (FIR) filter is a digital filter with impulse response h[n] that is a
finite sequence of numbers. FIR filters are always stable and generally designed to have a linear
phase response. The design of FIR filters is typically based on selecting a symmetric impulse
response h[n] whose length is determined to meet filter specifications. The most common design is
the windowed-sinc design. This design starts by determining the impulse response h[n] of an ideal
digital filter and then truncating the impulse response by an appropriate window.
Consider an ideal digital lowpass filter with cut-off frequency fc, in Hz, to be used at a
sampling frequency Fs. The frequency response H(Ω) is shown for one period as

where Ωc=2πfc/Fs. The impulse response h[n] that corresponds to this ideal lowpass frequency
response can be obtained by getting the Inverse Discrete Time Fourier Transform (IDTFT) of
H(Ω).
π Ωc

h[n] = IDFT { H(Ω) } = (1/2π) ∫ H(Ω) e-jΩn dΩ = (1/2π) ∫ 1 e-jΩn dΩ

-π -Ωc

h[n] = (Ωc/π) sinc(Ωc n)

SLU ECE 1
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

The impulse response h[n] of the ideal lowpass filter is a sinc function.The sinc function is
a sequence that is real-valued, even symmetric, and infinite in duration. The symmetry of the sinc
function ensures that the phase response is linear. To realize a practical digital filter, the impulse
response h[n] of the ideal lowpass filter is truncated symmetrically about n=0 to maintain its linear
phase response. The filter is then made causal by shifting the truncated h[n] such that the first
element of h[n] is at n=0. The choice for the truncated length N of h[n] depends on the filter
specifications to be satisfied. It is generally selected that N is odd to allow easy transformation of
the lowpass filter to the other basic filter types such as highpass, bandpass, and bandstop filters.

Low-pass FIR Filter Design


Example 1:
An FIR filter is designed using windowed-sinc. The filter is to be designed with a cut-off
frequency fc=1000Hz at a sampling frequency Fs=8000Hz. The filter length is selected to be
N=31. The impulse response of the filter is determined using the following SCILAB routine.

SLU ECE 2
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

Fs=8000 //Sampling frequency;


f=1000; //Analog cutoff frequency
Wc=2*%pi*f/Fs; //Digital cutoff frequency
N=31; //Filter length
n=-(N-1)/2:1:(N-1)/2; //Generate values of index n
hn=(Wc/%pi)*sinc(Wc*n) //Compute for truncated h[n]

//Generate the frequency values


W=0:0.001:%pi; //Generate values of Ω
f=(W*Fs)/(2*%pi); //Generate corresponding values of f

//Evaluate H(Ω)using DTFT for normalizing the frequency


HW=zeros(W);
for n=1:length(hn)
HW=HW + hn(n)*exp(-%i*W*(n-1));
end;
hn=hn./max(abs(HW)); //normalize hn

The magnitude response specifications are measured on a normalized magnitude response with the
peak passband gain made equal to 1. To normalize the magnitude response, the impulse response
h[n] is divided by the peak passband gain of the filter.

//Evaluate the normalized H(Ω) and plot themagnitude response and phase response
HW=zeros(W);
for n=1:length(hn)
HW=HW + hn(n)*exp(-%i*W*(n-1));
end;
clf;subplot(2,1,1); plot2d(f,abs(HW)); xgrid();
subplot(2,1,2); plot2d(f,atan(imag(HW),real(HW))*180/%pi); xgrid();

SLU ECE 3
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

The following figure shows the magnitude response of an FIR filter.

where: Ap - minimum passband gain


As - maximum stopband gain
fp - passband cutoff frequency
fs - stopband cutoff frequency

The truncation of h[n] results to an approximation of the frequency response of the ideal
filter. Truncation causes overshoots and ripples in the passband and in the stopband, creates a
transition band between the passband and the stopband, and a non-zero gain in the stopband. The
gain specifications may also be specified in decibel (dB) values using the conversion

AdB=20log10A. The peak passband gain is thus normalized as 0dB. For the 31-point FIR filter in

example 1, the filter specifications are Ap=0.851(-1.4dB), As=0.0736 (-22.7dB), fp=875Hz, and
fs=1118Hz. It should be noted that the cut-off frequency used in the design is approximately at
the middle of fp and fs such that fc=0.5(fp+fs).
To reduce or eliminate the overshoots and ripples in the magnitude response, the impulse
response of the ideal filter is truncated using a tapered symmetric window w[n]. The window
w[n] is multiplied point-wise with windowed-sinc impulse response. The most common windows
are the boxcar or rectangular window, Bartlett or triangular window, Hanning or raised cosine
window, Hamming window, and Blackman window.
Boxcar: w[n] = 1, for -(N-1)/2 to (N-1)/2
Bartlett: w[n] = 1- (2|n| / (N-1)), for -(N-1)/2 to (N-1)/2
Hanning: w[n] = 0.5 + 0.5cos(2πn/(N-1)), for -(N-1)/2 to (N-1)/2
Hamming: w[n] = 0.54 + 0.46cos(2πn/(N-1)), for -(N-1)/2 to (N-1)/2
Blackman: w[n] = 0.42 + 0.5cos(2πn/(N-1)) + 0.08cos(4πn/(N-1)), for -(N-1)/2 to (N-1)/2

SLU ECE 4
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

Most windows have been developed with an optimality consideration which could be
decreasing the ripples in the passband, decreasing the stopband gain, or reducing the transition
width. Ultimately, the choice of window is based on which will result to the shortest filter length
to satisfy filter requirements.

Example 2:
A lowpass FIR filter is designed using windowed-sinc with a Hanning window. The filter
has a cut-off frequency fc=1000Hz at a sampling frequency of Fs=8000Hz. The filter length is
N=31. Use the program in example1 but the command line for the computation of the impulse
response is replaced by hn=(Wc/%pi)*sinc(Wc*n).* (0.5 + 0.5*cos(2*%pi*n/(N-1)));
The passband gain is approximately uniform and the stopband gain approaches zero. It is
noted that the primary consideration for the Hanning window are improvements in gain
characteristics but these improvements are realized at the expense of a wider transition band.

Example 3:
A lowpass FIR filter is to be designed using windowed-sinc with a Bartlett window. The
design requirements are Ap=0.95, As=0.05, fp=1000Hz, and fs=1200Hz. The sampling frequency
is Fs=8000Hz. Determine the minimum filter length N that satisfies the given specifications.
The given filter requirements require that the gain for frequencies 1000Hz and below
should not be less than 0.95 and the gain for frequencies 1200Hz and above should not be more
than 0.05. The design starts with computing the cut-off frequency to be used for the windowed-
sinc. The cutoff frequency to be used is middle frequency between fp and fs such that
fc=0.5(fp+fs)=1100Hz. The equivalent digital frequency Ωc is then computed. An initial filter
length N is chosen, say N=11, and the impulse response h[n] is computed using the SCILAB
command line hn=(Wc/%pi)*sinc(Wc*n).* (1-2*abs(n)/(N-1));
The magnitude response is plotted and the actual filter specifications are determined. The
length N is adjusted until the minimum N that satisfies the design specifications is determined.
With N=11, the actual filter specifications are Ap=0.68 which is very much lower than the
minimum passband gain of 0.95 and As=0.46 which is very much higher than the maximum
stopband gain of 0.05.

SLU ECE 5
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

With N=127, Ap=0.955 satisfies the passband gain requirement but As=0.051 which does
not satisfy the stopband gain requirement. By trying different values for N, it is determined that
the minimum filter length is N=131 with actual As=0.04995 and Ap=0.955.

Spectral Transformation
The other basic filter types such as the highpass, bandpass, and bandstop can be designed
by spectral transformation of the frequency response of a lowpass filter. These transformations use
the unit impulse δ[n] whose DTFT is 1, denoted as
DTFT
δ[n] 1

and the DTFT properties of homogeneity, linearity, and modulation, denoted as


Homogeneity: kx[n] kX(Ω)
Linearity: k1x1[n] + k2x2[n] k1X1(Ω) + k2X2(Ω)
Modulation: x[n]cos(Ωon) 0.5X(Ω+Ωo) + 0.5X(Ω-Ωo)

The transformation from lowpass to highpass may be done by (1) inverting the frequency response
of the lowpass filter or (2) by shifting the frequency response of the lowpass filter. Inverting the
frequency response of the lowpass filter is done by a combination of amplitude reversal and
amplitude shifting, illustrated as

Let HLP(Ω) be the frequency response of an ideal digital lowpass filter with cutoff frequency ΩcLP.
Its impulse response is hLP[n]=(ΩcLP/π)sinc(ΩcLP n). The impulse response of the highpass filter is
obtained from the transformation as
HLP(Ω) hLP[n]
-HLP(Ω) -hLP[n]

SLU ECE 6
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

HHP(Ω) = 1 - HLP(Ω) hHP[n] = δ[n] - hLP[n]


Hence, the impulse response of the ideal highpass filter is described by the function
hHP[n] = δ[n] - hLP[n] = δ[n] - (ΩcLP/π)sinc(ΩcLP n)
where ΩcLP = ΩcHP = 2πfcHP/Fs.
The highpass cutoff frequency fcHP is approximately at the middle of the stopband cutoff
frequency fs and the passband cutoff frequency fp such that it is computed as fcHP=0.5(fp+fs).
The other lowpass-to-highpass transformation involves shifting the frequency response by
π in both directions using the modulation property. This transformation is illustrated as

and the impulse response of the highpass filter is derived as


HLP(Ω) hLP[n]
0.5HLP(Ω+π)+0.5HLP(Ω-π) hLP[n]cos(πn)
HHP(Ω) = HLP(Ω+π)+HLP(Ω-π) hHP[n] = 2hLP[n]cos(πn)
Hence, the impulse response of the ideal highpass filter is described by the function
hHP[n] = 2 hLP[n]cos(πn) = 2(ΩcLP/π)sinc(ΩcLP n)cos(πn)

If n is odd, cos(πn)=(-1)n={…,1, -1, 1, -1, …} and the impulse response becomes


hHP[n] = 2(-1)n(ΩcLP/π)sinc(ΩcLP n)

This method of lowpass-to-highpass transformation is commonly referred to as sign flipping.


Note, however, that the relationship of the highpass cutoff frequency ΩcHP and the lowpass cutoff
frequency ΩcLP is ΩcLP=π - ΩcHP.

Example 4:
A highpass FIR filter is to be designed using windowed-sinc with a Hamming window.
The lowpass-to-highpass transformation that uses amplitude inversion and shifting is used. The

SLU ECE 7
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

design requirements are Ap=0.95, As=0.05, fs=1000Hz, and fp=1500Hz. The sampling frequency
is Fs=8000Hz. Determine the minimum filter length N that satisfies the given specifications.

Fs=8000 //Sampling frequency;


fs=1000; fp=1500; //Analog cutoff frequencies
fclp=0.5*(fs+fp); //lowpass cutoff frequency
Wclp=2*%pi*fclp/Fs; //lowpass digital cutoff frequency
N=31; // Initial filter length

n=-(N-1)/2:1:(N-1)/2; //Generate values of index n


dn=[zeros(1,(N-1)/2, 1, zeros(1,(N-1)/2) //Generate values for δ[n]
hnhp=dn-(Wclp / %pi)*sinc(Wclp*n); //Ideal highpass impulse response
wn=0.54 + 0.46*cos(2*%pi*n/(N-1)); //Compute for the window w[n]
hn=hnhp.*wn; //Compute for the windowed hn

//Normalize the frequency response


W=0:0.001:%pi; //Generate values of Ω
f=(W*Fs)/(2*%pi); //Generate corresponding values of f
HW=zeros(W); //Evaluate H(Ω)
for n=1:length(hn)
HW=HW + hn(n)*exp(-%i*W*(n-1));
end;
hn=hn/max(abs(HW)); //Normalize the impulse response

//Evaluate the normalized H(Ω)


HW=zeros(W);
for n=1:length(hn)
HW=HW + hn(n)*exp(-%i*W*(n-1));
end;
clf; subplot(2,1,1); plot2d(f,abs(HW)); xgrid();
subplot(2,1,2); plot2d(f,atan(imag(HW),real(HW))*180/%pi); xgrid();

SLU ECE 8
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

The filter length N should be adjusted until the minimum length that satisfies the design
requirements is determined.

Example 5:
A highpass FIR filter is to be designed using windowed-sinc with a Hamming window.
The sign flipping lowpass-to-highpass transformation is used. Design requirements are Ap=0.95,
As=0.05, fs=1000Hz, and fp=1500Hz. The sampling frequency is Fs=8000Hz. Determine the
minimum filter length N that satisfies the given specifications.
The design of the highpass filter uses the same SCILAB routine as Example 4 with the
following revisions:

Wclp=%pi -2*%pi*fclp/Fs; //lowpass digital cutoff frequency


hnhp=2*(-1).^n.*(Wclp / %pi).*sinc(Wclp*n); //highpass impulse response

Lowpass-to-bandpass transformation is achieved by using the modulation property which shifts


the frequency response of the lowpass filter by Ωo in both directions.

and the impulse response of the bandpass filter is obtained as


HLP(Ω) hLP[n]
0.5HLP(Ω+Ωo)+0.5HLP(Ω-Ωo) hLP[n]cos(Ωon)
HBP(Ω) = HLP(Ω+Ωo)+HLP(Ω-Ωo) hBP[n] = 2hLP[n]cos(Ωon)

Hence, the impulse response of the ideal bandpass filter is described by the function

SLU ECE 9
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

hBP[n] = 2 hLP[n]cos(Ωon) = 2(ΩcLP/π)sinc(ΩcLP n)cos(Ωon)


The center frequency Ωo is at the middle of the passband such that Ωo=0.5(Ω1BP+Ω2BP). The
lowpass cutoff frequency ΩcLP is half of the bandwidth of the passband, hence, ΩcLP=0.5(Ω2BP-
Ω1BP). For a practical filter with passband cutoff frequencies fp1 and fp2 as well as the stopband
cutoff frequencies fs1 and fs2 where fs1<fp1<fp2<fs2, the passband cutoff frequencies are
computed as f1BP=0.5(fs1+fp1) and f2BP=0.5(fs2+fp2).

Example 6:
A bandpass FIR filter is to be designed using windowed-sinc with a Hamming window.
The design requirements are Ap=0.95, As=0.05, fs1=1000Hz, fs2=2500Hz, fp1=1250Hz, and
fp2=2750Hz. The sampling frequency is Fs=8000Hz. Determine the minimum filter length N that
satisfies the given specifications.
The design of the bandpass filter uses the same SCILAB routine as Example 4 except the
computation for the impulse response as

Fs=8000 //Sampling frequency;


fs1=1000; fs2=2500; //Analog cutoff frequencies
fp1=1250;fp2=2750;
f1bp=0.5*(fs1+fp1);
f2bp=0.5*(fs2+fp2);
W1bp=2*%pi*f1bp/Fs; //Digital cutoff frequencies
W2bp=2*%pi*f2bp/Fs;
Wclp=0.5*(W2bp-W1bp); //lowpass digital cutoff frequency
Wo=0.5*(W1bp+W2bp); //Digital center frequency
N=31; // Initial filter length
n=-(N-1)/2:1:(N-1)/2; //Generate values of index n
hnbp=2*(Wclp / %pi)*sinc(Wclp*n).*cos(Wo*n); //Bandpass impulse response
wn=0.54 + 0.46*cos(2*%pi*n/(N-1)); //Compute for the window w[n]
hn=hnbp.*wn; //Compute for the windowed h[n]

SLU ECE 10
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

Lowpass-to-bandstop transformation is accomplished by first performing a lowpass-to-bandpass


transformation and then a bandpass-to-bandstop transformation. The bandpass-to-bandstop
transformation involves amplitude reversal followed by amplitude shifting.

The impulse response of the bandstop filter is


hBS[n] = δ[n] - 2(ΩcLP/π)sinc(ΩcLP n)cos(Ωon)
The center frequency Ωo is at the middle of the stopband and Ωo=0.5(Ω1BS+Ω2BS). The lowpass
cutoff frequency ΩcLP is half of the bandwidth of the stopband, hence, ΩcLP=0.5(Ω2BS-Ω1BS). Given
the passband cutoff frequencies fp1 and fp2 as well as the stopband cutoff frequencies fs1 and fs2
of a practical filter, where fp1<fs1<fs2<fp2, the stopband cutoff frequencies are computed as
f1BP=0.5(fs1+fp1) and f2BP=0.5(fs2+fp2).

Example 6:
A bandstop FIR filter is to be designed using windowed-sinc with a Hamming window.
The design requirements are Ap=0.95, As=0.05, fp1=1000Hz, fp2=2750Hz, fs1=1250Hz, and
fs2=2500Hz. The sampling frequency is Fs=8000Hz. Determine the minimum filter length N that
satisfies the given specifications.
The design of the bandstop filter uses the same SCILAB routine as Example 4 except the
computation for the impulse response as

Fs=8000; //Sampling frequency


fs1=1250; fs2=2500; //Analog cutoff frequencies
fp1=1000;fp2=2750;
f1bs=0.5*(fs1+fp1);
f2bs=0.5*(fs2+fp2);

SLU ECE 11
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

W1bs=2*%pi*f1bs/Fs; //Digital cutoff frequencies


W2bs=2*%pi*f2bs/Fs;
Wclp=0.5*(W2bs-W1bs); //Lowpass cutoff frequency
Wo=0.5*(W1bs+W2bs); //Digital center frequency
N=31; // Initial filter length
n=-(N-1)/2:1:(N-1)/2; //Generate values of index n
dn=[zeros(1,(N-1)/2, 1, zeros(1,(N-1)/2) //Generate values for δ[n]
hnbs=dn-2*(Wclp / %pi)*sinc(Wclp*n).*cos(Wo*n);
wn=0.54 + 0.46*cos(2*%pi*n/(N-1)); //Compute for the window w[n]
hn=hnbs.*wn; //Compute for the windowed h[n]

IV. LABORATORY EXERCISES


1. Load the mono audio file sonatina.wav. The mono audio signal contains the combined bass
and treble parts. The bass part has a highest frequency of 196Hz while the treble part has a
lowest frequency of 261Hz. Filter the sonatina.wav as indicated.
(a) Design a low-pass filter using a Hamming window with the following filter specifications:
Ap=0.95, As=0.05, fp=200Hz, and fs=250Hz. Use a sampling rate Fs for the filter design that
is the same as the sampling rate of the signal to be filtered.
Determine the filter length N and the actual Ap and As. Ap=_________ As=__________
Plot the magnitude response for 150Hz  f  300Hz.
Insert the plot of the magnitude response here

(c) Use the low-pass filter to extract the bass part of sonatina.wav. Save the filtered signal as
sonatina_bass.wav. Normalize the filtered signal before saving.

(d) Design a high-pass filter using a Hamming window with the following filter specifications:
Ap=0.95, As=0.05, fp=250Hz, and fs=200Hz. Use a sampling rate Fs for the filter design that
is the same as the sampling rate of the signal to be filtered.
Determine the filter length N and the actual Ap and As. Ap=_________ As=__________
Plot the magnitude response for 150Hz  f  300Hz.
Insert the plot of the magnitude response here

(d) Use the high-pass filter to extract the treble part of sonatina.wav. Save the filtered signal as
sonatina_treble.wav. Normalize the filtered signal before saving.

(e) Save a stereo version of sonatina with the bass part in the left channel and the treble part in
the right channel. Save the stereo version as sonatina_stereo.wav

SLU ECE 12
ECE 515 FL
Digital Signal Processing Laboratory Activity 7: FIR Filter Design

2. Design a band-pass FIR filter with Ap=0.95, As=0.05, fs1=300Hz, fp1=500Hz, fp2=3000Hz,
fs2=3200Hz. Use Hanning window and Fs=44100.
(a) Which window should be used to obtain the lowest filter length? ____________________
What is the minimum filter length? _____
What are the actual Ap and As? Ap = __________ As=__________
Plot the magnitude response for 0Hz  f  4KHz.
Insert the plot of the magnitude response here

(b) Load the audio file dinar_16bit_44100Hz_stereo.wav and filter the audio signal. Use the filter
with the minimum length. Filter each channel separately and then save the result as a stereo
signal. Normalize the filtered audio signal and save it as dinar_bp_500Hz_3KHz.wav. Listen
to the original and filtered audio files. Does the filtered audio sound different to the original
audio file? _____
If yes, describe the difference. ___________________________________________________
____________________________________________________________________________

3. Design a 5-band equalizer with filter length of 63 and Hamming window. Let Fs=44100Hz.
Frequency Range Gain
200 Hz and below 0 dB
200-800 Hz -3 dB Note:
800-3200 Hz -6 dB Gain(dB) =20log(Gain)
3200 - 6400 Hz -3 dB Gain= 10^[Gain(dB)/20]
6400 Hz and above 0 dB 6 dB Gain approximately equal to gain of 10

(a) Plot the magnitude response of the equalizer for 0 to 8KHz.


Insert the plot of the magnitude response here

(b) Filter the audio signal in dinar_16bit_44100Hz_stereo.wav and save the filtered signal as
dinar_16bit_44100Hz_stereo_bass_treble_boost.wav.
(c) Listen to the original signal and the filtered signal. Does the filtered signal have a relatively
louder bass and treble sound compared to the original? ______

SLU ECE 13

Вам также может понравиться