0% found this document useful (0 votes)
18 views

18BEC015_Exp4

This document details an experiment on Digital Signal Processing focusing on estimating signal spectra using DFT and FFT. It includes MATLAB code for computing and plotting DFT and IDFT, timing comparisons between user-defined and built-in functions, and observations on spectral leakage and reconstruction of DTFT. The conclusion emphasizes the efficiency of built-in functions over user-defined implementations in terms of computation time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

18BEC015_Exp4

This document details an experiment on Digital Signal Processing focusing on estimating signal spectra using DFT and FFT. It includes MATLAB code for computing and plotting DFT and IDFT, timing comparisons between user-defined and built-in functions, and observations on spectral leakage and reconstruction of DTFT. The conclusion emphasizes the efficiency of built-in functions over user-defined implementations in terms of computation time.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

2EC502 Digital Signal Processing

Experiment-4

Aim: Estimate signal spectra using Discrete Fourier Transform (DFT)


and Fast Fourier Transform (FFT).
Submitted by: 18BEC015 – Vishwam Bhavsar

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:

Elapsed time is 0.275666 seconds. → dftuser()


Elapsed time is 0.280894 seconds. → idftuser()
Elapsed time is 0.000327 seconds. → fft()
Elapsed time is 0.000265 seconds. → ifft()
Results:

using DFT IDFT


600

400

200

0
0 200 400 600 800 1000 1200

1.5

0.5

0
0 200 400 600 800 1000 1200

using FFT IFFT


600

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.

c. A continuous-time signal x(t) = sin(20πt) is sampled at 64 Hz to obtain x[n]. If we


denote X[k] as a 32-point DFT of x[n], then identify the peak amplitude and their
respective indices k in MATLAB. Repeat the problem for a signal with analog
frequency of 11 Hz.

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.

For implementation purpose, calculate 32-point DFT of sin(20πt) (sampled at 64 Hz)


and show it’s DTFT spectrum having ω with 64, 128, 256 and 512 samples

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)

L1 = [128 256 512];


for p=1:length(L1)
subplot (5,1,p+2) % function of above code for comparison
[w, X_dtft] = dft2dtftuser(X,L1(p),N);
plot(w,abs(X_dtft));
str = [num2str(L1(p)),'-pt DTFT'];title(str)
end

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

⇒ Simple linear convolution between input and impulse response

⇒ 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

x = sin(2*pi*50*t) + sin(2*pi*100*t) + sin(2*pi*200*t);

num = [0.692 -1.762 3.545 -3.925 3.545 -1.762 0.692];


den = [1 -2.238 3.921 -3.844 3.075 -1.367 0.478];
h=impz(num,den);

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:

Elapsed time is 0.011879 seconds. → conv()

Elapsed time is 0.044952 seconds. → cconv()

Elapsed time is 0.001389 seconds. → fft() ifft()

Observation:

Here, the linear convolution is calculated using 1) inbuilt convolution function, 2)


circular convolution having N+M-1 samples and 3) using fast fourier transforms. The
result is given above, and it was observed that fast fourier transform implementation
is the most efficient as it takes the least computation time followed by direct
convolution method using dedicated inbuilt function and then by circular concolution
which is the least efficient among all.

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 =

10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -


2.0000 - 2.0000i

Elapsed time is 0.201821 seconds.


y1 =

10.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -


2.0000 - 2.0000i

Elapsed time is 0.053039 seconds.

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.

You might also like