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;
T_sample = 1/f_sample;
nLength = 200;
t = (0:nLength-1)*T_sample;
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.
0 Comments
Sign in to comment.