The Fourier Transform - Department of Music : Home

9
The Fourier Transform Learning outcomes Use the Discrete Fourier Transform to perform frequency analysis on a discrete (digital) signal Explain the significance of the Fast Fourier Transform algorithm; Explain why windowing is important in most real-world frequency analysis. The Discrete Fourier Transform Equation (1) below defines the Discrete Fourier Transform: ! " = " = 1 0 2 N k N mk j k m e x X # (1) For those familiar with continuous Fourier Transform (FT), DFT differs from FT in the following aspects: the continuous function of time x(t) (our signal) is replaced by the time sequence x k continuous time t is replaced by time index (or sample number) k which takes values 0, 1, 2, …N-1 the length of the signal (or of the chunk being transformed) is N samples the integral is replaced by a summation the continuous function of frequency X(ω) is replaced by the frequency sequence X m continuous frequency f is replaced by frequency index m which takes values 0, 1, 2, …N-1 There is a corresponding Inverse Discrete Fourier Transform which takes a frequency spectrum and turns it back into a time signal: ! " = = 1 0 2 1 N m N mk j m k e X N x # (19) DFT Example: A Rectangular Pulse Consider this simple finite pulse: 0 0 0 1 1 1 5 4 3 2 1 0 = = = = = = x x x x x x

Transcript of The Fourier Transform - Department of Music : Home

Page 1: The Fourier Transform - Department of Music : Home

The Fourier Transform Learning outcomes • Use the Discrete Fourier Transform to perform frequency analysis on a discrete

(digital) signal • Explain the significance of the Fast Fourier Transform algorithm; • Explain why windowing is important in most real-world frequency analysis. The Discrete Fourier Transform Equation (1) below defines the Discrete Fourier Transform:

!"

=

"

=1

0

2N

k

N

mkj

km exX

#

(1)

For those familiar with continuous Fourier Transform (FT), DFT differs from FT in the following aspects:

• the continuous function of time x(t) (our signal) is replaced by the time sequence xk

• continuous time t is replaced by time index (or sample number) k which takes values 0, 1, 2, …N-1

• the length of the signal (or of the chunk being transformed) is N samples • the integral is replaced by a summation • the continuous function of frequency X(ω) is replaced by the frequency

sequence Xm • continuous frequency f is replaced by frequency index m which takes values

0, 1, 2, …N-1 There is a corresponding Inverse Discrete Fourier Transform which takes a frequency spectrum and turns it back into a time signal:

!"

=

=1

0

2

1N

m

N

mkj

mk eXN

x

#

(19)

DFT Example: A Rectangular Pulse Consider this simple finite pulse:

0

0

0

1

1

1

5

4

3

2

1

0

======

x

x

x

x

x

x

Page 2: The Fourier Transform - Department of Music : Home

0 1 2 3 4 5-0.2

0

0.2

0.4

0.6

0.8

1

1.2

k

x

To calculate the DFT of this signal, we have to find each value of X in turn; first X0, then X1 and so on:

3543210

5

0

5

0

6

02

0=+++++=== !!

==

"

xxxxxxxexXk

k

k

kj

k

#

3

2

36

22

2

6

12

1

6

02

0

5

0

6

12

11

!!!!!! jjjjj

k

kj

k eeexexexexX

