Understanding Discrete Fourier Transform (DFT) and Fast Fourier Transform (FFT) PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Tiago Davi Curi Busarello October / 2014

Revised on July 2019

Understanding Discrete Fourier Transform (DFT) and Fast Fourier


Transform (FFT).
Simulation and Digital Implementation by means of blocks, not C-code

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 simulation used in this report is freely available on http://busarello.prof.ufsc.br/

2 The Discrete Fourier Transform (DFT)

2.1. Defining the input signal


The input signal we want to compute the DFT is given by (1).

xinput (t )  A1 sin(2 1000t )  A2 sin(2 2000t   ) (1)

Where A1 and A2 are the amplitude of each harmonic.

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)

Fig. 1: How the input signal is simulated.

2.2. Simulating the DFT equation


Supposing we have just access to the Vin singnal (Vin(t) = xinput(t)) and we want to know what are the amplitude
of the 1 kHz and 2 kHz components. This is what the DFT and FFT do.

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).

e  j  cos( )  j sin( ) (3)

Therefore, equation (2) can be written as (4).


N 1
X (m)   x(n)  cos(2 nm / N )  j sin(2 nm / N ) (4)
n 0

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

The imaginary part is given by (6).


N 1
X img (m)   x(n)  (1)sin(2 nm / N )  (6)
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.

2.3. Understanding the n, m and N terms in DFT equation.


Let’s see what are n, m and N.

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

For each m, the n values go from 0 to 7, according to the following chart.

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
m2
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)

Fig. 2: The input signal and the 8-point sampled signal.

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

Fig. 3: How to obtain 8 values in order to compute the DFT.

We have 8 samples x(0), x(1), x(2), x(3), x(4), x(5), x(6) and x(7).

2.2. Computing the DFT for m =1


Compute the DFT for m=1 means that we will calculate the amplitude values of the 1 kHz component which is
hidden in our input signal. We have m =1, N =8 and n going from 0 to 7. With these values, equation (5) can be
written as (11).
N 1
X real (m)   x(n)  cos(2 nm / N ) 
n 0
N 1
X real (1)   x(n)  cos(2 n1/ 8) 
n 0

X real (1)  x(0)  cos(2  0 1/ 8)  x(1)  cos(2 11/ 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)

Similarly, equation (6) can be written as


N 1
X im g (m)   x (n)  (1)sin(2 nm / N ) 
n 0
N 1
X im g (1)   x(n)  (1)sin(2 n1/ 8) 
n 0

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

Fig. 4: Simulation of equations (5) and (6).

Now that we have the real and imaginary parts, the amplitude of 1 kHz components is given by (13).

X 1magnitude  X real (1)2  X img (1)2 (13)

The phase of the same component is given by (14).


 X img (1) 
X 1 phase  tan 1   (14)
 X real (1) 

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. 5: how to simulate equations (13) and (14).

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.

Fig. 6: Magnitude of the 1 kHz component calculated by means of the DFT.

Similar analysis is easily performed for m=2.


2.1. Computing the DFT for m =0
How about m= 0? Such value means the DC value of the input signal. By replacing m = 0 in equations (5) and
(6), the results will be:
N 1
X real (0)   x(n) (15)
n 0

And

X img (0)  0 (16)

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)

The simulated circuit is shown in the following figure.


x0

x1

x2
V X0_mag
x3

1/8
x4 K

x5

x6

x7

Fig. 7: How to compute the DC values of the input signal.

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).

3 The Fast Fourier Transform (FFT)

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.

Here, the input signal is the same of the previous one.

3.1. Simulating the FFT equation


The FFT is given by (18).

 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.

For the sake of simplicity, let’s write equation (19) as


m
X (m)  A(m) W N B(m) (23)

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)

3.2. Computing the FFT equation for m =1

For m = 1 and N = 8, equation (23) becomes


1
X (1)  A(1) W 8B(1) (26)

equation (24) becomes:


3
n1
A(1)   x (2n)W 4 (27)
n 0

And equation (25) becomes:


3
n1
B(1)   x (2n  1)W 4 (28)
n 0

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 

Fig. 9 presents the constant in the simulation file.


1
real W4_0_real
W4_0
0
img W4_0_img

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. 9: The constants in the simulation file.

The equation (27) for real values is given by (40).


3
n1 0 1 2 3
A(1)real   x (2n)W 4  x (0)W 4 _ real  x(2)W 4 _ real  x (4)W 4 _ real  x(6)W 4 _ real (40)
n 0

Similarly, the equation (27) for imaginary values is given by (41).


3
n1 0 1 2 3
A(1)img   x(2n)W 4  x(0)W 4 _img  x (2)W 4 _img  x (4)W 4 _img  x (6)W 4 _img (41)
n 0

The equation (28) for real values is given by (42).


3
n1 0 1 2 3
B(1)real   x (2n  1)W 4  x (1)W 4 _real  x (3)W 4 _real  x(5)W 4 _real  x (7)W 4 _real (42)
n 0

Similarly, the equation (28) for imaginary values is given by (43).


3
n1 0 1 2 3
B(1)img   x(2n  1)W 4  x(1)W 4 _ img  x (3)W 4 _ img  x(5)W 4 _ img  x (7)W 4 _ img (43)
n 0

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)

But this equation is complex, so we need to write as it, given by (45).

X (m)  A(m)real  jA(m)img  W m


N _ real
m

 jW N _img  B (m) real  jB (m)img  (45)

Which results in (46) and (47).


m m
X (m) real  A(m) real  W N _ realB(m)real  W N _imgB(m)img (46)

m m
X (m)img  A(m)img  W N _ realB(m)img  W N _imgB(m) real (47)

For m =1 and N= 8, these equations results in (48) and (49).


1 1
X (1) real  A(1) real  W 8_ realB(1)real  W 8 _imgB(1)img (48)
1 1
X (1)img  A(1)img  W 8_ realB(1)img  W 8_imgB (1) real (49)

The magnitude of X(1) is given by

X 1magnitude  X real (1)2  X img (1)2 (50)

Fig. 11 presents how to calculate equations (48), (49) and (50).


V X1_mag_real V V X1_mag
X1_mag_img
A1_real
1/4 SPI4
xa K

W8_1_real xa

B1_real

W8_1_img

B1_img

A1_img

W8_1_real

B1_img

W8_1_img

B1_real

Fig. 11: How to calculate equations (48), (49) and (50).

Fig. 12 presents the X(1) magnitude. It is the same 1.125 found in the input signal.

Fig. 12: X(1) magnitude.


4 Experimental results
The FFT was tested in the DSC 28335 microcontroller.

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)

You might also like