0% found this document useful (0 votes)
47 views

Fourier Transforms in Matlab: 2 x10 Periodogram

The document discusses Fourier transforms in MATLAB. Some key points: 1) MATLAB has functions like fft() that implement the fast Fourier transform (FFT) to compute Fourier transforms. 2) A simple example computes the FFT of a 1D array, showing it returns a complex vector with the zero-frequency term first followed by positive then negative frequencies. 3) The FFT can be used for data analysis, like finding the approximate 11-year period of the sunspot cycle from 300 years of sunspot data.

Uploaded by

maharditio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Fourier Transforms in Matlab: 2 x10 Periodogram

The document discusses Fourier transforms in MATLAB. Some key points: 1) MATLAB has functions like fft() that implement the fast Fourier transform (FFT) to compute Fourier transforms. 2) A simple example computes the FFT of a 1D array, showing it returns a complex vector with the zero-frequency term first followed by positive then negative frequencies. 3) The FFT can be used for data analysis, like finding the approximate 11-year period of the sunspot cycle from 300 years of sunspot data.

Uploaded by

maharditio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Fourier Transforms in Matlab

Matlab has a number of great functions implementing the FFT. Lets start with some simple examples:

>> x=[4 3 7 -9 1 0 0 0];


>> y=fft(x)

y=

6.0000
11.4853 - 2.7574i
-2.0000 -12.0000i
-5.4853 +11.2426i
18.0000
-5.4853 -11.2426i
-2.0000 +12.0000i
11.4853 + 2.7574i

The first element is real and is the zero-frequency part of the transform. In particular adding together the ele-
ments of x:

4+3+7-9+1+0+0+0 = 6.

Since the elements of x are spaced by ones, the sum is simply the integral of the input function or the area under
the input function. Dividing by the number of samples yields the average of the input signal or its DC value.
The next three entries are the positive frequencies in the transform while 18.0 is the transform value at the Ny-
quist frequency. fN=1/(2Dt) = 0.5 Hz (given Dt=1). The final three elements are the negative frequency values.
If you compare the negative and positive frequency values, you see that the amplitudes are the same, but the
actual values differ in the sign of the imaginary value. y(w)=y(-w)* where * indicates the complex conjugate.
That is, the transform is Hermitian as indicated earlier in the notes. The Fourier Transform of a real function is
Hermitian. Lets see how this can be packed in a more rational way:

z=fftshift(y)

z=
7
x 10 Periodogram
2

18.0000 1.8

-5.4853 -11.2426i
-2.0000 +12.0000i
1.6

11.4853 + 2.7574i
1.4

6.0000 1.2

11.4853 - 2.7574i 1

-2.0000 -12.0000i 0.8

-5.4853 +11.2426i 0.6

0.4

Now the DC value is in the center with the negative


frequencies lying above the Nyquist until the DC value
0.2

is reached. The remaining frequencies are positive with


0
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5
cycles/year

the complex conjugate values of the negative frequen-


cies. Figure 1: Sunspot time series.
Fourier Transforms are helpful not only in solving x 10
7

partial differential equations, but can be very helpful in


2

data analysis. A good example, with a data table built 1.8

into Matlab itself, has to do with the variations in sun- 1.6

spot activity during the past 300 years. You likely know 1.4

that sunspot activity occurs in cycles with a period of 1.2

approximately 11 years. Its a fairly significant outcome

Power
1

of the processes that drive the magnetohydrodynamics


of the Sun. Astronomers have tabulated a Wolfer num-
0.8

ber that is representative of both the number and size of


0.6

sunspots. 0.4

0.2

>> load sunspot.dat 0


0 5 10 15 20 25 30 35 40

>> year=sunspot(:,1); Period(Years/Cycle)

>> wolfer=sunspot(:,2); Figure 2: Power spectrum for sunspots


>> plot(year,wolfer)
>> title(Sunspot Data)
>> xlabel(Year)
>> ylabel(Wolfer Number)

Matlab loads a dataset installed with Matlab called sunspot. The data in sunspot are arranged in two columns
with year in the first column and the Wolfer number in the second. The time series are shown in Figure 1. Now
take the FFT of the sunspot data.

>> Y=fft(wolfer);
>> N=length(Y);
>> Y(1)=[];
>> power=abs(Y(1:N/2)).^2;
>> nyquist=1/2;
>> freq=(1:N/2)/(N/2)*nyquist;
>> plot(freq,power), grid on
>> xlabel(cycles/year)
>> title(Periodogram)

The result is a complex vector, Y. The magnitude of Y squared is called the power and a plot of power versus
frequency is a periodogram. The first component of Y is removed (the DC component). The periodogram is
shown in Figure 1. A peak is readily seen in the signal at a frequency of 1/11 years. This would probably be
more obvious if the function was plotted in years/cycle (the inverse of the x-axis in Figure 1).

