0% found this document useful (0 votes)
9 views

ASP Lab 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

ASP Lab 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Advanced Signal Processing

Departement of electronics and Telecommunications


University of Ouargla , 2024-2025

Lab 2: FIR Filter Design by Windowing

1 Overview
An FIR filter’s output is a weighted sum of past input samples, and its impulse response
is directly related to the filter coefficients. The general FIR filter equation is:
M
X
y(n) = h(k)x(n − k) (1)
k=1

The windowing method for FIR filter design involves multiplying an ideal filter’s impulse
response (which may be infinite) by a finite-duration window function. This truncates
the ideal impulse response, making it realizable as an FIR filter. Suppose that we want
to design a lowpass filter with a cutoff frequency of c , i.e. the desired frequency response
will be: (
1 | ω |< ωc
Hd (ω) = (2)
0 otherwise
To find the equivalent time-domain representation, we calculate the inverse discrete-time
Fourier transform: Z π
hd (n) = Hd (ω)ejω dω (3)
−π
Substituting Equation (2) into Equation (3), we obtain:
sin(nωc )
hd (n) = (4)
πn
Try to compute hd (n) for ωc = π/4 using the following script:
wc = pi /4;
n = -20:20;
hd = sin ( n * wc ) ./( n * pi ) ;
a = find ( isnan ( hd ) ) ; % to avoid NAN
in = n ( a ) ;
hd ( a ) = wc * cos ( in * wc ) / pi ;
stem (n , hd )
After running this script, you can see that hd (n) needs an infinite number of input samples
to perform filtering and that the system is not causal. to make the filter causal and linear
phase, The obvious solution will be to truncate the impulse response and use, the N+1
samples of the input, and assume other coefficients to be zero. the obtained finite duration
sequence must be shifted by N/2 to guarantee the causality. Truncation of the impulse
response is equivalent to multiplying the shifted version of hd (n) by a rectangular window
w(n), which is equal to one for n = 0, 1, ...N and zero otherwise. Therefore, we obtain
the impulse response of the designed filter:
h(n) = hd (n − N/2).w(n) (5)
In the frequency domain:
H(ejω ) = Hd (ejω ) ∗ W (ejω ) (6)
with: (
1.e−jωN/2 | ω |< ωc
Hd (ω) = (7)
0 ωc <| ω |< π
sin(ω(N + 1)/2)
W (ω) = e−jωN/2 (8)
sin(ω/2)
N = 32; % example window length
w = linspace ( - pi , pi , 1000) ; % normalized frequency range
Win = ( sin (( N +1) * w /2) ) ./ ( sin ( w /2) ) ;
plot ( w / pi , Win ) ;
xlabel ( ' Normalized Frequency ( w /\ pi ) ') ;
ylabel ( ' Window Spectrum ') ;
Hd =[ zeros (1 ,300) ones (1 ,400) zeros (1 ,300) ];
H = conv ( Win , Hd , ' same ') ;
figure
plot ( H ) ;

• From the figure, try to compute the minimum stopband attenuation.


• Try to increase the filter order. you will find that the transition band gets smaller
but the ripple remains
A solution to the Sharp Discontinuity of Rectangular window consists of the use
of windows with no abrupt discontinuity in their time-domain response and conse-
quently low side-lobes in their frequency response. In the table below, a summary
of commonly used windows
(
1 0≤n≤N
Rectangular w(n) =
0 else
(
0.5 − 0.5cos(2πω/N ) 0 ≤ n ≤ N
Hanning w(n) =
0 else
(
0.54 − 0.46cos(2πω/N ) 0 ≤ n ≤ N
Hamming w(n) =
0 else
(
0.42 − 0.5cos(2πω/N ) + 0.08cos(2πω/N ) 0 ≤ n ≤ N
Blackman w(n) =
0 else

window side-lobe amplitude transition width stopband attenuation


Rectangular -13dB 1.8π/N -21dB
Hanning -dB 6.2π/N -44dB
Hamming -41dB 6.6π/N -53dB
Blackman -57dB 11π/N -74dB
For the other FIR filter types:
High pass: hd (n) = sin(π(n−N/2))
π(n−N/2)
− sin(ωc (n−N/2))
π(n−N/2)
bandpass: hd (n) = sin(ωc 2(n−N/2))
π(n−N/2)
sin(ωc 1(n−N/2))
− π(n−N/2)
Stopband :hd (n) = sin(π(n−N/2))
π(n−N/2)
− sin(ω c 2(n−N/2))
π(n−N/2)
+ sin(ω c 1(n−N/2))
π(n−N/2)

2
2 Objetives
This lab explores the synthesis of FIR filters through the windowing method. The focus
will be on designing various types of FIR filters, including low-pass, high-pass, band-pass,
and band-stop filters. Once developed, these filters will be applied to a sound signal to
analyze their effects on the signal’s frequency components. This hands-on experience will
provide a deeper understanding of filter design techniques and their practical applications
in signal processing.

3 Matlab exercises
1. Write a Matlab function hd = firwid(N,wc) that designs an ideal low-pass FIR filter
with order N and cutoff frequency c . The function will generate the impulse response
hd (n) of the filter using the rectangular window.
function hd = fir_w (N , wc )
n =0: N ;
hd = sin (( n - N /2) * wc ) ./(( n - N /2) * pi ) ;
a = find ( isnan ( hd ) ) ; % to avoid NAN
in = n ( a ) ;
hd ( a ) = wc * cos ( in * wc ) / pi ;
end

2. Using this function, Design a high-pass, band-pass, and stop-band filter. in each
case sketch the impulse response and the frequency response.

3. In the script below, a sequence of Gaussian random samples passes through the
designed filters. Complete the script by implementing the necessary filter designs
and applying them to the random sample sequence to observe the effects of each
filter type in action.
N =40; n =0: N ;
wc = ; % enter the cutoff frequency
hd =; % enter the the impulse response
stem (n , hd )
figure ,
freqz ( ) % display the frequency response
x = randn (1 ,512) ; % input signal
y = filter ( ) ; % to compute the output of the filter
X = fft (x ,512) ; Y = fft (y ,512) ;
figure ; subplot (2 ,1 ,1)
plot ( abs ( X (1:256) ) )
subplot (2 ,1 ,2)
plot ( abs ( Y (1:256) ) )
figure ; subplot (2 ,1 ,1)
plot ( x )
subplot (2 ,1 ,2)
plot ( y )

3
4. Design a low-pass filter according to the specifications:

ωp = 0.2π ωs = 0.3π δ1 = 0.01 δ2 = 0.01

sketch the impulse response h(n) and the frequency response.


wp = ; ws = ; A =50;
N= ; % According to the table
wc = ;
hd = fir_w ( wc , N ) ;
h = hd .* ; % h = hd .* window
[H , w ] = freqz ( h ,1 ,1000) ;
mag = abs ( H ) ; Hdb =20* log10 ( mag ) ;
figure (1) ; plot (w , Hdb )
figure (2) ; plot (w , mag )

5. Design a band-pass filter according to the specifications:

ωs1 = 0.2π ωp1 = 0.3π ωp2 = 0.6π ωs2 = 0.7π As = 60dB

sketch the impulse response h(n) and the frequency response.


wp1 = ; ws1 = ; wp2 = ; ws2 = ; A =50;
N= ; % According to the table
wc = ;
hd = ;
h = hd .* ; % h = hd .* window
[H , w ] = freqz ( h ,1 ,1000) ;
mag = abs ( H ) ; Hdb =20* log10 ( mag ) ;
figure (1) ; plot (w , Hdb )
figure (2) ; plot (w , mag )

6. Design a band-stop filter according to the specifications:

ωp1 = 0.2π ωs1 = 0.3π ωs2 = 0.6π ωp2 = 0.7π As = 60dB

sketch the impulse response h(n) and the frequency response.


wp1 = ; ws1 = ; wp2 = ; ws2 = ; A =50;
N= ; % According to the table
wc = ;
hd = ;
h = hd .* ; % h = hd .* window
[H , w ] = freqz ( h ,1 ,1000) ;
mag = abs ( H ) ; Hdb =20* log10 ( mag ) ;
figure (1) ; plot (w , Hdb )
figure (2) ; plot (w , mag )

7. Design a band-pass filter according to the specifications:

ωs1 = 0.2π ωp1 = 0.3π ωp2 = 0.6π ωs2 = 0.8π As = 60dB

sketch the impulse response h(n) and the frequency response.

4
wp1 = ; ws1 = ; wp2 = ; ws2 = ; A =50;
N= ; % According to the table
wc = ;
hd = ;
h = hd .* ; % h = hd .* window
[H , w ] = freqz ( h ,1 ,1000) ;
mag = abs ( H ) ; Hdb =20* log10 ( mag ) ;
figure (1) ; plot (w , Hdb )
figure (2) ; plot (w , mag )

4 Audio signals filtering


1. A musical note can be regarded as a "pure" sound produced by a sinusoidal signal.
As indicated in the table below, each note corresponds to a specific frequency in
Hertz.

Do Re Mi Fa Sol La Si
262 294 330 349 392 440 495

Write a program gamme.m that generates a musical signal. Each note should have
a duration of 1 second, meaning the total duration of the scale will be 7 seconds.
The sampling frequency can be set to 8192 Hz. The synthesized signal should be
saved in a variable s.

2. In the following, for each case give the characteristics (cutoff frequency in Hz, tran-
sition bandwidth in Hz, attenuation in the stop-band (in dB) of the designed filter
and the chosen window).
sketch the frequency response and the filtered signal.

• Low-pass FIR filter that allows only the first two notes to pass through the
filter.
• Height-pass FIR filter that allows only the last two notes to pass through the
filter.
• Band-pass FIR filter that allows only the Fa and Sol notes to pass through the
filter.
• Design a low-pass FIR filter that eliminates the note Sol from the musical
signal.

3. The file speech_dft.wav is an audio file sampled at 11025 Hz available in Matlab.


You can load it and listen to it in Matlab.
[ x Fs ]= wavread ( ' speech_dft . wav ') ;
sound (x , Fs ) ;

∗ Now play the same sound signal again setting higher and lower frequencies as Fs
(e.g. 18000, 24000). How does the voice sound? Why does the pitch change?

5
∗ Cosider a new signal which is the sum of the signal representing the voice and a
sinusoid of high frequency.

Ns = length ( x ) ;
int = 0.02* sin (2* pi *5000/ Fs *[0: Ns -1]) ;
xn = x + int ';
sound ( xn , Fs ) ;

You can also plot the resulting signal and its spectrum
X = fft (x ,512) ;
w =0: pi /256: pi - pi /256;
figure ; subplot (2 ,1 ,1)
plot ( w / pi , abs ( X (1:256) ) )
subplot (2 ,1 ,2) ; plot ( x )
subplot (2 ,1 ,2) ; plot ( y )

∗ If we want to remove the sinusoid signal to better listen to the speaker, what kind
of filter would we need? Low-pass, high-pass, band stop or band pass? Consider
that we might be able to discard sound components above a certain frequency and
still be able to recognize a voice.
We will now design a FIR filter to restore the voice signal. We decided initially to
use a low-pass filter and keep only frequencies below 8 kHz. Later we will use a
stop-band filter at 8 kHz.
[ x Fs ]= wavread ( ' speech_dft . wav ') ;
Ns = length ( x ) ;
int = 0.02* sin (2* pi *5000/ Fs *[0: Ns -1]) ;
x = x + int ';
sound (x , Fs )
N= ; % computr the filter order according to the
selected window
n =0: N ;
wc = ; % enter the cutoff frequency
hd = % compute the ideal impulse response
wind = % compute the selected window
freqz ( hd ,1 ,512 ) % display the frequency response
y = filter ( ) ; % to compute the output of the filter
X = fft (x ,512) ; Y = fft (y ,512) ; % spectrums
w =0: pi /256: pi - pi /256;
figure ; subplot (2 ,1 ,1)
plot ( w / pi , abs ( X (1:256) ) )
subplot (2 ,1 ,2)
plot ( w / pi , abs ( Y (1:256) ) )
figure ; subplot (2 ,1 ,1) , plot ( x )
subplot (2 ,1 ,2) , plot ( y )
sound (y , Fs )

∗ Similarly for stop-band filter.

You might also like