DSP Exp 7
DSP Exp 7
Laboratory (EE39203)
Experiment 7
Fast Fourier Transform
Eisha Singh
Roll no. 21IE10015
Indian Institute of Technology, Kharagpur
1 Introduction
1
1.1.2 MATLAB Code for DTFTsamples
1 %% DTFTsamples function
2 function [X,w] = DTFTsamples(x)
3 N = length(x);
4 k = 0:(N−1);
5 w = 2*pi*k/N;
6 w(w≥pi) = w(w≥pi)−2*pi;
7 X = DFTsum(x);
8 X = fftshift(X);
9 w = fftshift(w);
10 end
Figure 2: Magnitude of X2 0(k) vs the frequency in radians per sample for hamming(20)
The plot of DTFT samples for N = 50 and N = 200 are shown in Figure 3. The original DTFT of
x[n] is also plotted for comparison.
Figure 3: Magnitude of the absolute values of the DTFT samples vs the frequency in radians per
sample for N = 50 and N = 200
The plot of the absolute value of the DTFT samples for N = 200 is more like the original DTFT
of x[n]. The difference in plots is due to the different sampling frequencies. There are more sample
points for the DFT/FFT for N = 200 and more samples will produce a finer result of the original
signal.
2 The Fast Fourier Transform Algorithm
MATLAB code:
1 function X = dcDFT(x)
2 N = length(x);
3 X = zeros(1,N);
4 x o = x(2:2:N);
5 x e = x(1:2:N);
6 X o = DFTsum(x o);
7 X e = DFTsum(x e);
8 k = 0:1:(N/2−1);
9 W = exp(−sqrt(−1)*2*pi*(k)/N);
10 n = length(X o);
11 for i = 1:n
12 X(i) = X e(i) + X o(i)*W(i);
13 X(i+N/2) = X e(i) − X o(i)*W(i);
14 end
15 end
The DFT sum requires N multiplies. So these sums would return ( N2 )2 each. Then there are N
multiplies for combining both functions. Therefore the total number of multiplies would be
N 2
2( ) +N
2
2.1.2 Testing out the dcDFT function for the given sequences
The plots of dcDFT for the given sequences are same as the plots of magnitude of DFT obtained
in Experiment 6. Thus, the dcDFT() function works.
MATLAB code:
1 function X = FFT2(x)
2 X = zeros(1,2);
3 X(1) = x(1) + x(2);
4 X(2) = x(1) − x(2);
5 end
Figure 4: Testing out the dcDFT function for the given sequences
MATLAB code:
MATLAB code:
1 function X = FFT8(x)
2 N = 8;
3 X = zeros(1,N);
4 x e = x(1:2:N);
5 x o = x(2:2:N);
6 k = 0:1:(N/2−1);
7 w = exp(−sqrt(−1)*2*pi*k/N);
8 X e = FFT4(x e);
9 X o = FFT4(x o);
10 for i = 1:(N/2)
11 X(i) = X e(i) + w(i)* X o(i);
12 X(i+N/2) = X e(i) − w(i)* X o(i);
13 end
14 end
Figure 5: Testing out the FFT8 function for the given sequences
The plots of FFT8 for the given sequences are similar to the plots of magnitude of DFT obtained
in previous part. Only the magnitude is different since value of N is changed from 8 to 10.
The total number of multiplies by twiddle factors required for 8-point FFT using FFT8 function is
8. In FFT4, there will be 4 multiplications for every call and FFT4 is called twice so 4 multiplica-
tions with twiddle factor here. Then FFT8 is called once, and for each call 4 multiplications take
place. Hence, total 8 multiplications with twiddle factor take place.
For an input sequence of size N, the number of multiplications required are approximately N*log2(N).
For example, if the input sequence has a size of 1024 i.e. p = 10, then the number of multiplications
required by the FFT algorithm is approximately 1024 * log2(1024) = 10240 while for direct com-
putation, it requires N 2 i.e (210 )2 multiplications as discussed in the previous experiment. Hence,
the number of multiplications required are 1048576 through direct computation. This is a stark
difference and the FFT is obviously superior.
MATLAB Code:
1 %% Recursive function to calculate FFT using recursion
2 function [X] = fftstage(x)
3 N = length(x);
4 if N == 2
5 X(1) = x(1) + x(2);
6 X(2) = x(1) − x(2);
7 return;
8 end
9 if N>2
10 x e = x(1:2:N);
11 x o = x(2:2:N);
12 X o half = fftstage(x o);
13 X e half = fftstage(x e);
14 k = 0:1:(N/2−1);
15 w = exp(−sqrt(−1)*2*pi*k/N);
16 for k = 1:(N/2)
17 X(k) = X e half(k) + w(k)* X o half(k);
18 X(k+N/2) = X e half(k) − w(k)* X o half(k);
19 end
20 end
21 end
Figure 7: Testing out the fftstage function for the given sequences
As we can see, the graphs obtained using the FFT8 function and fftstage function are the same.
Thus, the fftstage() function can be used.
MATLAB code:
31 subplot(3,2,2)
32 stem(n, abs(fftstage(x 1)));
33 xlabel('k');
34 ylabel(' | fft stage | ');
35 title('x[n]=\ [n] N = 8');
36 sgtitle('Shelly Aggarwal 21IE10049');
37
38 x 2 = zeros(1,8);
39 for i = 1:8
40 x 2(i) = 1;
41 end
42 X 2 = FFT8(x 2);
43 subplot(3,2,3);
44 stem(n,abs(X 2));
45 xlabel('k');
46 ylabel(' | FFT8 | ');
47 title('x[n]=1 N = 8');
48 sgtitle('Shelly Aggarwal 21IE10049');
49
50 subplot(3,2,4);
51 stem(n,abs(fftstage(x 2)));
52 xlabel('k');
53 ylabel(' | fft stage | ');
54 title('x[n]=1 N = 8');
55 sgtitle('Shelly Aggarwal 21IE10049');
56
57 x 3 = exp(sqrt(−1)*2*pi*n/8);
58 X 3 = FFT8(x 3);
59 subplot(3,2,5);
60 stem(n,abs(X 3));
61
62 xlabel('k');
63 ylabel(' | FFT8 | ');
64 title('x[n]= e {j2\pin/8} N = 8');
65 sgtitle('Shelly Aggarwal 21IE10049');
66
67 subplot(3,2,6);
68 stem(n,abs(fftstage(x 3)));
69 xlabel('k');
70 ylabel(' | fft stage | ');
71 title('x[n]= e {j2\pin/8} N = 8');
72 sgtitle('Eisha Singh 21IE10015');
73
74 %% FFT function for N = 2
75 function X = FFT2(x)
76 X = zeros(1,2);
77 X(1) = x(1) + x(2);
78 X(2) = x(1) − x(2);
79 end
80
81 %% FFT function for N = 4
82 function X = FFT4(x)
83 N = 4;
84 X = zeros(1,N);
85 x e = x(1:2:N);
86 x o = x(2:2:N);
87 k = 0:1:(N/2−1);
88 w = exp(−sqrt(−1)*2*pi*k/N);
89 X e = FFT2(x e);
90 X o = FFT2(x o);
91 for i = 1:(N/2)
92 X(i) = X e(i) + w(i)* X o(i);
93 X(i+N/2) = X e(i) − w(i)* X o(i);
94 end
95 end
96