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

Mani Mam Lab Manual

Uploaded by

Geethakshaya
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)
57 views

Mani Mam Lab Manual

Uploaded by

Geethakshaya
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/ 92

Scanned by CamScanner

Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
HARITHA C

2018105540

EXPERIMENT 4: LINEAR AND CIRCULAR CONVOLUTION

AIM:

To Convolve the given sequences using linear and circular convolution using MATLAB.

SOFTWARE USED:

MATLAB R2020a

CODE:

Verify the codes for the following sequences

1. x[n]=[1 2 3 4] and h[n]=[1 1 1 1]

2. x[n]=[1 2 1 2] and h[n]=[6 0 2 1 2]

'2' Indicates the n=0 point.

1) Linear Convolution:
clc;
clear all;
close all;
% Gets the input from user for x[n] and h[n]
x=input('Enter x(n): ')
h=input('Enter h(n): ')
% Gets the position of index 0 from user
x_p=input('Enter the position of index 0 for x(n): ')
h_p=input('Enter the position of index 0 for h(n): ')
% Calculates the length of sequences
m=length(x);
n=length(h);
% Appends zeros in order to make both sequences of equal length
X=[x,zeros(1,n-1)];
disp(['x(n)= ',num2str(X)]);
H=[h,zeros(1,m-1)];
disp(['h(n)= ',num2str(H)]);
% Computes the range for double sided convolution
if x_p~=1 || h_p~=1
st_x= 1-x_p;
st_h= 1-h_p;
x_id=st_x:m-x_p;
h_id=st_h:n-h_p;
y_id=(min(x_id)+min(h_id)):(max(x_id)+max(h_id));
else
x_id=0:m-1;
h_id=0:n-1;
y_id=0:(max(x_id)+max(h_id));
end
% Performs linear convolution
for i=1:n+m-1
Y(i)=0;
for j=1:m
if(i-j+1>0)
Y(i)=Y(i)+X(j)*H(i-j+1);
end
end
end
disp(['y(n)= ',num2str(Y)]);
% Plots the sequences
subplot(3,1,1); stem(x_id,X(1:length(x_id)));
title('x[n]');
xlabel('n');
subplot(3,1,2); stem(h_id,H(1:length(h_id)));
title('h[n]');
xlabel('n');
subplot(3,1,3);stem(y_id,Y(1:length(y_id)));
title('Linear convolution');
ylabel('yl[n]');
xlabel('n');

1) OUTPUT:
(i)
x = 1×4
1 2 3 4
h = 1×4
1 1 1 1
x_p = 1
h_p = 1
x(n)= 1 2 3 4 0 0 0
h(n)= 1 1 1 1 0 0 0
y(n)= 1 3 6 10 9 7 4
(ii)
x = 1×4
1 2 1 2
h = 1×5
6 0 2 1 2
x_p = 2
h_p = 3
x(n)= 1 2 1 2 0 0 0 0
h(n)= 6 0 2 1 2 0 0 0
y(n)= 6 12 8 17 6 9 4 4
2) Circular convolution:

i) Time domain method:


clc;
clear all;
close all;
x=input('Enter x(n):');
h=input('Enter h(n):');
x_p=input('Enter the position of index 0 for x(n): ');
h_p=input('Enter the position of index 0 for h(n): ');
% Calculates the length of x[n] and h[n]
m=length(x);
n=length(h);
% Calculates the maximum length
N=max(m,n);
% Appends zeros to make both the sequences of equal length
x=[x,zeros(1,N-m)];
disp(['x(n)= ',num2str(x)]);
h=[h,zeros(1,N-n)];
disp(['h(n)= ',num2str(h)]);
if x_p~=1 || h_p~=1
x_id=1-x_p:m-x_p;
h_id=1-h_p:n-h_p;
else
x_id=0:m-1;
h_id=0:n-1;
end
% To do circular convolution in time domain
for n=1:N
Y(n)=0;
for i=1:N
j=n-i+1;
if(j<=0)
j=N+j;
end
Y(n)=[Y(n)+x(i)*h(j)];
end
end
n=0:N-1;
disp(['y(n)= ',num2str(Y)]);
subplot(3,1,1);
stem(x_id,x(1:length(x_id)));
title('x[n]');
xlabel('n');
subplot(3,1,2);
stem(h_id,h(1:length(h_id)));
title('h[n]');
xlabel('n');
subplot(3,1,3);
stem(n,Y);
title('Time domain');
xlabel('n');
ylabel('yc[n]');

2) i) OUTPUT:

