Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

National Institute of Technology Uttarakhand

Department of Electronics Engineering

ECL304- Digital Signal Processing

Lab Module -III (Even Semester-2020)

1. MATLAB CODE:

% calculating twiddle factor

w = [];

% for 4 point DFT


Nfo = 4;

% for 6 point DFT


Nsix = 6;

% for 8 point DFT


Neig = 8;

% for 10 point DFT


Nten = 10;

% take arbitrary signal of the given lengths


xfo = [4 , 2 , 1 , 3];
xsix = [5, 1, 3, 4, 2, 6];
xeig = [7 , 3, 4, 2, 5, 1, 6, 8];
xten = [10 , 2 , 1 , 4 ,5 , 6 , 7 , 8 , 9 ,3];

% performing computations for 4 point DFT


for i = 0 : Nfo - 1
for k = 0 : Nfo - 1
w(i + 1 , k + 1) = exp(-2.*pi.*j.* k * i./Nfo);
end
end

% plotting 4 point DFT


y4 = w * xfo.';
figure;
stem(y4);
title('4 point DFT');
xlabel('time');
ylabel('amplitude');

% performing computations for 6 point DFT


for i = 0 : Nsix - 1
for k = 0 : Nsix - 1
w(i + 1 , k + 1) = exp(-2.*pi.*j.* k * i./Nsix);
end
end

% plotting 6 point DFT


y6 = w * xsix.';
figure;
stem(y6);
title('6 point DFT');
xlabel('time');
ylabel('amplitude');

% perform computations for 8 point DFT


for i = 0 : Neig - 1
for k = 0 : Neig - 1
w(i + 1 , k + 1) = exp(-2.*pi.*j.* k * i./Neig);
end
end

% plotting 8 point DFT


y8 = w * xeig.';
figure;
stem(y8);
title('8 point DFT');
xlabel('time');
ylabel('amplitude');

% performing computations for 10 point DFT


for i = 0 : Nten - 1
for k = 0 : Nten - 1
w(i + 1 , k + 1) = exp(-2.*pi.*j.* k * i./Nten);
end
end

% plotting 10 point DFT


y10 = w * xten.';
figure;
stem(y10);
title('10 point DFT');
xlabel('time');
ylabel('amplitude');

% For matching the outputs of 4,6,8 and 10 point DFTs


y=fft(xfo);
figure;
stem(y);
title('DTFT for xfo');

y1=fft(xsix);
figure;
stem(y1);
title('DTFT for xsix');

y2=fft(xeig);
figure;
stem(y2);
title('DTFT for xeig');
y3=fft(xten);
figure;
stem(y3);
title('DTFT for xten');

OUTPUT:
2.MATLAB CODE:

w=[];
w1=[];
N=6;
x=[3 2 -2 1 0 1]';
h=[-5 -1 3 -2 0 0]';
for i=0:N-1
for k=0:N-1
w(i+1,k+1)=exp(-2.*pi.*j.*k*i./N);
end
end
for i=0:N-1
for k=0:N-1
w1(i+1,k+1)=exp(2.*pi.*j.*k*i./N);
end
end
X=w*x;
H=w*h;
C=X.*H;
I=(w1*C)./N;
stem(C)

OUTPUT:

3. MATLAB CODE:

x = [1,2,3,4,5,6,7,8,9,10];
h = [1,0,-1];
B = 5;
N = length(x);
M = length(h);
m = M - 1;
len = N + M -1;
r = 0:1:len-1;

H = [h,zeros(1,m)];

X1 = [zeros(1,m),x(1:m+1)];
X2 = [X1((length(X1)-1):length(X1)),x(4:m+4)];
X3 = [X2((length(X2)-1):length(X2)),x(7:m+7)];
X4 = [X3((length(X3)-1):length(X3)),x(10),zeros(1,m)];

disp(X1);
disp(X2);
disp(X3);
disp(X4);
disp(H);

n1 = length(X1);
n2 = length(H);
% Setting new axis in accordance to circular convolution requirements.
y1 = max(n1,n2);
a=1:y1;
% Making zero vector of final length
Y1 = zeros(1,y1);

for i =0:y1-1
for j = 0:y1-1
k1 = mod((i-j),y1);
Y1(i+1) = Y1(i+1) + X1(j+1)*H(k1+1);
end
end
disp(Y1);

n3 = length(X2);
y2 = max(n3,n2);
b = 1:y2;
% Making zero vector of final length
Y2 = zeros(1,y2);
for i =0:y2-1
for j = 0:y2-1
k2 = mod((i-j),y2);
Y2(i+1) = Y2(i+1) + X2(j+1)*H(k2+1);
end
end
disp(Y2);

n4 = length(X3);
y3 = max(n4,n2);
c = 1:y3;
% Making zero vector of final length
Y3 = zeros(1,y3);
for i =0:y3-1
for j = 0:y3-1
k3 = mod((i-j),y3);
Y3(i+1) = Y3(i+1) + X3(j+1)*H(k3+1);
end
end
disp(Y3);

