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

Solutions of Matlab

This document describes experiments performed with FIR filters. It includes: 1. The impulse response, amplitude response, and pole-zero plot of an FIR filter designed with different methods (type 1, 2, 3, 4). 2. A comparison of FIR bandpass filters designed using Hamming and Kaiser windows, showing the Kaiser window provides steeper roll-off. 3. Examples of FIR filters designed using frequency sampling and remez/firpm algorithms for lowpass, bandpass, and other cases. Filter characteristics like transition width and ripple are adjusted and compared. In summary, the document shows the design and analysis of various FIR filters using different methods, and compares their frequency and impulse responses

Uploaded by

RaviKiran
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)
103 views

Solutions of Matlab

This document describes experiments performed with FIR filters. It includes: 1. The impulse response, amplitude response, and pole-zero plot of an FIR filter designed with different methods (type 1, 2, 3, 4). 2. A comparison of FIR bandpass filters designed using Hamming and Kaiser windows, showing the Kaiser window provides steeper roll-off. 3. Examples of FIR filters designed using frequency sampling and remez/firpm algorithms for lowpass, bandpass, and other cases. Filter characteristics like transition width and ripple are adjusted and compared. In summary, the document shows the design and analysis of various FIR filters using different methods, and compares their frequency and impulse responses

Uploaded by

RaviKiran
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/ 14

Experiment No.

7
1. i) Output

Enter the values of h


[-4, 1, -1 -2, 5, 6, 5, -2, -1, 1, -4]
Impulse Response

Type-1 Amplitude Response


20

10

10
Hr

h[n]

5
0

-10

-5
0

5
n
a[n] coeffs

-20

10

10

0.5
freq in pi units
Pole-zero plot

Imaginary Part

5
a[n]

0
-5

0.5
10

0
-0.5
-1

5
n

10

-1

0
Real Part

ii) Program:

clc ; clearal l; close all;


h = input('Enter the values of h \n');
N = length(h);
n = 0:N-1;
% for type- 2
L = N/2; % order of Hr
b = 2*[h(L:-1:1)]; % type- 2 LPF coef f s
i = 1:1:L;
i = i-0.5;
w = [0:1:500]'*pi/500; % 500 f req between [0 pi ]
Hr = cos(w*i)*b'; % ampl i tude response

bmax = max(b)+1;
bmin = min(b) - 1;
subplot (221) , stem(n,h) , axis ( [ - 1 2*L+1 bmin bmax]) ;
xlabel '(n'), ylabel('h[n]'), title('Impulse Response');
subplot(222), plot(w/pi, Hr), grid on;
xlabel('freq in pi units'), ylabel('Hr'),
title('Type-2 Amplitude Response');
subplot(223), stem(1:L, b), axis([-1 2*L+1 bmin bmax]);
xlabel('n'), ylabel('b[n]'), title('b[n] coeffs');
subplot(224), zplane(h,1), title('Pole-zero plot');
Output:
Enter the values of h
[-4, 1, -1 -2, 5, 6, 6, 5, -2, -1, 1, -4]
Impulse Response

Type-2 Amplitude Response


40

10
20
Hr

h[n]

5
0

-5
0

-20

10

0.5
freq in pi units
Pole-zero plot

n
b[n] coeffs
1
Imaginary Part

10
5
b[n]

0
-5

0.5
11

0
-0.5
-1

10

-1

iii) Program:

clc; clear all; close all;


h = input('Enter the values of h \n');
N = length(h);

0
Real Part