(i)
x(n)= 1 2 3 4
h(n)= 1 1 1 1
y(n)= 10 10 10 10
(ii)
x(n)= 1 2 1 2
h(n)= 6 0 2 1 2
y(n)= 15 16 12 17 6
ii) Frequency domain method:
clc;
clear all;
close all;
% Gets the input from user for x[n] and h[n]
x=input('Enter x(n):')
h=input('Enter h(n):')
% Calculates the length of sequences
m=length(x);
n=length(h);
% Gets the position for 0 index from user
x_p=input('Enter the position of index 0 for x(n): ')
h_p=input('Enter the position of index 0 for h(n): ')
% calculates the max length
N=max(m,n);
% Appends zeros in order to make both sequences of equal length
xx=[x zeros(N-m)];
HH=[h zeros(N-n)];
disp(['x(n)= ',num2str(xx)]);
disp(['h(n)= ',num2str(HH)]);
% Computes Wn matrix
W=zeros(N,N);
for n=0:N-1
for k=0:N-1
W(n+1,k+1)=exp(-i*2*pi*n*k/N);
end;
end;
if x_p~=1 || h_p~=1
st_x= 1-x_p;
st_h= 1-h_p;
x_id=st_x:m-x_p;
h_id=st_h:n-h_p;
y_id=(min(x_id)+min(h_id)):(max(x_id)+max(h_id));
else
x_id=0:m-1;
h_id=0:n-1;
y_id=0:(max(x_id)+max(h_id));
end
% Circular convolution in frequency domain
X=W*xx.';
H=W*HH.';
disp('X(k)=');
disp(X.');
disp('H(k)=');
disp(H.');
YY=X.*H;
w=zeros(N,N);
for n=0:N-1
for k=0:N-1
w(n+1,k+1)=exp(i*2*pi*n*k/N);
end;
end;
B=w*YY;
Y=B/N;
disp("Y(k)=X(k)*H(k)");
disp('Y(k)=');
disp(YY.');
disp('IDFT of Y(k)=y[n]');
disp('y[n]=');
disp(Y.');
b=0:N-1;
% Plots individual sequences
subplot(3,2,1);
stem(b(1:length(x)),x);
title('x[n]');
xlabel('n');
subplot(3,2,2);
stem(b(1:length(h)),h);
title('h[n]');
xlabel('n');
subplot(3,2,3);
stem(b,X);
title('X(k)');
xlabel('k');
subplot(3,2,4);
stem(b,H);
title('H(k)');
xlabel('k');
subplot(3,2,5);
stem(b,YY);
title('Y(k)');
xlabel('k');
subplot(3,2,6);
stem(b,Y);
title('ycf[n]');
xlabel('n');
ylabel('ycf[n]');

2) ii) OUTPUT:
(i)
x = 1×4
1 2 3 4
h = 1×4
1 1 1 1
x(n)= 1 2 3 4
h(n)= 1 1 1 1

X(k)= 10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 - 0.0000i


-2.0000 - 2.0000i

H(k)= 4.0000 + 0.0000i -0.0000 - 0.0000i 0.0000 - 0.0000i


0.0000 - 0.0000i

Y(k)=X(k)*H(k)
Y(k)= 40.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i
-0.0000 + 0.0000i

IDFT of Y(k)=y[n]
y[n]= 10.0000 + 0.0000i 10.0000 + 0.0000i 10.0000 + 0.0000i
10.0000 - 0.0000i
ii)
x = 1×4
1 2 1 2
h = 1×5
6 0 2 1 2
x(n)= 1 2 1 2 0
h(n)= 6 0 2 1 2

X(k)= 6.0000 + 0.0000i -0.8090 - 1.3143i 0.3090 - 2.1266i


0.3090 + 2.1266i -0.8090 + 1.3143i

H(k)= 11.0000 + 0.0000i 4.1910 + 1.3143i 5.3090 + 2.1266i


5.3090 - 2.1266i 4.1910 - 1.3143i

Y(k)=X(k)*H(k)
Y(k)= 66.0000 + 0.0000i -1.6631 - 6.5716i 6.1631 -10.6331i
6.1631 +10.6331i -1.6631 + 6.5716i

IDFT of Y(k)=y[n]
y[n]= 15.0000 + 0.0000i 16.0000 - 0.0000i 12.0000 + 0.0000i
17.0000 - 0.0000i 6.0000 + 0.0000i
3) Linear convolution using circular convolution:
clc;
clear all;
close all;
% Gets the input from user for x[n] and h[n]
x=input('Enter x(n):');
h=input('Enter h(n):');
% Calculates the length of x[n] and h[n]
m=length(x);
n=length(h);
%Gets the position of index 0 from user
x_p=input('Enter the position of index 0 for x(n): ')
h_p=input('Enter the position of index 0 for h(n): ')
N=m+n-1;
% Appends zeros in order to make both sequences of equal length
x=[x,zeros(1,N-m)];
disp(['x[n]= ',num2str(x)]);
h=[h,zeros(1,N-n)];
disp(['h[n]= ',num2str(h)]);
% Computes the range for double sided convolution
if x_p~=1 || h_p~=1
st_x= 1-x_p;
st_h= 1-h_p;
x_id=st_x:m-x_p;
h_id=st_h:n-h_p;
y_id=(min(x_id)+min(h_id)):(max(x_id)+max(h_id));
else
x_id=0:m-1;
h_id=0:n-1;
y_id=0:(max(x_id)+max(h_id));
end
% Computes circular convolution
for n=1:N
Y(n)=0;
for i=1:N
j=n-i+1;
if(j<=0)
j=N+j;
end
Y(n)=[Y(n)+x(i)*h(j)];
end
end
n=0:N-1;
disp(['y[n]= ',num2str(Y)]);
subplot(3,1,1);
stem(x_id,x(1:length(x_id)));
xlabel('n');
title('x[n]');
subplot(3,1,2);
stem(h_id,h(1:length(h_id)));
xlabel('n');
title('h[n]');
subplot(3,1,3);
stem(y_id,Y(1:length(y_id)));
xlabel('n');
ylabel('y[n]');
title('Linear convolution through circular convolution');
3)OUTPUT
i)
x = 1×4
1 2 3 4
h = 1×4
1 1 1 1
x_p = 1
h_p = 1
x(n)= 1 2 3 4 0 0 0
h(n)= 1 1 1 1 0 0 0
y(n)= 1 3 6 10 9 7 4

(ii)
x(n)= 1 2 1 2
h(n)= 6 0 2 1 2
x_p = 2
h_p = 3
x[n]= 1 2 1 2 0 0 0 0
h[n]= 6 0 2 1 2 0 0 0
y[n]= 6 12 8 17 6 9 4 4
RESULT:
Thus, convolution of the given sequences are performed using MATLAB and the outputs are
verified.
HARITHA C
2018105540
EXPERIMENT 5: N POINT DFT AND FREQUENCY ANALYSIS OF AM DT
SIGNALS
AIM:
To determine the N point DFT of the given signal and to perform frequency analysis on
discrete time signals using MATLAB.
SOFTWARE USED:
MATLAB R2020a
1)N point DFT of cos(8πn/N)
CODE:
clc;
close all;
clear all;
% Gets the value of N point DFT
N=input("enter the value for N");
n=0:N-1
% Plots the time domain signal
x= cos((8*pi*n)/N);
% Finds N point DFT
y=fft(x,N);
subplot(3,1,1);
stem(n,x);
xlabel('n');
ylabel('amplitude');
title('x(n)');
subplot(3,1,2);
% Plots the N point DFT of x[n]
stem(n,y);
xlabel('k');
ylabel('amplitude');
title('DFT');
OUTPUT:

N=128

2) x[n]=cos[2*pi*f1*n]+cos[2*pi*f2*n], where f1=1/128 and f2=5/128, modulates the


amplitude of carrier xc[n]=cos[2*pi*fc*n], where fc=50/128. The AM signal is
xam[n]=x[n]cos[2*pi*fc*n].

a)Sketch the signal x[n], xc[n] and xam[n], 0<=n<=255.

CODE:

clc;
clear all;
close all;
m=0:127;
k=0:255;
for n=0:255
x(n+1)=cos(2*pi*(1/128)*n)+cos(2*pi*(5/128)*n);
xc(n+1)=cos(2*pi*(50/128)*n);
xam(n+1)=x(n+1)*xc(n+1);
end
subplot(3,2,1);
stem(k,x);
title('a) x[n] for n=0:255');
xlabel('n');
subplot(3,2,2);
stem(k,xc);
title('a) xc[n] for n=0:255');
xlabel('n');
subplot(3,2,3);
stem(k,xam);
title('a) xam[n] for n=0:255');
xlabel('n');

b) Compute and sketch the 128 point DFT of the signal xam[n], 0<=n<=127.
Xam1=fft(xam(1:128),128);
subplot(3,2,4);
stem(m,abs(Xam1));
title('b) 128 point DFT for n=0:127');
xlabel('k');

c) Compute and sketch the 128 point DFT of the signal xam[n], 0<=n<=99.
Xam2=fft(xam(1:100),128);
subplot(3,2,5);
stem(m,abs(Xam2));
title('c) 128 point DFT for n=0:99');
xlabel('k');

d) Compute and sketch the 128 point DFT of the signal xam[n], 0<=n<=179.
Xam3=fft(xam(1:180),256);
subplot(3,2,6);
stem(k,abs(Xam3));
title('d) 256 point DFT for n=0:179');
xlabel('k');

e) Explain the results obtained in parts b) through d), by deriving the spectrum of the
amplitude modulated signal and comparing it with the experimental results.

Answer: By comparing b), c) and d), it is observed that there is spectral leakage in the plots
of c) and d) while there is no spectral leakage in the plot of b).
OUTPUT:

RESULT:

Thus the N point DFT of given signal is calculated and the frequency analysis of amplitude
modulated DT signal is done using MATLAB and the outputs are verified.
HARITHA C

2018105540

EXPERIMENT 6: OVERLAP ADD AND OVERLAP SAVE ALGORITHMS

AIM:

To determine the response of the LTI system with impulse response for the long input sequences
using overlap add and overlap save algorithms in MATLAB.

SOFTWARE USED:

MATLAB R2020a

SEQUENCE:

x[n]=1.5^n, 0<=n<=15, h[n]=[1 1 1]

CODE:

i) Overlap add method:


clc;
clear all;
close all;
n=0:15;
Xn= (1.5).^n;
% Gets input sequence for h[n]
Hn=input('Enter Sequence h[n]= ')
L=5;
sgtitle('OVERLAP ADD METHOD')
subplot (3,1,1);
stem(Xn);
xlabel ('n');
title('x[n]');
a=0:length(Hn)-1
subplot (3,1,2);
xlabel ('n');
title('h[n]');
NXn=length(Xn);
M=length(Hn);
M1=M-1;
R=rem(NXn,L);
N=L+M1;
% Appends M-1 zeros
Xn=[Xn zeros(1,L-R)];
% Appends L-1 zeros
Hn=[Hn zeros(1,N-M)];
K=floor(NXn/L);
y=zeros(K+1,N);
z=zeros(1,M1);
for k=0:K
Xnp=Xn(L*k+1:L*k+L);
Xnk=[Xnp z];
% Calls the overlapadd function
y(k+1,:)=overlapadd(Xnk,Hn);
end
p=L+M1;
for i=1:K
y(i+1,1:M-1)=y(i,p-M1+1:p)+y(i+1,1:M-1);
end
z1=y(:,1:L)';
y=(z1(:))';
disp('Convolved sequence y[n]');
disp(y);
subplot (3,1,3);
stem(y);
xlabel ('n');
title('Convolved Signal y[n]');

OUTPUT:
Xn = 1×15
1.0000 2.2500 3.3800 5.0600 7.5900 11.3900
17.0800 25.6200 38.4400 57.6600 86.4900 129.7400
194.6200 291.9200 437.8900

Hn = 1×3
1 1 1

Convolved sequence y[n]


Columns 1 through 11

1.0000 2.5000 4.7500 7.1250 10.6875 16.0313


24.0469 36.0703 54.1055 81.1582 121.7373

Columns 12 through 20

182.6060 273.9089 410.8634 616.2951 924.4427 729.8232


437.8939 0 0
ii) Overlap save method:
clc;
clear all;
close all;
n=0:15;
x= (1.5).^n;
% Gets the input sequence for h[n]
h = input('Enter the sequence h(n) = ')
nl = length(x);
hl = length(h);
N = nl+hl-1;
% Appends L-1 zeros to h[n]
h1 = [h zeros(1,N-nl)];
nl1 = length(h1);
y = zeros(1,N);
% Appends M-1 zeros at the beginning and retains M-1 points of the block
x1 = [zeros(1,nl1-hl) x zeros(1,nl1)];
H = fft(h1);
for i = 1:hl:N
y1 = x1(i:i+(2*(nl1-hl)));
y2 = fft(y1);
y3 = y2.*H;
y4 = round(ifft(y3));
% M-1 data points are removed
y(i:(i+nl1-hl)) = y4(hl:nl1);
end
subplot(3,1,1);
sgtitle('OVERLAP SAVE METHOD');
stem(x(1:nl));
title('x[n]');
xlabel('n');
a=0:hl-1;
subplot(3,1,2);
stem(a,h(1:hl));
title(' h[n]');
xlabel('n');
subplot(3,1,3);
disp('Convolved sequence y[n] = ');
disp(y(1:N));
stem(y(1:N));
title('Convolved sequence y[n]');
xlabel('n');