>> period=1./freq;
>> plot(period,power), axis([0 40 0 2e7]), grid on
>> ylabel(Power)
>> xlabel(Period(Years/Cycle))

The peak at 11 years is now quite obvious (Fig. 2). If we look for the peak in the power spectrum, we can deter-
mine the sunspot period more precisely:

>> [mp index] =max(power);


>> period(index)
ans =

11.0769
1

0.8 So the period, based on 300 years of data, is slightly


0.6 greater than 11 years! The utility of the FFT in data
analysis should be pretty obvious. An entire course can
be devoted to time series analysis and the use of the
0.4

0.2
FFT!
0

Just a quick note, dont miss the subtle use of .^ and ./


30
25
30
20
above to denote element-by-element operations on vec-
25
15 20
10 15

tors.
10
5 5
0 0

Figure 3: Gate function in 2-D. 2-D Fourier Transforms

Lets have a look at some two-dimensional Fourier Transforms now.

>> [x,y]=meshgrid(1:30);
>> Z=zeros(30,30);
>> Z(5:24,13:17)=1;
>> mesh(x,y,Z)
>> [x,y]=meshgrid(1:30);

This creates a 2-D gate function or box in Matlab with different horizontal dimensions in the x,y directions with
a value of 1 within the box. The function is plotted in Figure 3. The Matlab functions fft, fft2 and fftn imple-
ment the Fast Fourier Transform for computing the 1-D, 2-D and N-dimensional transforms respectively. The
inverse functions ifft, ifft2 and ifftn compute the
inverse transforms.

Note that the function is long in the y-direction and


short in the x-direction. This will affect the Fourier

Figure 5: Fourier transform at low resolution.


Figure 4: Top view of gate function.
transform views in the linerarly-related frequency or
wavenumber domain. Well plot this function first with another Matlab routine called imshow:
>>imshow(Z,InitialMagnification,fit) 5

This provides an alternative view of the box with


amplitude = 1 at the center of the 2-D field (Fig.4). 4
Now lets take the Fourier transform:
3
>>F=fft2(Z);
>>F2=log(abs(F));
>>imshow(F2,[-1 5],InitialMagnification,fit); 2
colormap(jet); colorbar
1
The Fourier Transform (Fig. 5) just computed is
quite low resolution and the zero component (or
DC) is still in the upper left corner instead of at 0
the center. Lets pad the Fourier Transform to 256
points in each direction and have another look. 1
Figure 6: Higher resolution Fourier Transform. The DC
>>F = fft2(W,256,256); component is still inappropriately located in the plot.

5 >>imshow(log(abs(F)),[-1 5]); colormap(jet); color-


bar
4
This version (Fig 6) has a much higher resolution
although we have not yet moved the zero frequency
3 amplitude to the center. Lets do that.

2 >>F2 = fftshift(F)
>>imshow(log(abs(F2)),[-1 5]); colormap(jet); color-
1 bar

Figure 7 now shows the 2-D spectrum in high reslu-


0
tion with the DC component in teh correct place.

1 Its possible to view the 2-D spectrum differently.


Figure 7: High resolution 2-D Fourier Transform with Figure 8 shows a mesh presentation of the Fourier
the DC component located in the center of the plot. Recall Transform of the 2-D gate function.
that the original gate had a long dimension in the y-direc-
tion and a short, localized dimension in the x-direction.
The frequency content in the x-direction decays less rap- >>mesh(log(abs(F2))
idly than in the y-direction. A localized object in the x or t
direction requires a broad spectrum and vice-versa. Recall from the lecture notes that a 1-D gate function
transforms into a sinc function of sin(px)/(px).
Lets look at a simple 1-D example. First create a gate function:

>> t=zeros(256);
>> t(122:132)=1;

Now take the Fourier Transform:

>> w=fft(t);
>> x=fftshift(w);

This gives us the properly arranged FFT. Now generate a gate function that is broader:

>> t(102:152)=1;
>> w1=fft(t);
>> x1=fftshift(w1);
>> plot(x,r,x1,b)

Figure 8: Mesh plot of Figure 7. Now its possible to see clearly the sinc functions in both
the x & y directions rather than simply color graphics. All of these figures are itegrated into
the notes in encapsulated postcript format (eps) so the rendering on the page is a bit spotty.
Looks best when printed on a good color printer.

Now plot the narrow gate function in red followed by the broader function in blue:

>> plot(abs(x),r)
Figure 9: Two Fourier
>> hold on
Transforms of gate func-
60

>> plot(abs(x1),b)
tions. The blue function 50

correspondes to the broad- 40

er gate function resulting


in a more localized sinc 30

function in the frequency 20

domain. This is the same


effect as noted in Figures
10

5-8 above. 0
0 50 100 150 200 250 300

You might also like