discrete fourier transform

13
O3 1 © w t norris O3 DFT & Matlab, 06 December 2002 Example of the Discrete Fourier Transform in MATLAB MATLAB performs the DFT well. The function is fft(vec) where vec is a vector of a sequence of values over time. Take the simple example we discussed on p. 8 of section O2. x(n) n 0 0 1 1 2 2 3 3 4 Implied by assumed periodicity x = {0, 1, 2, 3 } The MATLAB command script »x=[0 1 2 3]; »dft=fft(x}; % The DFT »figure(1),stem([01 2 3],real(dft)) »figure(2),stem([01 2 3],imag(dft)) yields the stem plots below which contain the same information as the figures on p. 25. The left hand graph is the real part and the right hand graph the imaginary part. The harmonic number is the abscissa and the ordinates are the real and imaginary values. Real part 0 1 2 3 -2 0 2 4 6 Imaginary part 0 1 2 3 -2 -1 0 1 2 Before we had:– Re[X k ] Im[X k ] k 0 0 12 2 4 –2 –2 3 6 4 Implied by assumed periodicity k 0 0 1 2 2 3 4 Implied by assumed periodicity This merely serves to confirm that MATLAB is not in error - at least on this occasion. It even recovers the original signal with the inverse transform, ifft »ifft(dft) ans = 0 1 2 3

Upload: muhammad-redzuan

Post on 28-Jan-2016

221 views

Category:

Documents


0 download

DESCRIPTION

A notes about the Dicrete fourer transform and the graph obtain tru calculation and the matlab codes.

TRANSCRIPT

Page 1: Discrete Fourier Transform

O3 1

© w t norris O3 DFT & Matlab, 06 December 2002

Example of the Discrete Fourier Transform in MATLAB

MATLAB performs the DFT well. The function is fft(vec) where vec is a vector of asequence of values over time.

Take the simple example we discussed on p. 8 of section O2.

x(n)

n0�

0� 1�

1�

2�

2�

3�

3�

4�Implied by assumed

periodicity

x = {0, 1, 2, 3 }

The MATLAB command script

»x=[0 1 2 3];»dft=fft(x}; % The DFT»figure(1),stem([01 2 3],real(dft))»figure(2),stem([01 2 3],imag(dft))

yields the stem plots below which contain the same information as the figures on p. 25.The left hand graph is the real part and the right hand graph the imaginary part. Theharmonic number is the abscissa and the ordinates are the real and imaginary values.

Real part

0 1 2 3-2

0

2

4

6

Imaginary part

0 1 2 3-2

-1

0

1

2

Before we had:–

Re[Xk]

Im[Xk]

k0 0

1� 2�2

4

–2

–2

3�

6

4

Implied by assumed�periodicity

k0

0 1 2

2

34 Implied by assumed

periodicity

This merely serves to confirm that MATLAB is not in error - at least on this occasion. Iteven recovers the original signal with the inverse transform, ifft

»ifft(dft)

ans =

0 1 2 3

Page 2: Discrete Fourier Transform

O3 2

© w t norris O3 DFT & Matlab, 06 December 2002

100 sample waveform

The Appendix gives a MATLAB function for plotting various results of the DFT of agiven function. In the Command window define a function, e.g., thus

»y='1+cos(2*t) + 0.5*sin(34*t)'

The quotation marks are important as is the use of t as a variable though the name y is notimportant. Then

»fast(y)

gives six panels of plots shown below.

I used only 100 points in the analysis to show the stem plots clearly.

The symmet ryshould be clear. Thed.c. at 0 frequency ispresent. As we filledin the 4 point on p.25 so one could putin the d.c. value at af r e q u e n c y o fnumber 100.

Bear in mind thatafter the 49thharmonic the plot, inits idiosyncratic way,is showing the c– ncoefficients startingat -49 and advancingto -1.

The cos(2*t) appearsin the real part. For

Re(cn) = an

and an shows acosine wave

The s in (34* t )appears in theimaginary part for

Im(cn) = bn

and bn shows a sinewave

0 20 40 60 80 1000

20

40

60

80

100

harmonic number

Full DFT to show Symmetry - a Tyger plot

0 20 40 60 80 100

-20

-10

0

10

20

harmonic number

Page 3: Discrete Fourier Transform

O3 3

© w t norris O3 DFT & Matlab, 06 December 2002

The graph of the original samples 1+cos(2*t) + 0.5*sin(34*t) and the reconstructedplot at a limited number of points are :–