OUTPUT:
x = 1×15
1.0000 2.2500 3.3800 5.0600 7.5900 11.3900
17.0800 25.6200 38.4400 57.6600 86.4900 129.7400
194.6200 291.9200 437.8900

h = 1×3
1 1 1

Convolved sequence y[n] = 1 3 7 11 16 24


36 54 81 122 183 274 411 616 924 730 438
% Overlap add function
function y=overlapadd(x,h)
lx=length(x);
lh=length(h);
l=max(lx,lh);
X=[x zeros(1,l-lx)];
H=zeros(l);
H(1:lh)=h;
for j=1:l-1
for i=1:l-1
H(i+1,j+1)=H(i,j);
end
H(1,j+1)=H(l,j);
end
y=H*X';
end

RESULT:

Thus, the response of the LTI system with impulse response for the long input sequences is
obtained using overlap add and overlap save algorithms in MATLAB.
HARITHA C
2018105540

EXPERIMENT 7: DESIGN OF IIR FILTERS


AIM:
To design analog and digital IIR filters using Butterworth and
Chebyshev methods.
SOFTWARE USED:
MATLAB R2020a
CODE:
1] Design a digital Butterworth IIR filter using bilinear
transformation to meet the following specifications:
Pass band edge frequency: 200 Hz
Stop band edge frequency: 800 Hz
Pass band attenuation: 2 dB
Stop band attenuation: 35 dB
Sampling rate: 2kHz
1. Find the magnitude response and phase response of the system
2. Find the spectrum of input and output signals if the input
signal is x(t)=cos(2πf1t)+2cos(2πf2t)+2sin(2πf3t), where, f1=50Hz,
f2=100Hz and f3=950Hz.
clc;
clear all;
close all;
% Getting inputs from the user
fp=input("Enter the pass-band edge frequency in Hz: ")
disp("Wp= ");
disp(2*pi*fp);
fs=input("Enter the stop-band edge frequency in Hz: ")
disp("Ws= ");
disp(2*pi*fs);
ap=input("Enter the pass-band attenuation value: ");
as=input("Enter the stop-band attenuation value: ");
fsamp=input("Enter the sampling frequency in Hz: ")
% dB calculation for ap and as
if ap<1
ap_db=20*log10(ap)
else
ap_db=ap
end
if as<1
as_db=20*log10(as)
else
as_db=as
end
% Finding order and cut-off frequency of the filter
[N,Wc]=buttord(2*pi*fp,2*pi*fs,ap_db,as_db,'s');
disp("Order of the filter: ");
disp(N);
disp("Cut-off frequency of the filter: ");
disp(Wc);
% Finding the coefficients of the transfer function
[num,den]=butter(N,Wc,"low",'s');
disp("Numerator Coefficients of TF: ");
disp(num);
disp("Denominator Coefficients of TF: ");
disp(den);
% frequency response of the analog lpf filter
w=0:10000;
[h,w]= freqs(num,den,w);
plot(w,20*log10(h));
title("Magnitude response of analog LPF");
xlabel("Frequency(rad/sec)");
ylabel("Magnitude(dB)");
plot(w,angle(h));
title("Phase response of analog LPF");
xlabel("Frequency(rad/sec)");
ylabel("Phase(degrees)")
% Converting transfer function form to zero-pole-gain form
[zs ps ks]=tf2zp(num,den);
% Converting analog filter to digital filter using bilinear
transformation
[zd pd kd]=bilinear(zs,ps,ks,fsamp);
% Converting zero-pole-gain form to tf form
[numd dend]=zp2tf(zd,pd,kd);
% Frequency response of the digital lpf filter
freqz(numd,dend);
title("Digital LPF after BLT");
% Sampling the input signal
t=0:1/fsamp:1;
f1=input('f1: ');
f2=input('f2: ');
f3=input('f3: ');
x=cos(2*pi*f1*t)+2*cos(2*pi*f2*t)+2*sin(2*pi*f3*t);
% Applying input signal to the filter
y=filter(numd,dend,x);
% fft of input and output signals of the filter
X=fft(x, fsamp);
Y=fft(y, fsamp);
% Plotting the input and output spectrum
subplot(2,1,1);
stem(abs(X));
title("Input Spectrum X(k)");
xlabel("k");
subplot(2,1,2);
stem(abs(Y));
title("Output Spectrum Y(k)");
xlabel("k");
OUTPUT:
fp = 200

Wp=

1.2566e+03
fs = 800

Ws=

5.0265e+03

fsamp = 2000

ap_db = 2

as_db = 35

Order of the filter:

Cut-off frequency of the filter:

1.8356e+03

Numerator Coefficients of TF:

1.0e+13 *

0 0 0 0 1.1354

Denominator Coefficients of TF:

1.0e+13 *

0.0000 0.0000 0.0000 0.0016 1.1354


