18BEC015_Exp4
18BEC015_Exp4
Experiment-4
Laboratory Exercise
a. Write a MATLAB program to compute and plot the L-point DFT X[k] of a sequence
x[n] of length N with L ≥ N and then to compute and plot the L-point IDFT of X[k].
Save this code as user defined functions named as dftuser() and idftuser() for DFT and
IDFT, respectively. An L-point DFT of x can be calculated through, [X,k] =
dftuser(x,L); Likewise, the L-point IDFT of X will be, [x,n] = idftuser(X,N);
MATLAB Code:
1. DFT
function [X,k] = dftuser(x,L)
N = length(x);
X = zeros(1,L);
for k = 1:L
for n= 1:N
X(k) = X(k)+x(n)*exp(-1i*(2*pi/L)*(k-1)*(n-1));
end
end
k = 0:L-1;
end
2. IDFT
function [x,n] = idftuser(X,N)
L = length(X);
x = zeros(1,N);
for n = 1:N
for k= 1:L
x(n) = x(n)+X(k)*exp(1i*(2*pi/L)*(k-1)*(n-1));
end
end
x = x/L;
n = 0:N-1;
end
Results:
clc;clear all;close all;
f = 512;
fs = 4096;
N = 64;
n = 0:1/fs:(N-1)/fs;
x = sin(2*pi*f*n);
L = 64;
[X,k] = dftuser(x,L);
subplot 211
stem(abs(X));title('Using dftuser')
xlabel('K');ylabel('X(k)')
subplot 212
y = fft(x);
stem(abs(y));title('Using fft')
xlabel('K');ylabel('X(k)')
Using dftuser
40
30
X(k)
20
10
0
0 10 20 30 40 50 60 70
K
Using fft
40
30
X(k)
20
10
0
0 10 20 30 40 50 60 70
K
Observation:
As seen by the above plot of the DFT sequence, which is generated using the user-
defined function dftuser, results into the exact similar results as by using the inbuilt
function fft which is implemented using fast fourier transform algorithm but this
dftuser and idftuser is directly implementing the DFT and IDFT equation.
b. This exercise aimed to identify how fast fft() is compared to regular DFT. For that
reason, create a sinusoid that has the three spectral components of 50, 100 and 250
Hz, and sample this signal at 1000 Hz. Determine the duration of the signal so that
you end up with 1024 points. You will test various implementations of DFT (dftuser()
vs. fft()) on this three frequency test signal. Using tic and toc, identify which function
will use to optimum time to compute.
MATLAB Code:
clc;clear all;close all;
Fs=1e3;
N=1024;
t=0:1/Fs:(N-1)/Fs; %to get 1024 samples
L=length(t);
x=sin(2*pi*50*t)+sin(2*pi*100*t)+sin(2*pi*250*t);
figure(1);
subplot(211)
tic
[y1,k1] = dftuser(x,L); %using user defined functions
toc
stem(k1,abs(y1));
title('using DFT IDFT')
subplot(212)
tic
[y2,n1] = idftuser(y1,N);
toc
stem(n1,abs(y2));
figure(2);
subplot(211)
tic
y = fft(x); %using inbuilt functions
toc
stem(abs(y));
title('using FFT IFFT')
subplot(212)
tic
x = ifft(y);
toc
stem(abs(x));
Timing Results:
400
200
0
0 200 400 600 800 1000 1200
1.5
0.5
0
0 200 400 600 800 1000 1200
400
200
0
0 200 400 600 800 1000 1200
1.5
0.5
0
0 200 400 600 800 1000 1200
Observation:
Here, the sinusoid having multiple frequency components in it was created and then
its DFT was taken using dtftuser() and fft() function, Afterwards its IDFT was also
taken using idftuser() and ifft() function. Now as seen, the results are identical, but
using the tic toc function the time for computation was determined and the it was
observed that the fft() and ifft() inbuilt functions are way more efficient as compared
to user-defined functions because they implement the fft algorithm for the computation
of the DFT samples whereas the user-defined functions simply implement the equation.
MATLAB Code:
clc;clear all;close all;
Fs = 64; L = 32;
k = 0:L-1;
t = 0:1/Fs:(L-1)/Fs; %to have 32 samples
x1 = sin(20*pi*t);
X = fft(x1,L);
subplot 211
stem(k, abs(X));title('F=10Hz')
x2 = sin(2*11*pi*t);
Y = fft(x2,L);
subplot 212
stem(k, abs(Y));
title('F=11Hz')
Results:
Observation:
In the first subplot, the analog freq F = 10 Hz, the value of r = 5 at which the peak
occurs between 0 to L/2 (which can also be found out by formula k = F*L/Fs =
10*32/64 = 5) since the L/2 to L samples are mirror image of it, so the next peak will
be at L-r = 32-5 = 27.
For the second subplot, the analog freq F = 11 Hz, so when evaluating the value of
kth index of peak, the value doesn’t come out to be an integer, viz k – F*32/64 =
11*32/64 = 5.5. Now for this, we will get the peak between 5 and 6 and for the L/2
to L part its will be between 32-5=27 and 32-6=26. Moreover, the value of k takes
integer values of 0 to 31 (L-1), so the F takes even values (0,2,4,6 etc) so the Resolution:
2 and for odd values Spectrum leakage occurs which is shown by the spreading in the
second plot which indicates presence of other frequency components.
d. This exercise illustrates the reconstruction of DTFT via DFT using appropriately
chosen interpolation function. A DTFT requires ∞ samples of ω for representation. In
contrast, DFT only uses N points for representation. Since DFT is a sampled version
of DTFT, actual spectrum is obtained through DTFT only.
MATLAB Code:
clc;clear all;close all;
Fs = 64;
L = 64; %no of samp in DTFT
N = 32; %32 pt DFT
t = 0:1/Fs:(N-1)/Fs; %to have 32 samples
x = sin(20*pi*t);
[X,k1] = dftuser(x,N);
w = 0:2*pi/L:2*pi;
X_dtft = zeros(1,length(w));
for i=1:length(w)
for k=0:N-1
for n= 0:N-1
X_dtft(i)=(X_dtft(i)+X(k+1)*exp(-1i*(w(i)-
((2*pi*k)/N))*n));
end
end
end
X_dtft=X_dtft/N;
subplot 511
stem(k1,abs(X));
title('32-pt DFT')
subplot 512
plot(w,abs(X_dtft));
str = [num2str(L),'-pt DTFT'];title(str)
Results:
Observation:
The 32-pt DFT shown in the first subplot, by taking appropriate value of the shifted
ଶగ
version of digital sinc function viz, ∅ ቀ ݓ− ே
ቁ corresponding to the respective index
k of X[k] and each of the shifted value weighted by X[k] we get the DTFT spectrum
from the DFT X[k]. The DTFT is continuous means it contains infinite samples of w
for its representation. Here finite value of w is taken and the DTFT spectrum is
computed which is shown in the above subplots for various values of w, it was observed
that as the values of w increases it converges to actual DTFT as the samples in the
spectrum increases which leads to lesser discontinuity in the DTFT spectrum and for
higher values of w like 256, 512 it is almost continuous and smoothened as compared
to w = 64 which has abrupt changes and has more spikes.
e. This exercise illustrates methods to compute linear convolution along with its timing
requirements. Create a sinusoid that has the three spectral components of 50, 100 and
200 Hz, and sample this signal at 1000 Hz. Determine the duration of the signal so
that you end up with 1024 points. The LTI system is described with the difference
equation same as Experiment 3, equation (3). Determine the convoluted output using
⇒ Circular convolution
⇒ Discrete Fourier Transform (DFT) and Inverse Discrete Fourier Transform (IDFT)
Also, compare the timing requirement for each of the above-mentioned methods and
conclude the best method
MATLAB Code:
clc;clear all;close all;
Fs = 1e3;
L = 1024;
t = 0:1/Fs:(L-1)/Fs; %to have 1024 samples
N = length(t);
M = length(h);
len = N+M-1;
tic
y=conv(x, h);
toc
tic
y1 = cconv(x, h, len);
toc
tic
x_k = fft(x, len);
h_k = fft(h, len);
y3 = ifft(x_k.*h_k', len);
toc
Results:
Observation:
f. Implement the radix-2 FFT algorithm on your own. Demonstrate that your
algorithm works by comparing to the fft() command in MATLAB. Your
implementation may not (and probably will not) be as efficient as that of
MATLAB’s. However, if you can get close, you have done well
MATLAB Code:
function [y]=fftuser(x)
n = length(x);
switch n
case(2)
y=zeros(1,2);
y(1)=x(1)+x(2);
y(2)=x(1)-x(2);
otherwise
tem1=zeros(1,length(x)/2);
tem2=zeros(1,length(x)/2);
ev=1;od=1;
for i=1:length(x)
if(rem(i,2))
tem2(od)=x(i);
od=od+1;
else
tem1(ev)=x(i);
ev=ev+1;
end
end
rec1=fftuser(tem1);
rec2=fftuser(tem2);
y=zeros(1,length(x));
count=1;
for i=1:length(rec1)
y(count)=rec1(i)+(exp(-1i*2*pi*(i-
1)/length(x))*rec2(i));
count=count+1;
end
for i=1:length(rec1)
y(count)=rec1(i)-(exp(-1i*2*pi*(i-
1)/length(x))*rec2(i));
count=count+1;
end
end
end
Results:
clc;clear all;close all;
x = [1 2 3 4];
tic
y = fftuser(x)
toc
tic
y1 = fft(x)
toc
y =
Observation:
The user defined fft algorithm is not as fast as fft, but yields the similar result of DFT
samples which are being calculated by inbuilt fft() function. The fftuser() function
follows the fft algorithm of dividing the input sequence to even and odd index samples
and further diving uptill the least level where 2-pt DFT is calculated.
Conclusion:
From this experiment, I implemented the DFT and IDFT equations to make a user
defined function for calculating DFT and compared it with fft(). Moreover, the timing
analysis of the user defined functions vs the inbuilt functions was also done using tic
and toc, and it was concluded that the inbuilt functions were faster and took optimum
time to compute. Further, the concept of Spectral leakage was also observed for the
odd value of analog frequency which leads to spreading of the DFT spectrum.
Moreover, the DTFT spectrum was reconstructed using discrete DFT and it was
observed that as w samples increases it converges to actual DTFT spectrum. Further,
linear convolution was computed using various methods and the fft ifft method was
concluded to be the most efficient and at last user-defined function that implements
fft algorithm was made and compared with inbuilt fft function yielding the same result
but not as efficient.