0% found this document useful (0 votes)
27 views9 pages

DSP Exp 7

This document describes the implementation of the fast Fourier transform (FFT) algorithm. It begins with an introduction to shifting the frequency range using the discrete-time Fourier transform (DTFT). It then describes implementing the divide-and-conquer discrete Fourier transform (DFT) and the recursive divide-and-conquer FFT. MATLAB code is provided for functions to calculate the DFT, FFT2, FFT4, FFT8, and the overall FFT using recursive stages. Plots are shown comparing the outputs of these FFT functions to verify their correctness.

Uploaded by

eishasingh369
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)
27 views9 pages

DSP Exp 7

This document describes the implementation of the fast Fourier transform (FFT) algorithm. It begins with an introduction to shifting the frequency range using the discrete-time Fourier transform (DTFT). It then describes implementing the divide-and-conquer discrete Fourier transform (DFT) and the recursive divide-and-conquer FFT. MATLAB code is provided for functions to calculate the DFT, FFT2, FFT4, FFT8, and the overall FFT using recursive stages. Plots are shown comparing the outputs of these FFT functions to verify their correctness.

Uploaded by

eishasingh369
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/ 9

Digital Signal Processing

Laboratory (EE39203)
Experiment 7
Fast Fourier Transform

Eisha Singh
Roll no. 21IE10015
Indian Institute of Technology, Kharagpur

October 11, 2023

1 Introduction

1.1 Shifting the Frequency Range

1.1.1 DFT of hamming(20)

Figure 1: Magnitude of DFT for hamming(20)

1
1.1.2 MATLAB Code for DTFTsamples

MATLAB code for DTFTsamples function:

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

1.1.3 Plot of the magnitude of DTFT samples vs. frequency

Figure 2: Magnitude of X2 0(k) vs the frequency in radians per sample for hamming(20)

1.1.4 Complete MATLAB Code for the plots

1 %%%%%%%%%%%%%%%%%%%%%% Plotting the DFT of Hamming Window %%%%%%%%%%%%%%%%%%%%%%


2 x = hamming(20);
3 X = DFTsum(x);
4 figure();
5 subplot(1,1,1);
6 stem(0:19, abs(X));
7 title('20 point DFT of Hamming window for N = 20');
8 xlabel('k');
9 ylabel(' | X(k) | ');
10 sgtitle('Eisha Singh 21IE10015');
11
12 %%%%%%%%%%%%%%%%%%%%%% Plotting DTFTsamples for N = 20 %%%%%%%%%%%%%%%%%%%%
13 [X,w] = DTFTsamples(x);
14 figure;
15 subplot(1,1,1);
16 stem(w,abs(X));
17
18 title('Magnitude of DTFT samples for N = 20');
19 xlabel('\omega');
20 ylabel(' | X(\omega) | ');
21 sgtitle('Eisha Singh 21IE10015');
22

23 %% DFTsum function to calculate DFT


24 function X = DFTsum(x)
25 N = length(x);
26 X = zeros(1,N);
27 for i = 1:N
28 for k = 1:N
29 X(i) = X(i) + x(k)*exp(−sqrt(−1)*2*pi*(i−1)*(k−1)/N);
30 end
31 end
32 end
33
34 %% DTFTsamples function
35 function [X,w] = DTFTsamples(x)
36 N = length(x);
37 k = 0:(N−1);
38 w = 2*pi*k/N;
39 w(w≥pi) = w(w≥pi)−2*pi;
40 X = DFTsum(x);
41 X = fftshift(X);
42 w = fftshift(w);
43 end

1.2 Zero Padding

1.2.1 Plots of the DTFT samples for N = 50 and N = 200

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

2.1 Implementation of Divide-and-Conquer DFT

2.1.1 MATLAB Code for dcDFT function

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.

2.2 Recursive Divide and Conquer

2.2.1 MATLAB code for FFT2

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

2.2.2 MATLAB code for FFT4

MATLAB code:

1 %% FFT function for N = 4


2 function X = FFT4(x)
3 N = 4;
4 X = zeros(1,N);
5 x e = x(1:2:N);
6 x o = x(2:2:N);
7 k = 0:1:(N/2−1);
8 w = exp(−sqrt(−1)*2*pi*k/N);
9 X e = FFT2(x e);
10 X o = FFT2(x o);
11 for i = 1:(N/2)
12 X(i) = X e(i) + w(i)* X o(i);
13 X(i+N/2) = X e(i) − w(i)* X o(i);
14 end
15 end

2.2.3 MATLAB code for FFT8

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.

2.2.4 Output of FFT8 for x[n] = 1 for N = 8

Figure 6: Output of FFT8 for x[n] = 1 for N = 8

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.

2.2.5 MATLAB Code for fftstage function

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.

2.2.6 Complete MATLAB Code

MATLAB code:

1 %% Calculating FFT8 for x = 1


2 N = 8;
3 x 1 = zeros(1,N);
4 for i = 1:N
5 x 1(i) = 1;
6 end
7

8 %% Using the FFT8 function


9 disp('Using FFT8 function: ');
10 X 1 = FFT8(x 1);
11 disp(abs(X 1));
12 %% Using the fft stage function
13 disp('Using the fft stage function: ');
14 X 1 fft = fftstage(x 1);
15 disp(abs(X 1 fft));
16
17 n = 0:7;
18 x 1 = zeros(1,8);
19 x 1(1) = 1;
20 X 1 = FFT8(x 1);
21
22 %% Plotting the FFT8
23 figure();
24 subplot(3,2,1);
25 stem(n, abs(X 1));
26 xlabel('k');
27 ylabel(' | FFT8 | ');
28 title('x[n]=\ [n] N = 8');
29 sgtitle('Eisha Singh 21IE10015');
30

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

97 %% FFT function for N = 8


98 function X = FFT8(x)
99 N = 8;
100 X = zeros(1,N);
101 x e = x(1:2:N);
102 x o = x(2:2:N);
103 k = 0:1:(N/2−1);
104 w = exp(−sqrt(−1)*2*pi*k/N);
105 X e = FFT4(x e);
106 X o = FFT4(x o);
107
108 for i = 1:(N/2)
109 X(i) = X e(i) + w(i)* X o(i);
110 X(i+N/2) = X e(i) − w(i)* X o(i);
111 end
112 end
113
114 %% Recursive function to calculate FFT using recursion
115 function [X] = fftstage(x)
116 N = length(x);
117 if N == 2
118 X(1) = x(1) + x(2);
119 X(2) = x(1) − x(2);
120 return;
121 end
122 if N>2
123 x e = x(1:2:N);
124 x o = x(2:2:N);
125 X o half = fftstage(x o);
126 X e half = fftstage(x e);
127 k = 0:1:(N/2−1);
128 w = exp(−sqrt(−1)*2*pi*k/N);
129 for k = 1:(N/2)
130 X(k) = X e half(k) + w(k)* X o half(k);
131 X(k+N/2) = X e half(k) − w(k)* X o half(k);
132 end
133 end
134 end

You might also like