2] Design a digital Chebyshev IIR filter using bilinear
transformation to meet the following specification:
Pass band edge frequency:800 Hz
Stop band edge frequency:200 Hz
Pass band attenuation: 2dB
Stop band attenuation:35dB
Sampling rate: 2kHz
(i)Find the magnitude response and phase response of the system
(ii)Find the spectrum of input and output signals if the input
signal is x(t)=cos(2πf1t)+2cos(2πf2t)+2sin(2πf3t), where, f1=50Hz,
f2=100Hz and f3=950Hz.
clc;
clear all;
close all;
% Getting inputs from the user
fp=input("Enter the pass-band edge frequency in Hz: ")
disp("Wp= ");
disp(2*pi*fp);
fs=input("Enter the stop-band edge frequency in Hz: ")
disp("Ws= ");
disp(2*pi*fs);
ap=input("Enter the pass-band attenuation value: ");
as=input("Enter the stop-band attenuation value: ");
fsamp=input("Enter the sampling frequency in Hz: ");
% dB calculation for ap and as
if ap<1
ap_db=20*log10(ap)
else
ap_db=ap
end
if as<1
as_db=20*log10(as)
else
as_db=as
end
% finding order and cut-off frequency of the filter
[N,Wc]=cheb1ord(2*pi*fp,2*pi*fs,ap_db,as_db,'s');
disp("Order of the filter: ");
disp(N); disp("Cut-off frequency of the filter: ");
disp(Wc);
% finding the coefficients of the transfer function
[num,den]=cheby1(N,ap_db,Wc,"high",'s');
disp("Numerator Coefficients of TF: ");
disp(num);
disp("Denominator Coefficients of TF: ");
disp(den);
% frequency response of the analog lpf filter
w=0:10000;
[h,w]=freqs(num,den,w);
plot(w,20*log10(h));
title("Magnitude response of analog HPF");
xlabel("Frequency(rad/sec)");
ylabel(" Magnitude(dB)");
plot(w,angle(h));
title("Phase Response of analog HPF");
xlabel("Frequency(rad/sec)");
ylabel("Phase(degrees)");
% converting transfer function form to zero-pole-gain form
[zs ps ks]=tf2zp(num,den);
% analog to digital filter conversion using bilinear transformation
[zd pd kd]=bilinear(zs,ps,ks,fsamp);
% converting zero-pole-gain form to tf form
[numd dend]=zp2tf(zd,pd,kd);
% frequency response of the digital hpf filter
freqz(numd,dend);
title("Digital HPF after BLT");
% sampling the input signal
t=0:1/fsamp:1;
f1=input('f1: ');
f2=input('f2: ');
f3=input('f3: ');
x=cos(2*pi*f1*t)+2*cos(2*pi*f2*t)+2*sin(2*pi*f3*t);
% applying input signal to the filter
y=filter(numd,dend,x);
% fft of input and output signals of the filter
X=fft(x,fsamp);
Y=fft(y,fsamp);
% plotting the input and output spectrum
subplot(2,1,1);
stem(abs(X));
title("Input Spectrum X(k)");
xlabel("k");
subplot(2,1,2);
stem(abs(Y));
title("Output Spectrum Y(k)"); xlabel("k");
OUTPUT:
fp = 800

Wp=

5.0265e+03

fs = 200

Ws=

1.2566e+03

fsamp = 2000

ap_db = 2

as_db = 35

Order of the filter:

Cut-off frequency of the filter:

5.0265e+03

Numerator Coefficients of TF:

1 0 0 0

Denominator Coefficients of TF:

1.0e+11 *

0.0000 0.0000 0.0006 3.8852


3] Design a digital Butterworth IIR filter to meet the following
specifications:
Pass band edge frequency:200 Hz
Stop band edge frequency:800
Pass band attenuation: 2dB
Stop band attenuation:35dB
Sampling rate: 2kHz
(i)Find the magnitude response and phase response of the system
(ii)Find the spectrum of input and output signals if the input
signal is x(t)=cos(2πf1t)+2cos(2πf2t)+2sin(2πf3t), where, f1=50Hz,
f2=100Hz and f3=950Hz.
clc;
clear all;
close all;
% Getting inputs from the user
fp=input("Enter the pass-band edge frequency in Hz: ");
fsamp=input("Enter the sampling frequency in Hz: ");
% Calculating normalised frequency in rad/sec
disp("Wp= ");
disp(2*pi*fp/10000);
fs=input("Enter the stop-band edge frequency in Hz: ")
disp("Ws= ");
disp(2*pi*fs/10000);
ap=input("Enter the pass-band attenuation value: ");
as=input("Enter the stop-band attenuation value: ");
% dB calculation for ap and as
if ap<1
ap_db=20*log10(ap)
else
ap_db=ap
end
if as<1
as_db=20*log10(as)
else
as_db=as
end
% Finding order and cut-off frequency of the filter
[N,Wc]=buttord(2*pi*fp/10000,2*pi*fs/10000,ap_db,as_db,'z');
disp("Order of the filter: ");
disp(N);
disp("Cut-off frequency of the filter: ");
disp(Wc);
% Finding the coefficients of the transfer function
[num,den]=butter(N,Wc,"low",'z');
disp("Numerator Coefficients: ");
disp(num);
disp("Denominator Coefficients: ");
disp(den);
% Converting transfer function form to zero-pole-gain form
[zd pd kd]=tf2zp(num,den);
% Frequency response of the digital lpf filter
freqz(num,den);
title("Digital LPF");
% Sampling the input signal
t=0:1/fsamp:1;
f1=input('f1: ');
f2=input('f2: ');
f3=input('f3: ');
x=cos(2*pi*f1*t)+2*cos(2*pi*f2*t)+2*sin(2*pi*f3*t);
% Applying input signal to the filter
y=filter(num,den,x);
% fft of input and output signals of the filter
X=fft(x,2000);
Y=fft(y,2000);
% Plotting the input and output spectrum
subplot(2,1,1);
stem(abs(X));
title("Input Spectrum X(k)");
xlabel("k");
subplot(2,1,2);
stem(abs(Y));
title("Output Spectrum Y(k)");
xlabel("k");

OUTPUT:
fp = 200

fsamp = 1000

Wp=

0.1257

fs = 800

Ws=

0.5027

ap_db = 2

as_db = 35

Order of the filter:

Cut-off frequency of the filter:

0.1639

Numerator Coefficients of TF:

0.0108 0.0325 0.0325 0.0108

Denominator Coefficients of TF:

1.0000 -1.9802 1.4197 -0.3528


4] Design a digital Chebyshev IIR filter to meet the following
specifications:
Pass band edge frequency:800 Hz
Stop band edge frequency:200 Hz
Pass band attenuation: 2dB
Stop band attenuation:35dB
Sampling rate: 2kHz
(i)Find the magnitude response and phase response of the system
(ii)Find the spectrum of input and output signals if the input
signal is x(t)=cos(2πf1t)+2cos(2πf2t)+2sin(2πf3t), where, f1=50Hz,
f2=100Hz and f3=950Hz.