n = 0:N- 1;
% for type- 3
L = (N- 1)/2%
; order of Hr
c = 2*h(L+1:- 1:1)%; type-3 LPF coeffs
i = 0:1 :L ;
w = [0 :1 :500] ' *p i / 500
% ;500 freq between [0 pi]
Hr = s in (w* i ) * c%
' ; amplitude response
cmax = max(c)+1;
cmin = min(c) - 1;
subplot (221) , stem(n,h) , axis ( [ - 1 2*L+1 cmin cmax]) ;
xlabel '(n'), ylabel('h[n]'), title('Impulse Response');
subplot(222), plot(w/pi, Hr), grid on;
xlabel('freq in pi units'), ylabel('Hr'),
title('Type-3 Amplitude Response');
subplot(223), stem(0:L, c), axis([-1 2*L+1 cmin cmax]);
xlabel('n'), ylabel('c[n]'), title('c[n] coeffs');
subplot(224), zplane(h,1), title('Pole-zero plot');
Output:
Enter the values of h
[-4, 1, -1 -2, 5, 6, -5, 2, 1, -1, 4]
Impulse Response

Type-3 Amplitude Response


30

10
20
Hr

h[n]

5
0

-5
0

5
n
c[n] coeffs

-10

10

Imaginary Part

10
c[n]

10

5
0
-5
0

5
n

10

0.5
freq in pi units
Pole-zero plot

1
0.5
10

0
-0.5
-1
-1

0
Real Part

iii) Program:

clc ; clearal l; close all;


h = input('Enter the values of h \n');
N = length(h);
n = 0:N-1;
% for type- 4
L = N/2; % order of Hr
d = 2*[h(L:-1:1)]; % type- 2 LPF coef f s
i = 1:1:L;
i = i-0.5;
w = [0:1:500]'*pi/500; % 500 f req between [0 pi ]
Hr = sin(w*i)*d'; % ampl i tude response
dmax = max(d)+1;
dmin = min(d)-1;
subplot(221), stem(n,h), axis([-1 2*L+1 dmin dmax]);
xlabel('n'), ylabel('h[n]'), title('Impulse Response');
subplot(222), plot(w/pi, Hr), grid on;
xlabel('freq in pi units'), ylabel('Hr'),
title('Type-4 Amplitude Response');
subplot(223), stem(1:L, d), axis([-1 2*L+1 dmin dmax]);
xlabel('n'), ylabel('d[n]'), title('d[n] coeffs');
subplot(224), zplane(h,1), title('Pole-zero plot');
Output:
Enter the values of h
[-4, 1, -1 -2, 5, 6, -6, -5, 2, 1, -1, 4]

Impulse Response

Type-4 Amplitude Response


30

10
20
Hr

h[n]

5
0

10
0

-5
0

-10

10

n
d[n] coeffs
Imaginary Part

10
d[n]

5
0
-5
0

10

0.5
freq in pi units
Pole-zero plot

1
0.5
11

0
-0.5
-1
-1

0
Real Part

2. a) Output:
FIR filter using Hamming window
20
0
-20

Gain in dB

-40
-60
-80
-100
-120
-140

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency

0.7

0.8

0.9

b) Output:
FIR filter using Kaiser window
20
0
-20

Gain in dB

-40
-60
-80
-100
-120
-140

0.1

0.2

0.3

0.4
0.5
0.6
Normalised Frequency

0.7

0.8

0.9

3. Program:

clc , clear
al l,close all;
fs = 2000;
wpl = 400*2/fs;
wph = 500*2/fs;
wsl = 300*2/fs;
wsh = 600*2/fs;
delta_f = wsh-wph;
%a)Hamming Window
N = ceil(6.6/delta_f); % est imates order of the f i l t e r
b = fir1(N,[wpl wph]);
[H,W] = freqz(b,1,512,fs);
subplot(211), plot(W,20*log10(abs(H))), grid on;
title('a) FIR bandpass filter using Hamming window');
xlabel('Frequency in Hz'), ylabel('Gain in dB');
% th i s f i l t e r provides about 53dB attenuat ion in stop band
% and pass band r ipp les are less than 0.25dB as expected.

