You are on page 1of 15

Implementation of 1-D DFT (FFT) and Interpretation of the output

Md. Arif Khan EE, UW, USA and ECE, KUET, Bangladesh Email: mkhan4@uwyo.edu July 21, 2013
Abstract It has been observed that many pupils/technical persons nd Discrete Fourier Transform (DFT) (which is generally implemented as Fast Fourier Transform (FFT) ) somewhat dicult to understand ranging from complex mathematical equations to interpretation of nal results. In this document, practical implementation of 1-D DFT as FFT, interpretation of the FFT output, and several implications of the FFT implementation are discussed with few examples (corresponding MATLAB code is also provided). This is a quick and simple demonstration of FFT. Interested reader can go through any Digital Signal Processing (DSP)/ Digital Image Processing (DIP) books for more details. After careful reading, author hopes, reader will have a rm grasp on the FFT implementation for real world 1D signal/data processing.

Background

As a reminder, a brief theoretical and conceptual background of Discrete signal/sequence and DFT is provided here to be consistent with the subsequent subject matters. Details can be found in DSP or DIP books.

1.1

Discrete signal/sequence

To be comfortable in the discrete world (discrete and digital are not same although they are often used interchangeably), lets see an example. Figure 1 shows a single period of a continuous 1 Hz cosine signal x(t), together with its sampled version x[n], which is sampled with sampling frequency F s = 20 samples/second. ***Very very important thing to remember here, as soon as a signal is sampled with a sampling frequency F s, we immediately restrict the signal in the world of frequencies F s/2 to +F s/2. Any other frequencies outside this range shall be aliased back to lower frequencies 1 .
1

More details can be found in my Demonstration of sampling document

1 0.5 >x(t) 0 0.5 1 0 0.2 0.4 0.6 >time (seconds) 0.8 1 >x(t)

1 0.5 0 0.5 1 0 5 10 >sample no 15 20

1 >x(t), x[n] 0.5 0 0.5 1 0 0.2 0.4 0.6 >time (seconds) 0.8 1 >x(t), x[n]

1 0.5 0 0.5 1 0 5 10 >sample no 15 20

1 >x[n]) 0.5 0 0.5 1 0 0.2 0.4 0.6 >time (seconds) 0.8 1 >x[n]

1 0.5 0 0.5 1 0 5 10 >sample no 15 20

Figure 1: Continuous signal x(t) is 1 Hz cosine wave. x[n] is sampled version of x(t) with sampling frequency F s = 20 samples/second. See their x-labels and y-labels. So here we can say the length of the sequence x[n] is, N = 20 (since its not zero padded). Listing 1: MATLAB routine for the plots shown in Figure 1. % Generate 1 Hz cosine wave with sampling frequency Fs =20 Hz % x = A cos (2. pi.f.t) A = 1 ; % amplitude of the cosine wave fcos =1; % cosine wave frequency fs =20; % sampling frequency ts = 1/ fs ; % sampling interval t = 0 : ts : 1ts ; % time vector x = A cos ( 2 pi fcos t ) ; %cosine wave

11

You often notice, the authors in the DSP/DIP books talks about zero-padding. Why we need zero-padding will be discussed later. At this time, zero-padded sequence of x[n] is shown in Figure 2.
1 0.5 >x[n] 0 0.5 1 >x[n] zeropadded) 0 5 10 >sample no 15 20 1 0.5 0 0.5 1

10

15 20 >sample no

25

30

35

Figure 2: Discrete signal x[n], as shown in Figure 1, together with zero-padded sequence x[n]. See their x-labels and y-labels. So here we can say the length of the sequence x[n] is, N = 32 (zero-padded).

Listing 2: MATLAB routine for the plots shown in Figure 2. % Generate 1 Hz cosine wave with sampling frequency Fs =20 Hz and % then zero padded to length 32 % x = A cos (2. pi.f.t) N = 32; A = 1; fcos =1; fs =20; % % % % Zeropadded length amplitude of the cosine wave cosine wave frequency sampling frequency

11

ts = 1/ fs ; % sampling interval t = 0 : ts : 1ts ; % time vector x = A cos ( 2 pi fcos t ) ; %cosine wave l = length ( x ) ; % zeropadded coseine wave xpadd = zeros ( 1 , N ) ; xpadd ( 1 : l )=x ;

13

15

17

1.2

Discrete Fourier Transform (DFT)


N 1

The DFT of the discrete sequence x[n] is dened as X [f ] =


n=0

x[n] exp

j 2f n N

(1)

where f = 0, 1, 2, ...N 1 and Inverse DFT (IDFT) is dened as 1 x[n] = N where n = 0, 1, 2, ...N 1 Now try to understand the parameters of these equations Equation (1) and equation (2) together are called DFT pair.
1 can be in the forward ( equation (1)) or in the reverse (equation The scaling factor N (2)) direction. Both are correct. N 1

