Mani Mam Lab Manual
Mani Mam Lab Manual
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
AIM:
To Convolve the given sequences using linear and circular convolution using MATLAB.
SOFTWARE USED:
MATLAB R2020a
CODE:
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:
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
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
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
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
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:
CODE:
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
Columns 12 through 20
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
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
Wp=
1.2566e+03
fs = 800
Ws=
5.0265e+03
fsamp = 2000
ap_db = 2
as_db = 35
1.8356e+03
1.0e+13 *
0 0 0 0 1.1354
1.0e+13 *
Wp=
5.0265e+03
fs = 200
Ws=
1.2566e+03
fsamp = 2000
ap_db = 2
as_db = 35
5.0265e+03
1 0 0 0
1.0e+11 *
OUTPUT:
fp = 200
fsamp = 1000
Wp=
0.1257
fs = 800
Ws=
0.5027
ap_db = 2
as_db = 35
0.1639
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
5.027
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
1.0e+05 *
0.0030 1.3169
1.0e+15 *
0 0 0 2.2684 0 0 0
1.0e+22 *
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
1.0e+05 *
0.0030 1.3169
1.0e+15 *
0 0 0 2.2684 0 0 0
Denominator Coefficients of TF:
1.0e+22 *
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
2
Cut-off frequency of the filter:
1.0e+03 *
0.4460 1.3279
1.0e+11 *
1.0e+11 *
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
1.0e+03 *
0.4460 1.3279
1.0e+11 *
RESULT:
Thus, the IIR filters are designed and the outputs are verified using
MATLAB.
HARITHA C
2018105540
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.