0 1 2 3 4 5 6 7-2

0

2

4

time

Original and reconstructed function at time points

0 1 2 3 4 5 6 7-2

0

2

4

time

0 1 2 3 4 5 6 7-5

0

5x 10-15

time, secs

The lower graph shows the imaginary part of the reconstructed signal. Note the 10-15 onthe scale fro the imaginary part of teh reconstructed signal: this results from roundingerrors in the computation.

We can also look as is normal at the magnitude alone. It is here plotted to showsymmetry. There is also a phase plot which is rich in detail but a result of rounding errorsand of no significance.

Page 4: Discrete Fourier Transform

O3 4

© w t norris O3 DFT & Matlab, 06 December 2002

0 20 40 60 80 1000

20

40

60

80

100

harmonic number

Fourier analysis, magnitude and phase

0 20 40 60 80 100

-100

0

100

harmonic number

Page 5: Discrete Fourier Transform

O3 5

© w t norris O3 DFT & Matlab, 06 December 2002

The function can be used to demonstrate some features of the DFT.

Separation of harmonics y = cos(32 t) – cos(33 t)

The function looks like this. There is beating between the waves.

0 1 2 3 4 5 6 7-2

-1.5

-1

-0.5

0

0.5

1

1.5

2

time

The harmonic identification in the DFT is clear Notice that both have only realcomponents of cn but the even harmonic is positive and the odd harmonic is negative.

.

0� 5� 10� 15� 20� 25� 30� 35� 40� 45� 50�-50�

0�

50�

harmonic number�

Rea

l par

t of F

ourie

r� c

oeffi

cien

t, cn

0� 5� 10� 15� 20� 25� 30� 35� 40� 45� 50�-1�

-0.5�

0�

0.5�

1�

harmonic number�Imag

inar

y pa

rt o

f Fou

rier�

coe

ffici

ent,

cn�

Page 6: Discrete Fourier Transform

O3 6

© w t norris O3 DFT & Matlab, 06 December 2002

Cosine waves at a frequency that is not a multiple of the fundamental.

y = cos(32·2 t) with 100 samples over 0 < t < 2π. The samples wave is shown below withthe reconstituted wave below. The variability arises because of the sampling and is a sortof aliasing–

0� 1� 2� 3� 4� 5� 6� 7�-1�

-0.5�

0�

0.5�

1�

time, secs�

Val

ue�

Original function at time points�

0� 1� 2� 3� 4� 5� 6� 7�-1�

-0.5�

0�

0.5�

1�

time, secs�

Val

ue�

Reconstituted IDFT at time points�

This is brought out in the DFT where a spread of frequencies is shown. This is calledleakage. There also appear some imaginary components.

0 10 20 30 40 50

0

10

20

30

harmonic number

0 10 20 30 40 50

0

20

40

harmonic number

DFT

Page 7: Discrete Fourier Transform

O3 7

© w t norris O3 DFT & Matlab, 06 December 2002

The magnitude and phase plots for y = cos(32·2 t + 1) are below. The phase lag givessome sine components and so the phase change.

0 10 20 30 40 500

20

40

harmonic number

Fourier analysis, magnitude and phase

0 10 20 30 40 50

-100

0

100

harmonic number

The tidiness of the phase variation is notable. This is because the signal contributes in asmall but not too small way to each harmonic.

Page 8: Discrete Fourier Transform

O3 8

© w t norris O3 DFT & Matlab, 06 December 2002

Phase shift; y = cos(25 t +1)

The single frequency is picked out clearly in both real and imaginary parts

cos(25t + 1) = cos(25t) cos(1) – sin(25t) sin(1)

As real reflects a cosine and imaginary a sine wave we get both in this case.

0� 5� 10� 15� 20� 25� 30� 35� 40� 45� 50�-50�

0�

50�

harmonic number�

Rea

l par

t of F

ourie

r� c

oeffi

cien

t, cn

� First half of DFT�

0� 5� 10� 15� 20� 25� 30� 35� 40� 45� 50�

0�

5�

10�

15�

20�

harmonic number�

Imag

inar

y pa

rt o

f Fou

rier�

coe

ffici

ent,

cn�

Aliasing;

y = cos(72t+1) with 100 sample points over a time interval of 2π.

The DFT is shown below displaying a very apparent 28 th harmonic. This is illusory.

0� 5� 10� 15� 20� 25� 30� 35� 40� 45� 50�-50�

0�

50�

harmonic number�

Rea

l par

t of F

ourie

r� c

oeffi

cien

t, cn

0� 5� 10� 15� 20� 25� 30� 35� 40� 45� 50�

-20�

-15�

-10�

-5�

0�

harmonic number�

Imag

inar

y pa

rt o

f Fou

rier�

coe

ffici

ent,

cn�

Page 9: Discrete Fourier Transform

O3 9

© w t norris O3 DFT & Matlab, 06 December 2002

The 100 Hz sampled wave of cos(72t + 1) is ( over a time interval of 2π)

0� 1� 2� 3� 4� 5� 6� 7�-1�

-0.5�

0�

0.5�

1�V

alu

e�

Time

The difference between 72 and 50 is 22 and the alias frequency is (50–22) =28

The full 72 Hz wave is quite regular

0� 1� 2� 3� 4� 5� 6�

-1�

0�

1�

time, secs�

Val

ue�

We can look also at some of the 72.2 Hz curve with 100 Hz sampling marked by the dots.The 28 Hz pattern in the dots can be seen

0.05� 0.1� 0.15� 0.2� 0.25�

-1�

1�

t

Magnitude

Page 10: Discrete Fourier Transform

O3 10

© w t norris O3 DFT & Matlab, 06 December 2002

For a signal of 172 Hz we equally get an alias.

0 10 20 30 40 500

20

40

harmonic number

DFT

0 10 20 30 40 50-1

-0.5

0

0.5

1

harmonic number

Again, this time by eye, one sees the lower frequency displayed by the samples shown asdots superposed on a more accurate plot of this 172.2 Hz wave

0.02� 0.04� 0.06� 0.08� 0.1� 0.12�

-1�

1�

t

Magnitude

Sampling twice a cycle may not be enough. Below are a sine and a cosine samples twice acycle at the same times. The sine wave would appear as zero. The Nyquist criterion is thatthere should be more than two samples a cycle and even that is quantitatively dubious.

0.2�0.4�0.6�0.8� 1� 1.2�1.4�

-1�

1�

t t

0.2�0.4�0.6�0.8� 1� 1.2�1.4�

-1�

1�

Magnitude Magnitude

Page 11: Discrete Fourier Transform

O3 11

© w t norris O3 DFT & Matlab, 06 December 2002

MATLAB function fast(fun) too make plots of DFTs with 100 point sampling of thefunction fun.

function fast(fun)% A function to demonstrate fast Fourier transforms% with 100 sample points over a period 2π.%% "fun"is a function of t defined outside the function "fast".% For example in the Command window set%% y='3*cos(4.2*t+2) + t^2'%% The function must be in quotation marks and it must be a function of t.% Then running fast(y) will produce four panes of graphs as output.% There are six panels of graphs:–%% Figure 1. A stem plot of the whole of the DFT one plot for the reals and one forthe imaginaries which shows the symmetry , a Tyger plot.% Figure 2. The magnitude or absolute value, |cn| and the phase.% Figure 3. The first 50 values which contains all the information.% Figure 4. The magnitude or absolute value, |cn| and the phase just for the first 50.% Figure 5. A plot of (i) the original 100 samples, (ii) the real part of the IDFT and(iii) the imaginary part of the DFT; note the ordinate scale.% Figure 6. A plot of the original function samples at 1000 points.%%------------------------------------------------------------------------------%------------------------------------------------------------------------------%% METHOD%% First define a set of 100 sample points running from 0 to 99.%------------------------------------------------------------------------------%t=linspace(0,2*pi-2*pi/100,100);%------------------------------------------------------------------------------% Evaluate the function at each sample point.%------------------------------------------------------------------------------y=eval(fun);%------------------------------------------------------------------------------% Find the DFT%------------------------------------------------------------------------------ft=fft(y);%------------------------------------------------------------------------------% Plot the result and observe the symmetry.%------------------------------------------------------------------------------figure(1),subplot(2,1,1);stem([0:99],real(ft));xlabel('harmonic number');ylabel('Real part of Fourier coefficient, cn');title('Full DFT to show Symmetry - a Tyger plot')%------------------------------------------------------------------------------% The next line prevents% i) any stems reaching to the top of the graph box% ii) a plot of rounding errors if all the values are close to zero% and% (iii) for the zero or d.c. point to% not be drawn on the ordinate axis.%------------------------------------------------------------------------------limep=max(1,1.1*max(real(ft)));

