Fourier Transforms in Matlab: 2 x10 Periodogram
Fourier Transforms in Matlab: 2 x10 Periodogram
Matlab has a number of great functions implementing the FFT. Lets start with some simple examples:
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
0.4
spot activity during the past 300 years. You likely know 1.4
Power
1
sunspots. 0.4
0.2
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:
11.0769
1
0.2
FFT!
0
tors.
10
5 5
0 0
>> [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.
2 >>F2 = fftshift(F)
>>imshow(log(abs(F2)),[-1 5]); colormap(jet); color-
1 bar
>> t=zeros(256);
>> t(122:132)=1;
>> 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
5-8 above. 0
0 50 100 150 200 250 300