FFT2: Get the actual frequencies and amplitudes

28 views (last 30 days)
Hi guys, I have a problem concerning MATLAB's fft2 function. I am trying to understand the results and how to get the actual frequences and the connected amplitudes. In my code, I create a rect-like signal with the first five harmonics of a sine wave and sum them up. Then I put it through fft and, of course, get peaks at zero (dc part because I wanted no negative values), one, three, etc. After normalizing the output by dividing with the actual length of the signal, I also get the correct amplitudes.
Now the 2D case: I use the exact same signal, and create an image with the signal's luminance values. As it is an image now, I use fft2 because it is two dimensional. And here starts my problem: I cannot understand the magnitude of both amplitudes and frequencies. In my 1D example, the DC part is as big as the the first harmonic. That is not the case for the 2D one.
Here is my code for easy copy n paste:
clear; close all; clc;
f_sample = 200; % Sampling frequency
T_sample = 1/f_sample; % Sampling period
nLength = 200; % Length of signal
t = (0:nLength-1)*T_sample; % Time vector
f = 5;
nPicHeight = 40;
s = 1 + sin(2*pi*1*f*t) + sin(2*pi*3*f*t)/3 + sin(2*pi*5*f*t)/5 + sin(2*pi*7*f*t)/7 + sin(2*pi*9*f*t)/9;
fig1 = figure;
set(fig1,'Position',[1960, 620, 800, 400]);
plot(t, s);
S = fft(s);
P2 = abs(S/nLength);
P1 = P2(1:nLength/2+1);
P1(2:end-1) = 2*P1(2:end-1);
fig2 = figure;
set(fig2,'Position',[1960+900, 620, 800, 400]);
stem(P1);
i = repmat(s, [nPicHeight 1]);
imwrite(i, 'sinus.png');
i = imread('sinus.png');
fig3 = figure;
set(fig3,'Position',[1960+000, 60, 800, 400]);
imagesc(i);
colormap(gray);
I = fft2(i);
I = abs(I/(nPicHeight*nLength*255));
I = fftshift(I);
fig4 = figure;
set(fig4,'Position',[1960+900, 60, 800, 400]);
imagesc(I);
colorbar;
I save the created image to a file, because the purpose is going the read actual images into the script and have them evaluated. But first, I need to understand what I have to expect from real files by understanding artificial ones.
Thanks.

Answers (1)

Zhao Wang
Zhao Wang on 27 Sep 2016
I understand that you use the "fft2" function to analyze an artificial image. The image is created by summing up the first five harmonics of a sine wave and expand it to the image height. The result shows difference in magnitudes between the DC component and the first harmonic. In addition, the result seems not to match the output from directly using the "fft" function on the 1D sine wave.
In the 1D example, you actually scale the result of the "fft" function to match the magnitudes of the DC component and the first harmonic, using:
>> P1(2:end-1) = 2*P1(2:end-1);
For the 1D example, you can directly display the result of the "fft" function and shift it using the "fftshift" function. The result should be very similar to the result of the 2D situation. The results from the "fft" function and the "fft2" function actually match. This is because the command
>> fft2(X)
is equivalent to
>> fft(fft(X).').'
For more details about the "fft2" function, refer to the following page:
There is a good column by Steve talking about 'Plotting the DTFT using the output of fft'. Refer the column through the following link:

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!