X [f ] exp
f =0

j 2f n N

(2)

n in x[n] goes from n = 0 to N 1. So N is the length of the sequence x[n] (may be zero-padded or not, but keep in mind interpretations of the DFT (FFT) output will be somewhat dierent). f in X [f ] goes from f = 0 to N 1. So N is the length of the sequence X [f ]. Sampling interval, T s = 1/F s = 1/20 = 0.05 seconds/sample, and frequency step, f = F s/N = 20/20 = 1 Hz From the discussion so far we can conclude: Result 1: Length of the DFT output is equal to the length of the input sequence, in our example which is N = 20. Result 2: Frequency f = 0 means DC component of the signal. So, very rst point of the X [f ], as described in equation (1) is DC component (plug f = 0 in equation (1)). Result 3: DFT output is limited within the range [-Fs/2, Fs/2], with the f = F s/N Result 4: Max frequency F s/2 would show up at F s/2 1/f = F s/2 N/F s = N/2 on the frequency axis. Important to note, if F s is normalized to 1, then Fs/2 would be 0.5, and if F s is normalized to 2 (frequency in radian) then F s/2 would 4

be . Result 5: X [f ], as described in equation (1) is always complex number. So there will be phase terms associated with it, and without this phase information, original signal cannot be reconstructed perfectly. MATLAB t command calculates the DFT of a discrete sequence x[n] and splits the results in the order of DC, Positive frequencies in increasing order, Negative frequencies in decreasing order.

DFT (FFT) result interpretation

Now its time for implementation and interpretation of few examples. Example 1: In this example we shall try to compute and interpret the DFT (FFT) output of the discrete sequence as shown in Figure 1. Figure 3 shows the input sequence x[n] with the corresponding DFT output magnitude |X [f ]|.
1 0.5 >x[n] 0 0.5 1 >Normalized magnitude (|X|) 0.5 0.4 0.3 0.2 0.1 0 0 5 10 >frequency f 15 20

10 >sample no

15

20

Figure 3: Discrete sequence x[n] and magnitude of the DFT output. Note they are of same length (N = 20). Listing 3: Part of MATLAB routine for the plots shown in Figure 3. % Generate 1 Hz cosine wave with sampling frequency Fs =20 Hz % Then compute the DFT (FFT ) X = f f t ( x ) ; % 1D FFT of signal x N = length ( X ) ; mag = abs ( X ) ; % Magnitude magN = mag / N ; % Normalizing pha = angle ( X ) ; %phase pha = pha 180/ pi ; %phase in degree

10

Now from section 1.2 and from Figure 3: Result 1: Length of x[n] is equal to the the length of X [f ], which is N = 20. Result 2: Now look at x[n]. Is there any DC component? NO. According to result 2, DC component would show up at f=0, and you can see |X [f ]| is zero at f = 0. Result 3: f = F s/N = 20/20 = 1 Hz . Spikes show up at f = 1, and at f = 19 in the plot of |X [f ]|. Which means signal frequencies of x[n] are f 1 = 1 Hz and f (19 20) = 1 Hz . What was the frequency of the original signal? Yes! its 1 Hz . ***What does negative frequency of a signal mean? It means phase of the signal at that frequency is reversed. Almost every one has seen the eect of negative frequency. Try to remember the rotating wheels of a moving car. You may notice that sometimes it seems rotating backward while the car is actually moving in the forward direction. Also there is an aliasing eect which makes rpm of the wheel is much less than its actual rpm. Why negative frequencies show up in the DFT output? Its due to the denition of the DFT. The DFT output is always complex. First half of the result is complex conjugate to remaining half (except DC point). Result 4: Max frequency F s/2 would show up at N/2 = 20/2 = 10. Lets check; max f requency = F s/2 = 20/2 = 10 Hz , which is equal to N/2 f = 10 1 = 10 Hz . The DFT output sequence is 0, 1f, 2f, 3f, ....., 8f, 9f, 10f, 9f, 8f, ....., 3f, 2f, 1f Hz , which means FFT result is in the order of DC, Positive frequencies in increasing order, Negative frequencies in decreasing order.

>Normalized magnitude (|X|)

0.5 0.4 0.3 0.2 0.1 0 0 5 10 >frequency f 15 20 >Phase in degree

200 100 0 100 200

10 >frequency f

15

20

Figure 4: Magnitude of the DFT output and associated phase in degree. Note they are of same length (N=20).

Result 5: Output of DFT is always complex. Figure 4 shows the magnitude and phase of the result. Although magnitude is often used than phase, but it is very important when reconstruction of original signal is done from DFT output 2 . Example 2: Now what will happen if we add a DC signal (say DC=1 unit) to the original cosine wave (as shown in Figure 1). Lets see.
1 >xDC[n] 0 0.2 0.4 0.6 0.8 >time (seconds) 1 >xDC(t) 0.5 0 0.5 1 >Normalized magnitude (|XDC|) 1 0.5 0 0.5 1 0 5 10 >sample no 15 20