"""""

=

"

++=++==#

We can reduce the amount of algebra here by defining a new variable. Let

866.05.03 jez

j

!==

! "

z is periodic with a period of 6 (because !2j

e" is periodic with a period of 1). This

helps us to evaluate:

410

28

06

15

24

3

3

2

2

1

866.05.0

866.05.0

1

866.05.0

zz

zz

zz

jzz

jzz

ez

jezj

j

=

=

==

+==

+!==

==

!!==

!

!

!

!

"

"

Using these substitutions in our DFT calculations, we have:

732.11866.05.0866.05.0112

1jjjzzX !=!!!+=++=

Similarly,

0866.05.0866.05.01

4204

2

2

1

0

0

5

0

2

5

0

6

22

2

=+!!!=

++=++=== ""==

!

jj

zzzzxzxzxzxexXk

k

k

k

kj

k

#

Page 3: The Fourier Transform - Department of Music : Home

0866.05.0866.05.01

6306

2

3

1

0

0

5

0

3

3

=+!!!=

++=++=="=

jj

zzzzxzxzxzxXk

k

k

0866.05.0866.05.01840

5

0

4

4=!!+!=++=="

=

jjzzzzxXk

k

k

732.11866.05.0866.05.011050

5

0

5

5jjjzzzzxX

k

k

k +=+!++=++=="=

We can now plot the magnitude spectrum:

0 1 2 3 4 50

0.5

1

1.5

2

2.5

3

m

|

X

|

… and the phase spectrum:

0 1 2 3 4 5-2.5

-2

-1.5

-1

-0.5

0

0.5

1

1.5

m

arg(X)

Page 4: The Fourier Transform - Department of Music : Home

The Fast Fourier Transform Calculating the DFT for our six-point signal above was quite a lot of work. Most real digital signals, though, are much longer. A signal of 1,000 points or even 1,000,000 points is quite usual. But the number of (complex) multiplications required to find the DFT of an N-point signal is N 2. What if we take one second of signal from an audio CD and we want to know what the frequency content of that one second is? That’s 44,000 sample points. Clearly, we need an automated method to calculate the DFT! It would be quite possible (and not very hard) to write a computer program to calculate the DFT directly. But this would still be slow. Modern signal procesing often demands that the spectrum be calculated in real time - that is, the spectrum display is updated continuously as new signal values arrive. This sort of speed requires a very efficient algorithm to calculate the DFT. Fortunately for us, one exists. The Fast Fourier Transform is a matrix-based algorithm for calculating the DFT, discovered by a number of independent workers in the 1950’s. It is the cornerstone of modern signal processing. You can read about how the FFT works in just about any text on digital signal processing – we need not concern ourselves with its workings here. Implementations of it exist in most programming languages as subroutines or functions. One of the main reasons for using Matlab for this course, though, is that Matlab comes with the FFT built in. This makes it very quick and easy to obtain the spectrum of a signal. For example, to generate the short pulse in the example above and obtain its spectrum took only the following lines in Matlab: x = zeros(6,1); % initialise x to have six values, all 0 x(1:3) = ones(3,1); % then set the first 3 values to 1 X = fft(x); % find the DFT of x There is one restriction on the FFT algorithm:

integer,2 pNp

= That is, the number of points in the time domain signal must be equal to an integer power of 2. Common FFT sizes are 512, 1024, 2048 points. There are other algorithms available for computing a DFT when this condition on N is not satisfied, but they are much less efficient than the FFT. (Matlab will automatically use the best algorithm for the size of signal you give it, but it will be much faster if N is a power of 2.) The FFT is so ubiquitous that its name is often used in place of the DFT – this is, strictly speaking, incorrect. The DFT is the mathematical transform that allows us to take a digital signal in the time domain and transform it to the frequency domain. The FFT is just an efficient way of doing the DFT.

Page 5: The Fourier Transform - Department of Music : Home

FFT Example: The Rectangular Pulse Revisited Now that we have a way of transforming longer signals, let’s look at the spectrum of a pulse with a few more sample points:

!"#

$%&

''

''=

10238,0

70,1

k

kyk

0 200 400 600 800 1000-0.2

0

0.2

0.4

0.6

0.8

1

1.2

k

y

Taking the FFT of this signal and ploting the magnitude:

0 200 400 600 800 1000 12000

1

2

3

4

5

6

7

8

m

|

Y

|

We notice two things about this spectrum plot:

• it is symmetrical • the first half of it looks like the absolute value of the sinc function we saw in

the spectrum of the periodic rectangular pulse train earlier. The symmetry is a feature of the DFT. It is also present in the first example which we calculated by hand. In fact, only the first half of the spectrum has physical meaning. (The RH half is sometimes interpreted as existing at negative frequencies, but these

Page 6: The Fourier Transform - Department of Music : Home

will not concern us on this module.) Thus, though we put N points of information into the DFT (or FFT), we only get N/2 points back out. Frequency resolution and sampling rate So far, we have not said anything about how our time signal samples are spaced in time (other than that they must be regularly spaced). If we do specify the sampling rate, though, we can put units on both the time axis and the frequency axis of our plots. Let the sampling frequency of our time signal xk be fs so that the time interval from one sample point to the next is

sf

1=!

The signal then covers the time range from 0 to (N-1)τ seconds. The frequency resolution, or the spacing between consecutive points of Xm is given by

1)1(

1

!=

!="

N

f

Nf s

# (20)

The N points of the DFT Xm must cover the frequency range from 0 Hz to a maximum frequency of sffN =!" )1( Hence the maximum useable frequency in the DFT is fs/2 - the Nyquist frequency! (Remember that we only use the first half of the mirrored spectrum.) For example, if we set the sampling rate of the pulse above at, say, 10 kHz, then we can replot the graphs against time and frequency respectively:

0 0.02 0.04 0.06 0.08 0.1-0.2

0

0.2

0.4

0.6

0.8

1

1.2

time (s)

y

Page 7: The Fourier Transform - Department of Music : Home

0 1000 2000 3000 4000 50000

1

2

3

4

5

6

7

8

frequency (Hz)

|Y|

Windowing The example we have used so far, the single rectangular pulse, is short enough that we can fit the whole signal nicely into our analysis frame for the FFT. In fact, in the later examples, I deliberately chose the length of the signal to be 1024 points, a convenient size for the FFT algorithm. Most real-world signals are not like this. Very often, we wish to extract part of a long signal and perform frequency analysis on it (not least because execution times for the FFT rise with the number of points being transformed). Extracting part of the signal is called windowing. Mathematically, we are multiplying our long signal by a shorter window function. The naïve window function to use is a rectangular one. This is equivalent to simply chopping out the part of the signal that we want to analyse and putting it in the DFT. Doing this can lead to substantial errors in the resulting spectrum, however. The plot below illustrates the effects of rectangular windowing for a sine wave at 10 Hz. The peak in the frequency spectrum is not as sharp as it should be (in theory a delta function) and there seems to be a lot of energy present at a range of other frequencies. This sort of spectral error is called smearing and it is important to minimise it.

Page 8: The Fourier Transform - Department of Music : Home

0 20 40 60 80 100-40

-20

0

20

40|X(f)|(dB)

f (Hz)

0 0.2 0.4 0.6 0.8 1-1

-0.5

0

0.5

1

x(t)

t (s)

What we need is a window which does not suddenly cut off the signal at its edges, but rather one which smooths the signal gradually down to zero at its edges. One candidate for this is called the Hamming window. This is defined by

NkN

khk

!!"#

$%&

'

((= 0,

1

2cos46.054.0

)

and it looks like this:

0 20 40 60 80 100

0

0.2

0.4

0.6

0.8

1

k

Hamming window

Comparing the two windows, we can see that the Hamming window will smooth the data more effectively than the rectangular window.

Page 9: The Fourier Transform - Department of Music : Home

0 20 40 60 80 100

0

0.2

0.4

0.6

0.8

1

k

Rectangular window

Now look at the spectrum of the sine wave again. This time, the signal is multiplied by a Hamming window before the FFT is taken:

0 20 40 60 80 100-40

-20

0

20

40|X(f)|(dB)

f (Hz)

0 0.2 0.4 0.6 0.8 1-1

-0.5

0

0.5

1

x(t)

t (s)

While not perfect, the spectrum is much improved – we can see that most of the energy is at he expected peak at 10 Hz. (By the way, the plots demontrating the effects of windowing were taken from one of Matlab’s own demos. You can run these yourself, if you have a version of Matlab with the Signal Processing Toolbox installed. To do this, go to Help, Examples and Demos, then double-click on Toolboxes.)