clc;
clear all;
close all;
% Getting inputs from the user
fp=input("Enter the pass-band edge frequency in Hz: ")
disp("Wp= ");
disp(2*pi*fp/10000);
fs=input("Enter the stop-band edge frequency in Hz: ")
disp("Ws= ");
disp(2*pi*fs/10000);
ap=input("Enter the pass-band attenuation value: ");
as=input("Enter the stop-band attenuation value: ");
fsamp=input("Enter the sampling frequency in Hz: ");
% dB calculation for ap and as
if ap<1
ap_db=20*log10(ap)
else
ap_db=ap
end
if as<1
as_db=20*log10(as)
else
as_db=as
end
% finding order and cut-off frequency of the filter
[N,Wc]=cheb1ord((2*pi*fp/10000),(2*pi*fs/10000),ap_db,as_db,'z');
disp("Order of the filter: ");
disp(N);
disp("Cut-off frequency of the filter: ");
disp(Wc);
% finding the coefficients of the transfer function
[num,den]=cheby1(N,ap_db,Wc,"high",'z');
disp("Numerator Coefficients of TF: ");
disp(num);
disp("Denominator Coefficients of TF: ");
disp(den);
% converting transfer function form to zero-pole-gain form
[zd pd kd]=tf2zp(num,den);
% frequency response of the digital hpf filter
freqz(num,den);
title("Digital HPF");
% sampling the input signal
t=0:1/fsamp:1;
f1=input('f1: ');
f2=input('f2: ');
f3=input('f3: ');
x=cos(2*pi*f1*t)+2*cos(2*pi*f2*t)+2*sin(2*pi*f3*t);
% applying input signal to the filter
y=filter(num,den,x);
% fft of input and output signals of the filter
X=fft(x,fsamp);
Y=fft(y,fsamp);
% plotting the input and output spectrum
subplot(2,1,1);
stem(abs(X));
title("Input Spectrum X(k)");
xlabel("k");
subplot(2,1,2);
stem(abs(Y));
title("Output Spectrum Y(k)");
xlabel("k");

OUTPUT:
fp = 800

Wp=

5.027

fs = 200

Ws=

1.257

fsamp = 2000

ap_db = 2

as_db = 35

Order of the filter:

Cut-off frequency of the filter:

5.027

Numerator Coefficients of TF:

0.1043 -0.3130 0.3130 -0.1043

Denominator Coefficients of TF:

1.0000 0.5792 0.7263 0.3124


5] Design an Analog Band Pass IIR filter to meet the following
specifications:
Pass band lower cut-off frequency: 50 Hz
Pass band upper cut-off frequency: 20 kHz
Stop band lower cut-off frequency: 20 Hz
Stop band upper cut-off frequency: 45 kHz
Pass band attenuation: 3dB
Stop band attenuation:20dB
Find the monotonic frequency response of the system.
clc;
clear all;
close all;
% Getting inputs from the user
fp1=input("Enter the pass-band lower edge frequency in Hz: ")
disp("Wp1= ");
disp(2*pi*fp1);
fp2=input("Enter the pass-band upper edge frequency in Hz: ")
disp("Wp2= ");
disp(2*pi*fp2);
fs1=input("Enter the stop-band lower edge frequency in Hz: ")
disp("Ws1= ");
disp(2*pi*fs1);
fs2=input("Enter the stop-band upper edge frequency in Hz: ")
disp("Ws2= ");
disp(2*pi*fs2);
ap=input("Enter the pass-band attenuation value: ");
as=input("Enter the stop-band attenuation value: ");
% dB calculation for ap and as
if ap<1
ap_db=20*log10(ap)
else
ap_db=ap
end
if as<1
as_db=20*log10(as)
else
as_db=as
end
% Finding order and cut-off frequency of the filter
[N,Wc]=buttord([2*pi*fp1 2*pi*fp2],[2*pi*fs1
2*pi*fs2],ap_db,as_db,'s');
disp("Order of the filter: ");
disp(N);
disp("Cut-off frequency of the filter: ");
disp(Wc);
% Finding the coefficients of the transfer function
[num,den]=butter(N,Wc,"bandpass",'s');
disp("Numerator Coefficients of TF: ");
disp(num);
disp("Denominator Coefficients of TF: ");
disp(den);
% frequency response of the analog bpf filter
w=0:500000;
[h,w]= freqs(num,den,w);
plot(w,20*log10(h));
title("Magnitude response of analog BPF");
xlabel("Frequency(rad/sec)");
ylabel("Magnitude(dB)");
plot(w,angle(h));
title("Phase response of analog BPF");
xlabel("Frequency(rad/sec)");
ylabel("Phase(degrees)")

OUTPUT:
fp1 = 50

Wp1=

314.1593

fp2 = 20000

Wp2=

1.2566e+05

fs1 = 20

Ws1=

125.6637

fs2 = 45000
Ws2=

2.8274e+05

ap_db = 3

as_db = 20

Order of the filter:

Cut-off frequency of the filter:

1.0e+05 *

0.0030 1.3169

Numerator Coefficients of TF:

1.0e+15 *

0 0 0 2.2684 0 0 0

Denominator Coefficients of TF:

1.0e+22 *

0.0000 0.0000 0.0000 0.0000 0.0001 0.0410 6.1529