%b)Kaiser Window
Rp = 0.25;
As = 40;
beta = 0.4852*(As- 21)^0.4+0.07886*(As- 21);
% estimates
BETA
value for the window
N = cei l ( ( A s - 7.95) / ( 14 .36*de l ta_
% estimates
f));
order of the
filter
b = f i r 1 (N , [wp l wph],ka i se r (N+1,beta) ) ;
[H,W] = f reqz(b ,1 ,512, f s ) ;
subplot (212) ,p l o t (W,20* log10(abs(H) ) ) ,on;gr id
title('b) FIR filter using Kaiser window');
xlabel('Frequency in Hz'), ylabel('Gain in dB');
a) FIR bandpass filter using Hamming window

Gain in dB

-50

-100

100

200

300

400
500
600
700
Frequency in Hz
b) FIR filter using Kaiser window

800

900

1000

100

200

300

800

900

1000

Gain in dB

50
0
-50
-100

400
500
600
Frequency in Hz

700

4. Output:
FIR LPF using frequency sampling design
50
0
-50

Gain in dB

-100
-150
-200
-250
-300
-350

0.1

0.2

0.3

0.4
0.5
0.6
Normalised Frequency

0.7

0.8

0.9

5. Program:

clc , clear
al l,close all;
M = 25;
alpha = (M-1)/2;
Hrs = [1,1,1,1,zeros(1,18),1,1,1];
k1 = 0:floor((M-1)/2);
k2 = floor((M-1)/2)+1:M-1;
angH = [-alpha*(2*pi)*k1/M, alpha*(2*pi)*(M-k2)/M];
H = Hrs.*exp(j*angH);
h = real(ifft(H,M));
[H,W]=freqz([h],1,1024);
plot(W/pi,20*log10(abs(H))), zoom on, grid on;
title('FIR LPF using frequency sampling design');
xlabel('Normalised Frequency'), ylabel('Gain in dB');

Outout:
FIR LPF using frequency sampling design
10
0
-10

Gain in dB

-20
-30
-40
-50
-60
-70
-80
-90

0.1

0.2

0.3

0.4
0.5
0.6
Normalised Frequency

0.7

0.8

0.9

Comment:When the length of the filter is increase, we get reduction in transition width
but ripples are also increased in the PB in the process. We do not abserve much change in
stopband attenuation.
6. Program:

clc , clear
al l,close all;
M = 20;
alpha = (M-1)/2;
Hrs = [ 1, 1, 1, 0.5, 0.1, zeros(1,11), 0.1,0.5,1,1];
k1 = 0:floor((M-1)/2);
k2 = floor((M-1)/2)+1:M-1;
angH = [-alpha*(2*pi)*k1/M, alpha*(2*pi)*(M-k2)/M];
H = Hrs.*exp(j*angH);
h = real(ifft(H,M));
[H,W]=freqz([h],1,1024);
plot(W/pi,20*log10(abs(H))), zoom on, grid on;
title('FIR LPF using frequency sampling design');
xlabel('Normalised Frequency'), ylabel('Gain in dB');

% Note that stop band attenuat ion i s not very good, i s about
15dB only
Output:
FIR LPF using frequency sampling design
10
0
-10

Gain in dB

-20
-30
-40
-50
-60
-70
-80
-90

0.1

0.2

0.3

0.4
0.5
0.6
Normalised Frequency

0.7

Comment: The cost paid here is increase in the transition width.

0.8

0.9

7. Output:
FIR LPF using optimum designing
20

Gain in dB

-20

-40

-60

-80

-100

-120

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency

0.7

0.8

0.9

8. Program:

clc , clear
al l,close all;
wp1 = 0.4*pi;
wp2 = 0.6*pi;
Rp = 0.25;
As = 50;
delta_p = (10^(Rp/20)-1)/(10^(Rp/20)+1);
delta_s = (1+delta_p)*(10^(-As/20));
delta_f = 0.2/2; % assuming t rans i t i on width of 0.2p i
b1 = 0.01201;
b2 = 0.09664;
b3 = -0.51325;
b4 = 0.00203;
b5 = -0.5705;
b6 = -0.44314;