n5 = length(X4);
y4 = max(n5,n2);
d = 1:y4;
% Making zero vector of final length
Y4 = zeros(1,y4);
for i =0:y4-1
for j = 0:y4-1
k4 = mod((i-j),y4);
Y4(i+1) = Y4(i+1) + X4(j+1)*H(k4+1);
end
end
disp(Y4);

result = conv(x,h);
disp(result);
figure;
stem(r,result);

Y = [Y1(m+1:y1),Y2(m+1:y2),Y3(m+1:y3),Y4(m+1:y4)];
disp(Y);
figure;
stem(r,Y);

OUTPUT:

4.MATLAB CODE:
n=0:10;
x=n.^2;
y=fliplr(x);

%X[<n+4>11]
N1=11;
%DFT of sequence x[n] can be calculated as follows:
X=zeros(1,11);
for k=0:10
for m=0:10
X(k+1) = X(k+1) + x(m+1).*exp(-(1i*2*pi*k*m)./N1);
end
end

C=zeros(1,11);
C(1)=X(1);
for i = 2:11
C(i) = X(size(n,2)-i+2);
end
% To Calculate EVEN and ODD component
EVEN = (x + y).*(1/2);
odd = (x - y).*(1/2);
figure;
subplot(2,1,1);
plot(n,x);
ylabel('EVEN COMPONENT')
subplot(2,1,2);
plot(n,y);
ylabel('ODD COMPONENT')

n1=(n-4);
X1=zeros(1,15);
t=-4:1:10;
X1(find(t>=min(n1)&t<=max(n1)==1))=X;

%X[<n-3>15]
N2=15;
X_2=zeros(1,11);
for k=0:10
for m=0:10
X_2(k+1) = X_2(k+1) + x(m+1).*exp(-(1i*2*pi*k*m)./N2);
end
end

X2=zeros(1,14);
n2=n+3;
t1=0:13;
X2(find(t1>=min(n2)&t1<=max(n2)==1))=X_2;

figure;
stem(n,x,'r')
hold on
stem(n,y)
grid on
legend('x[n]=n^2','x[-n]')
axis([-0.5 10.5 0 101])

figure;
subplot(2,1,1)
stem(n,X)
grid on
axis([-1 11 -200 400])
subplot(2,1,2)
stem(t,X1)
grid on
axis([-4.5 10 -200 400])

figure;
subplot(2,1,1)
stem(n,X_2)
grid on
axis([-1 11 -300 400])
subplot(2,1,2)
stem(t1,X2)
grid on
axis([0 14 -300 400])

OUTPUT:
5. MATLAB CODE:

t=-2:0.1:2;
fsa=1100;
fmes1=40;
fmes2=100;
Am1=0.7;
Am2=1;
%Continous Analog Signals--
s1=Am1*sin(2*pi*fmes1*t);
s2=Am2*sin(2*pi*fmes2*t);
c=s1+s2;

%Sampling the signals the sampling frequency of 1000Hz


%Sampled signals in the same interval of time are as follows:
ts=-2:1/fsa:2;
sa1=Am1*sin(2*pi*fmes1*ts);
sa2=Am2*sin(2*pi*fmes2*ts);
sa=sa1+sa2;
%Zero mean random noise for corrupting the signal
noise=randn([1,numel(ts)]);
SNR=5;

signal=sa+randn(1,numel(ts));
%[corrupt_signal,ratio]=SNRS(sa,noise,SNR);
% OR
%corrupt_signal=sa+randn(1,numel(ts));
% With the frequency domain analysis of the signal we can see that the
% noise has frequency near the value of 0Hz.
fds=fft(signal);
f=1./ts;
p=periodogram(fds);
%Verification of Parseval's
N=length(ts);
E1_timedomain=sum(abs(signal.^2)) %Value of energy in time and frequency
E1_frequdomain=sum(abs(fds.^2))/N %domain must be equal
subplot(411)
plot(ts,sa)
title('40Hz & 100Hz sinusoidal signals sampled at 1100Hz')
grid on
axis([-2.2 2.2 -2 2])
subplot(412)
plot(f,fds)
title('Frequency domain representation of corrupted signal with Noise
Component at 0Hz (Zero Mean)')
grid on
subplot(413)
plot(ts,signal)
title('Signal corrupted with zero mean random noise')
grid on
axis([-2.2 2.2 -5 5]);
subplot(414)
plot(p)
title('Power Spectral Density of the above signal')
grid on
axis([0 4100 0 16000])
OUTPUT:

6.MATLAB CODE:

clc;
clear all;
close all;
load('rec_1m');
x=val;
N = length(x);
xdft = fft(x);
xdft = xdft(1:N/2+1);
Fs=500;
psdx = (1/(Fs*N)) * abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
freq = 0:Fs/length(x):Fs/2;
figure;plotATM('rec_1m');title('Time-Domain Plot');
figure;plot(freq,10*log10(psdx)),title('Power Spectral Density Using FFT'),
xlabel('Frequency (Hz)'),ylabel('Power/Frequency (dB/Hz)');
energy=0;
for r = 1:length(psdx)
energy= energy+ (abs(psdx(r))^2);
end
disp('Energy of the sequence:');
disp(energy);
disp('Power of the sequence:');
disp(energy/(2*length(psdx)));
OUTPUT:

You might also like