6] Design a Digital Band Pass IIR filter using impulse invariance
method to meet the following specifications:
Pass band lower cut-off frequency: 50 Hz
Pass band upper cut-off frequency: 20 kHz
Stop band lower cut-off frequency: 20 Hz
Stop band upper cut-off frequency: 45 kHz
Pass band attenuation: 3dB
Stop band attenuation:20dB
Sampling rate: 100kHz
Find the monotonic frequency response of the system.
clc;
clear all;
close all;
% Getting inputs from the user
fp1=input("Enter the pass-band lower edge frequency in Hz: ")
disp("Wp1= ");
disp(2*pi*fp1);
fp2=input("Enter the pass-band upper edge frequency in Hz: ")
disp("Wp2= ");
disp(2*pi*fp2);
fs1=input("Enter the stop-band lower edge frequency in Hz: ")
disp("Ws1= ");
disp(2*pi*fs1);
fs2=input("Enter the stop-band upper edge frequency in Hz: ")
disp("Ws2= ");
disp(2*pi*fs2);
fsamp=input("Enter the sampling frequency");
ap=input("Enter the pass-band attenuation value: ");
as=input("Enter the stop-band attenuation value: ");
% dB calculation for ap and as
if ap<1
ap_db=20*log10(ap)
else
ap_db=ap
end
if as<1
as_db=20*log10(as)
else
as_db=as
end
% Finding order and cut-off frequency of the filter
[N,Wc]=buttord([2*pi*fp1 2*pi*fp2],[2*pi*fs1
2*pi*fs2],ap_db,as_db,'s');
disp("Order of the filter: ");
disp(N);
disp("Cut-off frequency of the filter: ");
disp(Wc);
% Finding the coefficients of the transfer function
[num,den]=butter(N,Wc,"bandpass",'s');
disp("Numerator Coefficients of TF: ");
disp(num);
disp("Denominator Coefficients of TF: ");
disp(den);
% frequency response of the analog bpf filter
w=0:500000;
[h,w]= freqs(num,den,w);
[num1,den1]=impinvar(num,den,fsamp);
freqz(num1,den1);
title("Digital BPF");
OUTPUT:
fp1 = 50

Wp1=

314.1593

fp2 = 20000

Wp2=

1.2566e+05

fs1 = 20

Ws1=

125.6637

fs2 = 45000

Ws2=

2.8274e+05

fsamp = 100000

ap_db = 3

as_db = 20

Order of the filter:

Cut-off frequency of the filter:

1.0e+05 *

0.0030 1.3169

Numerator Coefficients of TF:

1.0e+15 *

0 0 0 2.2684 0 0 0
Denominator Coefficients of TF:

1.0e+22 *

0.0000 0.0000 0.0000 0.0000 0.0001 0.0410 6.1529

7] )Design an Analog Band stop IIR filter to meet the following


specifications:
Pass band lower cut-off frequency: 25 Hz
Pass band upper cut-off frequency: 225 Hz
Stop band lower cut-off frequency: 100 Hz
Stop band upper cut-off frequency: 150 Hz
Pass band attenuation: 3dB
Stop band attenuation:18dB
Find the monotonic frequency response of the system.
clc;
clear all;
close all;
% Getting inputs from the user
fp1=input("Enter the pass-band lower edge frequency in Hz: ")
disp("Wp1= ");
disp(2*pi*fp1);
fp2=input("Enter the pass-band upper edge frequency in Hz: ")
disp("Wp2= ");
disp(2*pi*fp2);
fs1=input("Enter the stop-band lower edge frequency in Hz: ")
disp("Ws1= ");
disp(2*pi*fs1);
fs2=input("Enter the stop-band upper edge frequency in Hz: ")
disp("Ws2= ");
disp(2*pi*fs2);
ap=input("Enter the pass-band attenuation value: ");
as=input("Enter the stop-band attenuation value: ");
% dB calculation for ap and as
if ap<1
ap_db=20*log10(ap)
else
ap_db=ap
end
if as<1
as_db=20*log10(as)
else
as_db=as
end
% Finding order and cut-off frequency of the filter
[N,Wc]=buttord([2*pi*fp1 2*pi*fp2],[2*pi*fs1
2*pi*fs2],ap_db,as_db,'s');
disp("Order of the filter: ");
disp(N);
disp("Cut-off frequency of the filter: ");
disp(Wc);
% Finding the coefficients of the transfer function
[num,den]=butter(N,Wc,"stop",'s');
disp("Numerator Coefficients of TF: ");
disp(num);
disp("Denominator Coefficients of TF: ");
disp(den);
% frequency response of the analog bpf filter
w=0:2500;
[h,w]= freqs(num,den,w);
plot(w,20*log10(h));
title("Magnitude response of analog BSF");
xlabel("Frequency(rad/sec)");
ylabel("Magnitude(dB)");
plot(w,angle(h));
title("Phase response of analog BSF");
xlabel("Frequency(rad/sec)");
ylabel("Phase(degrees)")

OUTPUT:
fp1 = 25

Wp1=

157.0796

fp2 = 225

Wp2=

1.4137e+03

fs1 = 100

Ws1=

628.3185

fs2 = 150

Ws2=

942.4778

ap_db = 3

as_db = 18

Order of the filter:

2
Cut-off frequency of the filter:

1.0e+03 *

0.4460 1.3279

Numerator Coefficients of TF:

1.0e+11 *

0.0000 0 0.0000 0 3.5067

Denominator Coefficients of TF:

1.0e+11 *

0.0000 0.0000 0.0000 0.0074 3.5067


8] )Design a Digital Band stop IIR filter using bilinear
transformation to meet the following specifications:
Pass band lower cut-off frequency: 25 Hz
Pass band upper cut-off frequency: 225 Hz
Stop band lower cut-off frequency: 100 Hz
Stop band upper cut-off frequency: 150 Hz
Pass band attenuation: 3dB
Stop band attenuation:18dB
Sampling rate: 500Hz
Find the monotonic frequency response of the system.
clc;
clear all;
close all;
% Getting inputs from the user
fp1=input("Enter the pass-band lower edge frequency in Hz: ")
disp("Wp1= ");
disp(2*pi*fp1);
fp2=input("Enter the pass-band upper edge frequency in Hz: ")
disp("Wp2= ");
disp(2*pi*fp2);
fs1=input("Enter the stop-band lower edge frequency in Hz: ")
disp("Ws1= ");
disp(2*pi*fs1);
fs2=input("Enter the stop-band upper edge frequency in Hz: ")
disp("Ws2= ");
disp(2*pi*fs2);
fsamp=input("Enter the sampling frequency in Hz: ");
ap=input("Enter the pass-band attenuation value: ");
as=input("Enter the stop-band attenuation value: ");
% dB calculation for ap and as
if ap<1
ap_db=20*log10(ap)
else
ap_db=ap
end
if as<1
as_db=20*log10(as)
else
as_db=as
end
% Finding order and cut-off frequency of the filter
[N,Wc]=buttord([2*pi*fp1 2*pi*fp2],[2*pi*fs1
2*pi*fs2],ap_db,as_db,'s');
disp("Order of the filter: ");
disp(N);
disp("Cut-off frequency of the filter: ");
disp(Wc);
% Finding the coefficients of the transfer function
[num,den]=butter(N,Wc,"stop",'s');
disp("Numerator Coefficients of TF: ");
disp(num);
disp("Denominator Coefficients of TF: ");
disp(den);
% frequency response of the analog bsf filter
w=0:2500;
[h,w]= freqs(num,den,w);
% Converting transer function form to zero-pole-gain form
[zs ps ks]= tf2zp(num,den);
% Converting transfer function conversion using bilinear
transformation
[zd pd kd]=bilinear(zs,ps,ks,fsamp);
% Converting zero-pole-gain form to tf form
[num1 den1]=zp2tf(zd,pd,kd);
freqz(num1,den1);
title("Digital BSF using BLT");
OUTPUT:
fp1 = 25