C = (b1*( l og10(de l ta_p ) )^2+b2*log10(del ta_p )


+b3)* log10(del ta_ s ) +
b4*( ( l og10(de l ta_p ) )^2)+b5*log10(de l ta_p )+b6;
G = - 16.9- 14.61*( l og10(de l ta_p ) - log10(de l ta_s ) ) ;
N = cei l ( ( C / de l t a _ f ) - (G*del ta_ f )+1) ;
f = [0 0.2 0.4 0.6 0.8
%vector
1] ;
of band edge frequencies
from 0 to 1
m = [0 0 1 1 0 %amplitude
0] ;
vector corresponding f.
b = remez(N, f ,m) ;
disp('The filter coefficients are: ');
disp(b);
[h,w] = freqz(b,1,512);
plot(w/pi,20*log10(abs(h))), grid on;;
title('FIR BPF using optimum designing');
xlabel('Normalized Frequency'), ylabel('Gain in dB');
Output:
FIR BPF using optimum designing
20

Gain in dB

-20

-40

-60

-80

-100

-120

0.1

0.2

0.3

0.4
0.5
0.6
Normalized Frequency

0.7

0.8

0.9

9. In place of remez(), use firpm(). It gives the same output. Actually, this question is re-

dundant. remez is a function of the older versions. In the version that we use of Matlab,
firpm does the same work. You can ask the students to just change the function & observe the output. No need of copying it in the observation copy.

10. Program:

clc , clear
al l,close all;
Fs = 2000;
wp1 = 400*2/Fs;
wp2 = 500*2/Fs;
ws1 = 300*2/Fs;
ws2 = 600*2/Fs;
Rp = 0.25;
As = 50;
%a) Frequency Sampling Design
M = 20;
alpha = (M-1)/2;
Hrs = [zeros(1,4),1,1,zeros(1,14)];
k1 = 0: floor((M-1)/2);
k2 = floor((M-1)/2)+1:M-1;
angH = [-alpha*(2*pi)*k1/M, alpha*(2*pi)*(M-k2)/M];
H = Hrs.*exp(j*angH);
h = real(ifft(H,M));
[H,W]=freqz([h],1,1024);
subplot(211),
plot(W*Fs/(2*pi),20*log10(abs(H))), axis([0 1000 -100 50]),
grid on;
title('a)FIR BPF using frequency sampling design');
xlabel('Frequency in Hz'), ylabel('Gain in dB');
%b) Optimum Design
delta_p = (10^(Rp/20)-1)/(10^(Rp/20)+1);
delta_s = (1+delta_p)*(10^(-As/20));
delta_f = (ws2-wp2)/2;
b1 = 0.01201;
b2 = 0.09664;
b3 = -0.51325;
b4 = 0.00203;
b5 = -0.5705;
b6 = -0.44314;
C = (b1*(log10(delta_p))^2+b2*log10(delta_p)
+b3)*log10(delta_s) +
b4*((log10(delta_p))^2)+b5*log10(delta_p)+b6;

G = - 16.9- 14.61*( l og10(de l ta_p ) - log10(de l ta_s ) ) ;


N = cei l ( ( C / de l t a _ f ) - (G*del ta_ f )+1) ;
f = [0 ws1 wp1 wp2 ws2 1]
%vector
;
of band edge frequencies from 0 to 1
m = [0 0 1 1 0 %amplitude
0] ;
vector corresponding f.
b = remez(N, f ,m) ;
[h ,w] = f reqz(b ,1 ,512,Fs ) ;
subplot (212) , plot (w ,20* log10(abs(h) ) )on;;
, gr id
title('b)FIR BPF using optimum designing');
xlabel('Frequency in Hz'), ylabel('Gain in dB');
Output:
a)FIR BPF using frequency sampling design

Gain in dB

50
0
-50
-100

100

200

100

200

300

400
500
600
700
Frequency in Hz
b)FIR BPF using optimum designing

800

900

1000

800

900

1000

Gain in dB

50
0
-50
-100

300

400
500
600
Frequency in Hz

700

You might also like