You are on page 1of 6

Lab Report

Lab-4
Digital Signal Processing

Md. Zahidul Islam


160041010
Group: X
 Implementing DFT Algorithms:

Task 1:

Creating the functions myDFT and myIDFT.

CODE:
function [reX,imX] = myDFT(X)

N = size(X,2);
reX = zeros(1,N/2+1);
imX = zeros(1,N/2+1);

for k = 1:N/2+1
for i = 1:N
reX(k) = reX(k) + X(i)*cos(2*pi*(k-1)*(i-1)/N);
imX(k) = imX(k) + X(i)*sin(2*pi*(k-1)*(i-1)/N);
end
end

end

function [X] = myIDFT(reX,imX)

N = (size(reX,2)-1)*2;
X = zeros(1,N);

imX = imX.*(-1);

reX = reX./(N/2);
imX = imX./(N/2);

reX(1) = reX(1)/2;
reX(N/2+1)= reX(N/2+1)/2;

for i = 1:N
for k = 1:N/2+1
X(i) = X(i) + reX(k)*cos(2*pi*(k-1)*(i-1)/N);
X(i) = X(i) + imX(k)*sin(2*pi*(k-1)*(i-1)/N);
end
end

end
Task 2:

Verifying the functions myDFT and myIDFT.

CODE:

X = [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];
[reX, imX] = myDFT(X);
Xrecons = myIDFT(reX, imX);

MagX = (reX.^2 + imX.^2).^(1/2);


PhaX = atan2(imX,reX);

subplot(2,3,1); stem(X); title("X");


subplot(2,3,2); stem(reX); title("ReX");
subplot(2,3,3); stem(MagX); title("MagX");
subplot(2,3,4); stem(Xrecons); title("X reconstructed");
subplot(2,3,5); stem(imX); title("ImX");
subplot(2,3,6); stem(PhaX); title("PhaseX");

Fig: verifying DFT and IDFT functions.


 Applications of DFT:

Task 1:
Implementing Convolution via Frequency Domain.
CODE:

n = 1:32;
h = 32;
X = sin(n);
Y = cos(n);
Z = conv(X,Y);
[reZ,imZ,ZusingFreqDom] = convFreqDomain(X,Y);

subplot(2,2,1); stem(Z); title('using conv()'); %xlim([0 35]);


subplot(2,2,2); stem(ZusingFreqDom); title('conv. via Frequency Domain');
subplot(2,2,3); stem(X); title('X');
subplot(2,2,4); stem(Y); title('Y');

function [reZ,imZ,Z] = convFreqDomain(X,Y)

[reX, imX] = myDFT(X);


MagX = (reX.^2 + imX.^2).^(1/2);
PhaX = atan2(imX,reX);

[reY, imY] = myDFT(Y);


MagY = (reY.^2 + imY.^2).^(1/2);
PhaY = atan2(imY,reY);

MagZ = MagX.*MagY;
PhaZ = PhaX + PhaY;
reZ = MagZ.*cos(PhaZ);
imZ = MagZ.*sin(PhaZ);
Z = myIDFT(reZ,imZ);

end
Fig : convolution via frequency domain. Error due to cyclic convolution.

Task 2:
Spectral Analysis of a given signal.

CODE:

[X,fs] = audioread('tuning_fork_A4.wav');
X = X(1:8192*4);
X= X';
[reX, imX] = myDFT(X);
MagX = (reX.^2 + imX.^2).^(1/2);
PhaX = atan2(imX,reX);

stem(MagX); title('MagX');xticks(0:500:2000);

This outputs a signal with spike at 512 Hz. Which means, the frequency
of the tuning fork was 512 Hz.
Fig : spectral analysis of tuning_fork.wav

_________________X__________________

You might also like