Wp1=

157.0796

fp2 = 225

Wp2=

1.4137e+03

fs1 = 100

Ws1=

628.3185

fs2 = 150

Ws2=

942.4778

fsamp = 500

ap_db = 3

as_db = 18

Order of the filter:

Cut-off frequency of the filter:

1.0e+03 *

0.4460 1.3279

Numerator Coefficients of TF:


1.0e+11 *

0.0000 0 0.0000 0 3.5067

Denominator Coefficients of TF:

1.0e+11 *

0.0000 0.0000 0.0000 0.0074 3.5067

RESULT:
Thus, the IIR filters are designed and the outputs are verified using
MATLAB.
HARITHA C
2018105540

EXPERIMENT 8: DESIGN OF FIR FILTERS

AIM:
To design digital FIR filters using different windowing techniques.
SOFTWARE USED:
MATLAB R2020a
CODE:
1] Design a low pass FIR filter for the following specifications:
Cut off frequency = 0.25 kHz
Sampling frequency = 1 kHz
Filter length = 11
Use Hanning window and rectangular window

HANNING WINDOW:
clc;
clear all;
close all;
% Gets inputs from the user
m= input("Enter the length of filter");
fc= input("Enter the cut off frequency in Hz");
fs= input("Enter the sampling frequency in Hz");
% Normalising the cut off frequency
fc_n =2*(fc/fs);
order=m-1;
% Filter response for LPF using Hanning window
a=fir1(order, fc_n, "low", hann(m));
freqz(a);
title("LPF using hanning window");

OUTPUT:
Enter the length of filter
11
Enter the cut off frequency in Hz
250
Enter the sampling frequency in Hz
1000

RECTANGULAR WINDOW:
clc;
close all;
clear all;
% Gets input from the user
m= input("Enter the length of filter");
fc= input("Enter the cut off frequency in Hz");
fs= input("Enter the sampling frequency in Hz");
% Normalising of cut off frequency
fc_n =2*(fc/fs);
order=m-1;
% Filter response of LPF using rectangular window
a=fir1(order, fc_n, "low", rectwin(m));
freqz(a);
title("LPF using rectangular window");

OUTPUT:
Enter the length of filter
11
Enter the cut off frequency in Hz
250
Enter the sampling frequency in Hz
1000
2] Design a high pass FIR filter for the following specifications:
Cut off frequency = 0.25 kHz
Sampling frequency = 1 kHz
Filter length = 7
Use Hamming window
clc;
clear all;
close all;
% Gets inputs from the user
m= input("Enter the length of filter");
fc= input("Enter the cut off frequency in Hz");
fs= input("Enter the sampling frequency in Hz");
% Normalising the cut off frequency
fc_n =2*(fc/fs);
order=m-1;
% Filter response for HPF using Hamming window
a=fir1(order, fc_n, "high", hamming(m));
freqz(a);
title("HPF using hamming window");

OUTPUT:
Enter the length of filter
7
Enter the cut off frequency in Hz
250
Enter the sampling frequency in Hz
1000
3] Design a band pass FIR filter for the following specifications:
Fc1 = 100 Hz
Fc2 = 200 Hz
Fs = 1 kHz
Filter length = 50
Use Blackman window
clc;
close all;
clear all;
% Gets input from the user
m= input("enter the length of filter");
fc1= input("enter the cut off frequency 1 in Hz");
fc2= input("enter the cut off frequency 2 in Hz");
fs= input("enter the sampling frequency in Hz");
% Normalising of cut of frequencies
fc_n1 =2*(fc1/fs);
fc_n2 = 2*(fc2/fs);
order=m-1;
% Frequency response of BPF using Blackman window
a=fir1(order, [fc_n1 fc_n2] , "bandpass", blackman(m));
freqz(a);
title("BPF using blackman window");

OUTPUT:
enter the length of filter
50
enter the cut off frequency 1 in Hz
100
enter the cut off frequency 2 in Hz
200
enter the sampling frequency in Hz
1000
4] Design a band stop FIR filter for the following specifications:
Fc1 = 100 Hz
Fc2 = 200 Hz
Fs = 1kHz
Filter length = 41
Use Blackman window
clc;
close all;
clear all;
% Gets input from the user
m= input("enter the length of filter");
fc1= input("enter the cut off frequency 1");
fc2= input("enter the cut off frequency 2");
fs= input("enter the sampling frequency");
% Normalising of cut off frequencies
fc_n1 =2*(fc1/fs);
fc_n2 = 2*(fc2/fs);
order=m-1;
% Frequency response of BSF using Blackman window
a=fir1(order, [fc_n1 fc_n2] , "stop", blackman(m));
freqz(a,m,512);
title("BSF using blackman window");

OUTPUT:
enter the length of filter
41
enter the cut off frequency 1
100
enter the cut off frequency 2
200
enter the sampling frequency
1000
RESULT:
Thus, FIR filters are designed using different windows and the
outputs are verified using MATLAB.

You might also like