DSP - LAB-2, 3, 4 and 5 - Report
DSP - LAB-2, 3, 4 and 5 - Report
Lab Experiment – 02
Apparatus: MATLAB
Program:
a) Code
clc;
clear all;
close all;
N=40;
n=0:1:N-1;
eq1 = sin(0.5*pi*n);
eq2 = sin(0.7*pi*n);
sm = eq1 + eq2;
subplot(2,1,1),stem(n, sm);
subplot(2,1,2),plot(n, sm);
xlabel('n');ylabel('sm');
title('Addition of two Sinusoidal Sequences');
a) Code
clc;
clear all;
close all;
N=40;
n=0:1:N-1;
eq1 = sin(0.5*pi*n);
eq2 = sin(0.7*pi*n);
ml = eq1 .* eq2;
subplot(2,1,1),stem(n, ml);
subplot(2,1,2),plot(n, ml);
xlabel('n');ylabel('ml');
title('Multiplication of two Sinusoidal Sequences');
a) Code
clc;
clear all;
close all;
N=5;
l1=[1 2 3 4 5 6 7 8];
a=length(l1);
n=0:1:a-1;
subplot(3,1,1);
stem(l1);
xlabel('Number of Samples');ylabel('Amplitude');
title('Input Signal');
l2=N*l1;
subplot(3,1,2);
stem(l2);
xlabel('Number of Samples');ylabel('Amplitude');
title('Amplified Signal');
l3=l1/N;
subplot(3,1,3);
stem(l3);
xlabel('Number of Samples');ylabel('Amplitude');
title('Attenuated Signal');
display(l1);
display(l2);
display(l3);
b) Code and Output Screenshot
a) Code
clc;
clear all;
close all;
n = 0:8;
x = [0 1 6 3 2 7 1 4 5];
subplot(2,1,1);
stem(n,x);
title('x(n) Signal');
xlabel('n'); ylabel('x(n)');
m=n+2;
y=x;
subplot(2,1,2);
stem(m,y);
title('y(n)=x(n-2) Signal');
xlabel('n'); ylabel('y(n)');
b) Code and Output Screenshot
a) Code
clc;
clear all;
close all;
n=0:8;
x=[0 2 4 6 3 1 7 5 3];
subplot(2,1,1);
stem(n,x),title('x(n) Signal');
xlabel('n');ylabel('x(n)');
m=fliplr(n);
y=fliplr(x);
subplot(2,1,2);
stem(m,y),title('y(n) = - x(n) Signal');
xlabel('n');ylabel('y(n)');
b) Code and Output Screenshot
a) Code
clc;
clear all;
close all;
N=40;
n=0:1:N-1;
eq1 = sin(0.5*pi*n);
eq2 = sin(0.7*pi*n);
sb = eq1 - eq2;
subplot(2,1,1),stem(n, sb);
subplot(2,1,2),plot(n, sb);
xlabel('n');ylabel('sb');
title('Subtraction of two Sinusoidal Sequences');
b) Code and Output Screenshot
a) Code
clc;
clear all;
close all;
N=40;
n=0:1:N-1;
eq1 = sin(0.5*pi*n);
eq2 = sin(0.7*pi*n);
dv = eq1 ./ eq2;
subplot(2,1,1),stem(n, dv);
subplot(2,1,2),plot(n, dv);
xlabel('n');ylabel('dv');
title('Division of two Sinusoidal Sequences');
b) Code and Output Screenshot
Conclusion:
Thus, we have successfully implemented all the signals in MATLAB.
Lab Experiment – 03
IMPLEMENTATION OF DISCRETE-TIME SYSTEMS
Aim: To find the impulse response of a system with the transfer function
y(n)+1/2y(n-1) + 1/3y(n-2) = x(n).
Apparatus: MATLAB
Program:
IMPULSE RESPONSE OF SYSTEM FOR GIVEN TRANSFER FUNCTION
y(n)+1/2y(n-1) + 1/3y(n-2) = x(n).
a) Code
% IMPULSE RESPONSE OF SYSTEM FOR GIVEN TRANSFER
FUNCTION
% y(n)+1/2y(n-1) + 1/3y(n-2) = x(n).
clc;
clear all;
close all;
b=[1];
a=[1;1/2;1/3];
h=impz(a,b,4);
disp('Impulse Response h(n)');
disp(h);
n1=0:1:length(h)-1;
subplot(2,1,1);
stem(n1,h);
xlabel('n1');ylabel('x(n)');
title('Impulse Response h(n)');
x=[1,2];
y=conv(h,x);
disp('Output y(n)');
disp(y);
n2=0:1:length(y)-1;
subplot(2,1,2);
stem(n2,y);
xlabel('n2');ylabel('Amplitude');
title('Output y(n)');
Conclusion:
Thus, the generation of impulse sequence for given difference
equation is simulated using MATLAB.
VIVA
ANSWERS:
Apparatus: MATLAB
Program:
1. Question 1
a) Code
%1
clc ;
clear all ;
close all ;
x=[1,1,1,1];
N1=length(x);
n1=0:1:N1-1;
subplot(2,2,1),
stem(n1,x);
xlabel('n1');
ylabel('x(n1)');
title('Input sequence 1');
x2=[1,1,1,1];
N2=length(x2);
n2=0:1:N2-1;
subplot(2,2,2),
stem(n2,x2);
xlabel('n2');
ylabel('x(n2)');
title('Input sequence 2');
y=conv(x,x2);
n=0:1:length(y)-1;
subplot(2,1,2),stem(n,y);
xlabel('n');
ylabel('y(n)');
title('Convolution of x(n)*x2(n)');
a) Code
%2
clc ;
clear all ;
close all ;
x=[1,1,0,1,1];
N1=length(x);
n1=-2:1:2;
subplot(2,2,1),
stem(n1,x);
xlabel('n1');
ylabel('x(n1)');
title('Input Sequence 1');
h=[1,-2,-3,4];
N2=length(h);
n2=-3:1:0;
subplot(2,2,2),
stem(n2,h);
xlabel('n2');
ylabel('x(n2)');
title('Input Sequence 2');
y=conv(x,h);
n=0:1:length(y)-1;
subplot(2,1,2),stem(n,y);
xlabel('n');
ylabel('y(n)');
title('Convolution of x(n)*h(n)');
b) Code and Output Screenshot
Conclusion:
Thus, we have successfully implemented Convolution on the
given sequences in MATLAB.
VIVA:
Apparatus: MATLAB
Using a function
a) Code
%Using a function
clc;
clear all;
close all;
x1=input("Enter 1st Sequence:");
x2=input("Enter 2nd Sequence:");
N=max(length(x1),length(x2));
X1=fft(x1,N);
X2= fft(x2,N);
X3= X1.*X2;
x3= ifft(X3,N);
disp('Circular Convolution');
disp(x3);
N=0:1:N-1;
subplot(2,1,1);
stem(N,x3);
xlabel("Range");ylabel("Magnitude");
subplot(2,1,2);
stem(N,abs(X1));
xlabel("Range");ylabel("Magnitude");
b) Code and Output Screenshot
Without a function
• DFT
a) Code
%Without a function
clc;
clear all;
close all;
xn = input("Enter Sequence: ");
N = input("Enter Length: ");
Xk = dft_fun(xn, N);
k=0:N-1;
subplot(2,1,1)
stem(k, abs(Xk))
xlabel("Frequency");ylabel('Magnitude |Xk|')
title('Magnitude Plot')
subplot(2,1,2)
stem(k, angle(Xk))
xlabel("Frequency");ylabel('Angle (Xk)')
title('Phase Plot')
function [Xk] = dft_fun(xn, N)
L = length(xn);
if(N<L)
error("Length should be greater than or equal to
sequence length")
end
xn = [xn zeros(1, N-L)];
for k=0:1:N-1
for n=0:1:N-1
Wn=exp(-j*2*pi*k*n/N);
X1(k+1,n+1)=Wn;
end
end
disp('Transformation matrix for DFT')
disp(xn);
disp(X1);
Xk=X1*xn.';
end
a) Code
Conclusion:
Thus, we have successfully implemented DFT and IDFT in
MATLAB.
VIVA:
1. Define DFT and IDFT (or) what are the analysis and synthesis equations of
DFT?
DFT (Analysis Equation)
IDFT (Synthesis Equation)
2. What is magnitude and phase spectrum?
Magnitude spectrum is the graph between Fourier transform
magnitudes and frequency. Phase spectrum is the graph between
Fourier transform phases and frequency.