Understanding Discrete Fourier Transform (DFT) and Fast Fourier Transform (FFT) PDF
Understanding Discrete Fourier Transform (DFT) and Fast Fourier Transform (FFT) PDF
Understanding Discrete Fourier Transform (DFT) and Fast Fourier Transform (FFT) PDF
Content
1 Introduction ..................................................................................................................................... 2
2 The Discrete Fourier Transform (DFT) .............................................................................................. 2
2.1. Defining the input signal .............................................................................................................. 2
2.2. Simulating the DFT equation ....................................................................................................... 2
2.3. Understanding the n, m and N terms in DFT equation. ................................................................... 3
2.1. Sampling ................................................................................................................................... 4
2.2. Computing the DFT for m =1 ..................................................................................................... 5
2.1. Computing the DFT for m =0 ..................................................................................................... 8
3 The Fast Fourier Transform (FFT)..................................................................................................... 9
3.1. Simulating the FFT equation..................................................................................................... 9
3.2. Computing the FFT equation for m =1.................................................................................... 10
4 Experimental results ....................................................................................................................... 15
5 Final considerations ........................................................................................................................ 15
6 References..................................................................................................................................... 16
1 Introduction
The DFT and FFT are used mainly to obtain the amplitude of a harmonic component from a periodic signal
containing several harmonics. For instance, it is desired to know the 180 Hz harmonic amplitude from a signal.
Then, the DFT and FFT are the best choices to do that.
This report presents how to implement DFT and FFT by means of simplified blocks, which are sine, cosine, sum,
square, etc. It is easy to find on the internet the DFT and FFT performed in C-code. Actually, the DFT and FFT
are almost always performed in C-code mainly because its modularity. However, DFT and FFT in C-code are
exhaustive for those who want to learn how the DFT and FFT work. Here, the objective is to present a step-by-
step way to implement DFT and FFT. An 8-point DFT and FFT is presented using simplified blocks.
The Fig. 1 presents how the input signal is simulated. In this case, A1 = 1.125 and A2 = 0.52.
V Vin
V1 V2
1k 2k
1.125 135 0.52
V1 V2 Vin
2
1.5
0.5
1
1
0.5
0 0 0
-0.5
-1
-1
-0.5
-1.5
-2
0.006 0.0062 0.0064 0.0066 0.0068 0.007 0.0072 0.0074 0.0076 0.0078 0.008 0.006 0.0062 0.0064 0.0066 0.0068 0.007 0.0072 0.0 074 0.0076 0.0078 0.008 0.006 0.0065 0.007 0.0075 0.008 0.0085 0.009
Time (s) Time (s) Time (s)
Let’s simulate the DFT equation, which is given by (2). Don’t care about what are the terms n, m and N now.
N 1
X (m) x(n)e j 2 nm/ N (2)
n 0
Let’s see how to simulate it, but first equation (2) will be rewritten in a better visualized way. The Euler’s
relationship is given by (3).
The j term is still embarrassing us. So, we will split equation (4) into two parts, a real and an imaginary, but both
with real values. The real part is given by (5).
N 1
X real (m) x (n) cos(2 nm / N ) (5)
n 0
We clearly see that equations (5) and (6) don’t have neither exponential (e) nor (j) terms. Both of them are real
values. We will simulate these equations soon.
N is the number of points the DFT will be performed. In this example, it will be 8 points.
N 8 (7)
Therefore, we need 8 samples from our input signal. The sampling frequency is 8 kHz. The value n goes from 0 to
7.
n 0
n 1
n 2
n 3
(8)
n 4
n 5
n 6
n 7
x(n) in equation (4) is the sampled value. Let’s see how to do that.
We are interesting in obtaining the amplitude of the 1 kHz and 2 kHz components. Therefore, the values m goes
from 1 to 2.
m 1
(9)
m 2
n 0
n 1
n 2
n 3
m 1
n 4
n 5
n 6
n 7
n 0
n 1
n 2
n 3
m2
n 4
n 5
n 6 (10)
n 7
2.1. Sampling
Fig. 2 presents the input signal again and the 8-point sampled signal. We clearly see how bad a low sampling
frequency is.
Vin_sampled Vin
2
-1
-2
0.004 0.006
Time (s)
In order to compute equation (5) and (6) we need 8 values. Fig. 3 shows how to obtain 8 values in order to
compute the DFT.
V Vin_sampled
fs = 8 kHz
V x0
fs
Comes from ZOH x0
the input signal V x1
fs
1 x1
z
V x2
unit 1 fs x2
z
delay
fs V x3
1 x3
z
fs
V x4
1 x4
z
fs V x5
1 x5
z
V x6
fs
1 x6
z
fs V x7
1 x7
z
We have 8 samples x(0), x(1), x(2), x(3), x(4), x(5), x(6) and x(7).
X real (1) x(0) cos(2 0 1/ 8) x(1) cos(2 11/ 8) x (2) cos(2 2 1/ 8) (11)
x(3) cos(2 3 1/ 8) x(4) cos(2 4 1/ 8) x(5) cos(2 5 1/ 8)
x(6) cos(2 6 1/ 8) x (7) cos(2 7 1/ 8)
X im g (1) x(0) (1)sin(2 0 1/ 8) x(1) (1)sin(2 1 1/ 8) x(2) (1)sin(2 2 1/ 8) (12)
x(3) (1)sin(2 3 1/ 8) x (4) (1)sin(2 4 1/ 8) x(5) (1)sin(2 5 1/ 8)
x(6) (1)sin(2 6 1/ 8) x(7) (1)sin(2 7 1/ 8)
Equations (11) and (12) are simulated according to the Fig. 4, which are also the equations (5) and (6).
(2*3.14159265)/8
V
V X1_0_real V X1_real X1_img
r
0 cos
x0
V
X1_0_img
r
sin
x0
V X1_1_real
r
1 cos
x1
V
X1_1_img
r
sin
x1
V
X1_2_real
r
2 cos
x2
V
X1_2_img
r
sin
x2
V
X1_3_real
r
3 cos
x3
V
X1_3_img
r
sin
x3
V X1_4_real
r
4 cos
x4
V
X1_4_img
r
sin
x4
V X1_5_real
r
5 cos
x5
V
X1_5_img
r
sin
x5
V
X1_6_real
r
6 cos
x6
V
X1_6_img
r
sin
x6
V
X1_7_real
r
7 cos
x7
V
X1_7_img
r
sin
x7
Now that we have the real and imaginary parts, the amplitude of 1 kHz components is given by (13).
Fig. 5 presents how to simulate equations (13) and (14). The factor ¼ is inherent of the DFT, which is equal to
N/2.
V X1_mag
2 1/4
Comes from X1_real xa K
2
Comes from X1_img xa
V X1_phase
180/3.1415
y
K -1
tg
180/3.1415 x
K
Fig. 6 presents the magnitude of the 1 kHz component calculated by means of the DFT. Its value is constant and
very close to 1.125.
And
Therefore, in order to compute the DC value of an input signal, it is necessary just sum all the samples.
X 0 magnitude x(0) x(1) x(2) x (3) x (4) x(5) x(5) x (7) (17)
x1
x2
V X0_mag
x3
1/8
x4 K
x5
x6
x7
In our input signal the DC value is zero. But in order to verify the DFT efficacy, let’s add 0.32V into the 1 kHz
signal. Fig. 8 shows the new input signal and the DFT result for X(0). On the left is the new input signal and the
average value of it, calculated by the simulator. On the right is the DFT result for X(0). Exactly the same.
Fig. 8: New input signal and the DFT result for X(0).
Solving sines and cosines may be a burden for microcontrollers. As verified, the 8-point DFT makes use 8 sines
and 8 cosines. Now imagine a 1024-point DFT. The Fast Fourier Transform appears as a solution to this
inconvenience. The FFT is the same of DFT, but in a very simplified way. It is important to highlight that the FFT
is the same thing of DFT, but performed in easiest way. The results are the same.
N 2 1 N 2 1
nm m nm
X (m ) n 0
x(2n)W N /2
W N
n 0
x(2n 1)W N /2 (18)
Don’t scare about it. Let’s see part-by-part. Rewritten with distinguishing.
N 2 1 N 2 1
nm m nm
X (m ) x(2n)W N /2 W N x (2n 1)W N /2
n 0
(19)
constant
n 0
for even values of n for odd values of n
The constant term is given by (20). The y and z are variable just to illustrate where to substitute them in the
constant equation (20).
y
W z
e 2 j y /z (20)
This constant is complex. So, we need to separate into two parts, real and imaginary, as follows (applying the
Euler’s relationship).
y 2 y
W z_( real )
cos (21)
z
y 2 y
W z_(img)
sin (22)
z
Similar to the DFT methodology, for each interest m, the equation (19) must compute all values of n, from 0 to 7.
Where
N 2 1
nm
A(m) n 0
x (2n)W N /2 (24)
and
N 2 1
nm
B ( m) n 0
x(2n 1)W N/2
(25)
From equations (26), (27) and (28), the following constant must be computed, for real and imaginary parts.
1
W 8
0
W 4
1
W 4 (29)
2
W 4
3
W 4
The results are the following:
1 2 1
W 8 _ real
cos 0.707 (30)
8
1 2 1
W 8 _img
sin 0.707 (31)
8
0 2 0
W 4 _ real
cos 1 (32)
4
0 2 0
W 4 _img
sin 0 (33)
4
1 2 1
W 4 _ real
cos 0 (34)
4
1 2 1
W 4 _img
sin 1 (35)
4
2 2 2
W 4 _ real
cos 1 (36)
4
2 2 2
W 4 _img
sin 0 (37)
4
3 2 3
W 4 _ real
cos 0 (38)
4
3 2 3
W 4 _img
sin 1 (39)
4
0
real W4_1_real
W4_1
-1
img W4_1_img
-1
real W4_2_real
W4_2
0
img W4_2_img
0
real W4_3_real
W4_3
-1
img W4_3_img
1
real W8_0_real
W8_0
0
img W8_0_img
Fig. 10 presents how A(1) and B(1) both for real and imaginary parts are simulated.
x0 Vb_X0
x4
Vb_X4
-1
K Vb_X4 A1_real
A1_img
Vb_X6
x2 Vb_X2
W4_1_real
x6
W4_1_img
Vb_X6
-1
K
V
Vb_X5 B1_real
x1 Vb_X1 B1_img
Vb_X7
x5
W4_1_real
Vb_X5
W4_1_img
-1
K
x3 Vb_X3
x7
Vb_X7
-1
K
Fig. 10: How A(1) and B(1) both for real and imaginary parts are simulated.
Now we are ready to compute equation (23), rewrote here in (44) for simplicity.
m
X (m) A(m) W N B(m) (44)
m m
X (m)img A(m)img W N _ realB(m)img W N _imgB(m) real (47)
W8_1_real xa
B1_real
W8_1_img
B1_img
A1_img
W8_1_real
B1_img
W8_1_img
B1_real
Fig. 12 presents the X(1) magnitude. It is the same 1.125 found in the input signal.
Fig. 13 presents the sine waves with 1kH and 2 kHz, as well as, the input signal. The frequencies and RMS
values are also presented. The staircase visualization is due to the DAC used. The DAC is used in order to see a
digital variable in the oscilloscope and it does not influence on the FFT analysis.
Fig. 13: The sine waves with 1kH and 2 kHz, as well as, the input signal.
Fig. 14 presents the magnitude of 1 kHz component from the input signal calculated by the FFT. Its average value
is 654 mV, the same of the input signal 1 kHz component. (the value was divided by srqt(2) because the FFT
gives the amplitude). The non-constant behavior is mainly due to the DAC.
Fig. 14: The magnitude of 1 kHz component from the input signal calculated by the FFT
5 Final considerations
This report presented a simplified way to perform DFT and FFT. The objective was make the readers understand
how the DFT and FFT work. Computing the DFT and FFT using 8 point gives poor results. The recommended is
512 or more points. Here, the phenomena like Leakage, Windows, Scalloping Loss, Zero Padding, etc were not
took account. Reference [1] is one of the best books about Digital Signal Processing and which this report is
based on.
6 References
[1] Understanding Digital Signal Processing (3rd Edition). Richard G. Lyons (Author)