>Phase in degree 0 5 10 15 >frequency f 20

200 100 0 100 200 0 5 10 15 >frequency f 20

0.5

Figure 5: See at f = 0 a spike shows up in magnitude plot representing a DC level of 1. Listing 4: Part of MATLAB routine for the plots shown in Figure 5. % Generate 1 Hz cosine wave with sampling frequency Fs =20 Hz % Add a DC level , then compute the DFT (FFT) DC = 1 ; % DC level xDC = DC+A cos ( 2 pi fcos t ) ; %cosine wave + DC

To keep the volume of this document reasonable, I will explain the eects of phase in the signal or image later in another documentation.

Example 3: Now what if sequence is zeropadded (as shown in Figure 2)?

1 >xpad[n] 0 0.5 1 1.5 >time (seconds) 2 >xpad(t) 0.5 0 0.5 1 >Normalized magnitude (|Xpadd|)

1 0.5 0 0.5 1

10

20 >sample no

30

40

0.4 0.3 0.2 0.1 0 >Phase in degree 0 10 20 30 >frequency f 40

200 100 0 100 200

10

20 30 >frequency f

40

Figure 6: DFT of the zero padded sequence. Note the length N = 32. Result 1: Length of xpad[n] is equal to the the length of Xpad[f ], which is N = 32. Result 2: Now look at xpad[n]. Is there any DC component? NO. You can see |Xpad[f ]| is zero at f = 0. Result 3: f = F s/N = 20/32 = 0.625 Hz . Spikes show up at dierent values of f , although original signal frequency is 1 Hz . This is due to frequency leakage which is discussed in section 5. So, why we need zeropadding? The answer is to avoid circular convolution eect of the DFT. The inherent periodicity of DFT causes this eect. More details can be found in DSP/DIP books. Result 4: Max frequency F s/2 would show up at N/2 = 32/2 = 16. Lets check; max f requency = F s/2 = 20/2 = 10 Hz , which is equal to N/2 f = 16 0.625 = 10 Hz . Result 5: Output of DFT is again complex.

Usage of f f tshif t

As described in section 1.2, MATLAB t command calculates the DFT of a discrete sequence x[n] and splits the results in the order of DC, Positive frequencies in increasing order, Negative frequencies in decreasing order. This is somewhat less intuitive when someone tries to explain output from gures. MATLAB f f tshif t command shifts the DC point (f = 0) to the center of the data sequence. After that, FFT output sequence becomes neagtive frequencies in decreasing order, DC, and positive frequencies in increasing order. The f f tshif t version of the outputs for the examples in section 2 are shown in Figure 7.

>Normalized |X|

0.6 0.4 0.2 0 0 5 10 >frequency f 15 20

>fftshift version

0.8

0.8 0.6 0.4 0.2 0 10 1 5 0 >frequency f 5 10

>Normalized |XDC|

0.5

>fftshift version 0 5 10 >frequency f 15 20

0.5

0 10 0.4 0.3 0.2 0.1 0 20

0 >frequency f

10

>Normalized |Xpadd|

0.3 0.2 0.1 0 0 10 20 >frequency f 30 40

>fftshift version

0.4

10

0 >frequency f

10

20

Figure 7: f f tshif t version of the DFT outputs from section 2. Note DC is shifted at the center.

Whether it is F s, or 2 , or 1?

Sometimes this becomes very confusing. These are dierent ways of describing frequency variable. Here you go F s is in samples/second, which is equal to 2 in radian (idea from analog frequency ), or 1 when frequencies are normalized. F s/2 is in samples/second (Nyquist limit), which is equal to in radian, or 0.5 when frequencies are normalized. This idea comes from the unit circle of Z-transform. Now see Figure 8, and inspect the dierences in x-labels of the plots.

>Normalized (|X|)

0.5

>fftshift version 0 5 10 15 >frequency step df 20 >fftshift version

0.5

0 1

0 10 1

>Normalized (|X|)

0 5 >frequency step df

10

0.5

0.5

0 1

>Normalized (|X|)

10 15 >frequency f in Hz

20 >fftshift version

0 10 1

0 5 >frequency f in Hz

10

0.5

0.5

0 1

>Normalized (|X|)

2 3 4 >frequency f in pi unit

6 >fftshift version

0 4 1

2 0 2 >frequency f in pi unit

0.5

0.5

0.2 0.4 0.6 0.8 >frequency f (normalized)

0 0.5

0 >frequency f (normalized)

0.5

Figure 8: Dierent labeling of frequency axis.

10

Frequency/Spectral Leakage

