DSP LAB 1to 6
DSP LAB 1to 6
Aim: Write a program to compute N point DFT of a given sequence and plot
magnitude and phase spectrum.
%write a program to compute N point DFT of a given sequence and plot magnitude and phase
spectrum.
clc;
close all;
clear all;
N = input(' Enter the DFT sequence:');
j =sqrt(-1);
xn = zeros (1,N);
xn(1)=1/3;
xn(2) =1/3;
xn(3) = 1/3;
XK =zeros (1,N);
for k =0:1:N-1
for n= 0:1:N-1
XK(k+1) =XK(k+1)+xn(n+1)*exp(-j*2*pi*k*n/N);
end
end
disp('The DFT sequence is');
XK
disp('The Magnitude sequence is ');
MagXK = abs(XK)
disp ('the phase sequence is');
phaXK =angle(XK)
wk =0:1:N-1;
subplot(2,1,1); stem(wk,MagXK);
title('Magnitude spectrum');
xlabel('k');
ylabel ('MagXK');
subplot(2,1,2)
stem(wk, phaXK);
title('Phase spectrum');
xlabel('k');
ylabel ('PhaseXK');
1
2
EXPERIMENT NO -02
Aim: To find a linear Convolution of two given sequences
clc;
close all;
x=input('enter the value of x(n):');%[1 2 3 1]
y=input('enter the value of y(n):');%[1 1 2 3]
v=conv(x,y);
x3=length(v);
t1=0:1:x3-1;
subplot(1,1,1);
stem(t1,v);
disp(v);
title('linear convolution of x(n) and y(n):');
xlabel('frequency');
ylabel('amplitude');
grid on;
3
4
EXPERIMENT NO -03
Aim: To find a Circular Convolution of two given sequences
5
6
EXPERIMENT NO - 04
Aim: To Perform linear Convolution from Circular Convolution and vice
versa
Code:
%Program to find the linear convolution using circular convolution method
clc;
clear all;
close all;
x1=input('Enter the value of First sequence:'); %[1 2 3 1]
x2=input('Enter the value of Second sequence:'); %[1 1 2 3]
l1=length(x1);
l2=length(x2);
n=l1+l2-1;
display(n);
subplot(3,1,1);
stem(x1);
xlabel('time');
ylabel('Amplitude');
title('first Sequence(x1)');
grid on;
subplot(3,1,2);
stem(x2);
xlabel('time');
ylabel('Amplitude');
title('Second Sequence(x2)');
grid on;
if l1>l2
l3=l1-l2
x2=[x2,zeros(1,l3)];
elseif l2>l1
l3=l2-l1
x1=[x1,zeros(1,l3)];
end
n=l1+l2-1
v=conv(x1,x2);
disp('The Convoluted Sequence is:');
disp(v);
subplot(3,1,3);
stem(v);
xlabel('time');
ylabel('Amplitude');
title('The Convoluted Sequence of x1 and x2');
grid on;
7
OUTPUT
8
EXPERIMENT NO - 05
Aim: To Implement Time Shifting and Time Reversal property of DFT
Code:
clc;
clear all;
close all;
t=input('Enter the Time Sequence');%[1,2,3,4]
n=input('Enter the Amplitude Sequence');%[0,1,2,3]
subplot(4,1,1);
stem(t,n);
title('Original signal');
xlabel('amplitude');
ylabel('time');
grid on;
subplot(4,1,2);
stem(t-1,n);
title('Advanced Signal');
xlabel('amplitude');
ylabel('time');
grid on;
subplot(4,1,3);
stem(t+1,n);
title('Delayed Signal');
xlabel('amplitude');
ylabel('time');
grid on;
subplot(4,1,4);
stem(-t,n);
title('Reversed Signal');
xlabel('amplitude');
ylabel('time');
grid on;
9
OUTPUT
10
EXPERIMENT NO - 06
To Implement Parseval Theorem of DFT
Code:
% Define a signal in the time domain
clc;
clear all;
close all;
N = input('Enter the no. of Samples'); % 64
display(N);
t = 0:1:N-1; % Time vector
x = cos(2*pi*0.1*t) + 0.5*cos(2*pi*0.3*t); % Example signal
% Calculate the DFT of the signal
X = fft(x);
% Calculate the energy in the time domain
energy_time_domain = sum(abs(x).^2);
% Calculate the energy in the frequency domain using Parseval's theorem
energy_frequency_domain = sum(abs(X).^2) / N;
% Display the energies and compare them
fprintf('Energy in the time domain: %.2f\n', energy_time_domain);
fprintf('Energy in the frequency domain: %.2f\n', energy_frequency_domain);
% Check if Parseval's theorem holds (within a small tolerance)
tolerance = 1e-10;
if abs(energy_time_domain - energy_frequency_domain) < tolerance
disp('Parseval''s theorem holds: Energy is conserved.');
else
disp('Parseval''s theorem does not hold: Energy is not conserved.');
end
subplot(1,1,1);
stem(energy_frequency_domain,energy_time_domain);
title('energy plot')
xlabel('Energy time domain');
ylabel('Energy frequency domain');
grid on;
11
Output:
12