Page 12: Discrete Fourier Transform

O3 12

© w t norris O3 DFT & Matlab, 06 December 2002

limem=max(1,abs(1.1*min(real(ft))));axis([-2 101 -limem limep])%------------------------------------------------------------------------------% The imaginary part%------------------------------------------------------------------------------subplot(2,1,2);stem([0:99],imag(ft));xlabel('harmonic number');ylabel('Imaginary part of Fourier coefficient, cn');limep=max(1,1.1*max(imag(ft)));limem=max(1,abs(1.1*min(imag(ft))));axis([-2 101 -limem limep])%------------------------------------------------------------------------------% Now look at the magnitude and the phase in figure 2.% First the absolute magnitude%------------------------------------------------------------------------------figure(2),subplot(2,1,1);stem([0:99],abs(ft));xlabel('harmonic number');ylabel('Magnitude of Fourier coefficient, cn');title('Fourier analysis, magnitude and phase')limep=max(1,1.1*max(abs(ft)));axis([-2 102 0 limep])%------------------------------------------------------------------------------% Then the phase%------------------------------------------------------------------------------figure(2),subplot(2,1,2);stem([0:99],180*angle(ft)/pi);xlabel('harmonic number');ylabel('Phase in degrees, cn');limep=max(1,1.1*max(180*angle(ft)/pi));limem=min(-10,1.1*min(180*angle(ft)/pi));axis([-2 102 limem limep])%------------------------------------------------------------------------------% Now just the first half of the DFT%------------------------------------------------------------------------------fts=ft([1:50]);%------------------------------------------------------------------------------% Plot the result for the real part% in the first graph of Figure 3.%------------------------------------------------------------------------------figure(3),subplot(2,1,1);stem([0:49],real(fts));xlabel('harmonic number');ylabel(['Real part of cn']);title('DFT')limep=max(1,1.1*max(real(fts)));limem=max(1,abs(1.1*min(real(fts))));axis([-2 51 -limem limep])%------------------------------------------------------------------------------% Repeat for the imaginary part% in the second graph of Figure 3.%------------------------------------------------------------------------------subplot(2,1,2);stem([0:49],imag(fts));xlabel('harmonic number');ylabel('Imaginary part of cn');limep=max(1,1.1*max(imag(fts)));limem=max(1,abs(1.1*min(imag(fts))));axis([-2 51 -limem limep])%------------------------------------------------------------------------------% Do magnitude and phase plots for the first half of the DFT% Figure 4.

Page 13: Discrete Fourier Transform

O3 13

© w t norris O3 DFT & Matlab, 06 December 2002

%------------------------------------------------------------------------------figure(4),subplot(2,1,1);stem([0:49],abs(fts));xlabel('harmonic number');ylabel('Magnitude cn');title('Fourier analysis, magnitude and phase')limep=max(1,1.1*max(abs(ft)));axis([-2 52 0 limep])%------------------------------------------------------------------------------% Then the phase%------------------------------------------------------------------------------figure(4),subplot(2,1,2);stem([0:49],180*angle(fts)/pi);xlabel('harmonic number');ylabel('Phase in degrees, cn');limep=max(1,1.1*max(180*angle(ft)/pi));limem=min(-10,1.1*min(180*angle(ft)/pi));axis([-2 52 limem limep])%------------------------------------------------------------------------------% Plot the original sampled function% in the first graph of Figure 5.%------------------------------------------------------------------------------figure(5),subplot(3,1,1);plot(t,y);xlabel('time');ylabel('Original');title('Original and reconstructed function at time points')%------------------------------------------------------------------------------% and the real part of the inverse DFT% for the second graph of Figure 4.%------------------------------------------------------------------------------subplot(3,1,2);plot(t,real(ifft(ft)));xlabel('time');ylabel('Reconstructed');%------------------------------------------------------------------------------% and to check, the imaginary part of the IDFT% for the third graph of Figure 4.%------------------------------------------------------------------------------subplot(3,1,3);plot(t,imag(ifft(ft)));xlabel('time, secs');ylabel('Imaginary part of reconstructed');%------------------------------------------------------------------------------% Plot the original function with more time points% in Figure 6.%------------------------------------------------------------------------------t=linspace(0,2*pi-2*pi/5000,5000);y=eval(fun);figure(6),plot(t,y)xlabel('time');ylabel('Value');title('Original function at 1000 time points')