This is very important to know because it may screw you up. It may make you think your result is a mess while its not. Once again, from Figure 1 you can see the period of the 1 Hz cosine wave is Ncos = 20 samples. What will happen if we take few more samples from the 1 Hz cosine wave and compute the DFT. Lets see! Figure 9 shows the eects of data length in DFT output. Note how frequency leakage increases with the data length becomes more and more o from the integer period of the original signal.

1 0.4 0 1 0 1 0.4 0 1 0 1 0.4 0 1 0 1 0.4 0 1 0 1 0.4 0 1 0 10 20 30 (i) N = 2*20 (two periods) 40 0.2 0 0 10 20 30 (g) Again no leakage 40 10 20 30 40 (g) N = Ncos+Ncos/2 = 20+10 =30 0.2 0 0 10 20 30 (h) Considerable leakage 40 10 20 30 (e) N = Ncos+3 = 20+3 =23 40 0.2 0 0 10 20 30 (f) Small leakage 40 10 20 30 (c) N = Ncos+1 = 20+1 =21 40 0.2 0 0 10 20 30 (d) Very small leakage 40 10 20 30 40 (a) N = Ncos = 20 (One period) 0.2 0 0 10 20 30 (b) No leakage 40

Figure 9: See x-labels for details. Note how frequency leakage changes.

11

Now you should be able to describe what is going on after a glance to your results. This is due to the mathematical denition of DFT, and does not mean your result is o. DFT thinks data is periodic with the period of N . Figure 10 shows this phenomena. What are the frequencies associated with an edge? Answer is high frequencies (you might wanna study Fourier Series of a pulse). If the original signal does not capture an integer multiple of its wavelength (or period), repitition results a sharp transition (edge) in the signal which causes this frequency leakage in the DFT output. When we analyze a real world signal (say speech signal), it may contain lot of frequencies. Capturing an integer multiple of wavelength for all frequencies is almost impossible. So dont panic when output looks somewhat dierent than what you expected.

1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 10 20 30 40 (k) original input 50 60 0 10 20 30 40 (i) original input 50 60 0 10 20 30 40 (g) original input 50 60 0 10 20 30 40 (e) original input 50 60 0 10 20 30 40 (c) original input 50 60 0 10 20 30 40 (a) original input 50 60

1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 1 0 1 0 10 20 30 40 (l) What DFT thinks 50 60 0 10 20 30 40 (j) What DFT thinks 50 60 0 10 20 30 40 (h) What DFT thinks 50 60 0 10 20 30 40 (f) What DFT thinks 50 60 0 10 20 30 40 (d) What DFT thinks 50 60 0 10 20 30 40 (b) What DFT thinks 50 60

Figure 10: See x-labels for details. Note sharp transition in the signal.

12

Mairalaise, KUET amare mairalaise!!!!!!!!!!!

:) Now lets see DFT of a real signal. Required MATLAB les are provided in association with this document (see the attached zip le). Figure 11 shows the recorded signal, a cosine wave of 3 kHz , and the combined signal created by adding the previous two signals. The DFT of these signals are shown in Figure 12. Closely inspect how does the spectrum change.

0.5 Recorded signal

0.5

0.5

1 1.5 Time in seconds

2.5

0.02 Cosine signal 0.01 0 0.01 0.02

0.005

0.01 0.015 Time in seconds

0.02

0.025

0.5 combined signal

0.5

0.5

1 1.5 Time in seconds

2.5

Figure 11: Recorded, cosine of 3 kHz , and combined signal in time domain.

13

150 |Yrecorded|

100

50

0 4000 300

3000

2000

1000 0 1000 Frequency in Hz

2000

3000

4000

|Xcosine|

200

100

0 4000 300 |Xcombined|

3000

2000

1000 0 1000 Frequency in Hz

2000

3000

4000

200

100

0 4000

3000

2000

1000 0 1000 Frequency in Hz

2000

3000

4000

Figure 12: Recorded, cosine of 3 kHz , and combined signal in frequency domain.

14

Conclusions

This is a simple and quick demonstration of how one can implement and interpret the DFT (FFT) of a 1D signal. Everything discussed here is really very important because DFT is a great and powerful tool for many people ranging from non-technical to technical eld. The results discussed in section 1.2 must be carefully understood. Say, why do we care about the length of the DFT output? Its because when you are trying to implement DFT/FFT in limited memory chips/processors, or try to implement a real time processing algorithm (for example sound system, or MP3, Mobile, IPad.. etc ), you have to calculate how much memory space is needed, or the associated delay of the system accordingly. To keep the volume reasonable, more detail discussion about the results is omitted. I hope, at this point you found DFT/FFT easy and interesting to work with. Have a great time with DFT!

***Associated MATLAB les are also provided in association with this document. For MATLAB les, question, suggestion, and possible heads up, go to the following link https://www.facebook.com/groups/353398578095803/

15

You might also like