# Comparing the FFT to numerical integration in Matlab

The result of calculating the fourier transform using numerical integration is:

the result of using Matlab's FFT is:

So where did I go wrong here? I know the FT of a Gaussian should be another Gaussian. Shouldn't they be the same? Furthermore I would have thought the

# Edit:

Following a suggestion using the fftshift function gets me to the following plot, but this looks very different to the original fft despite documentation stating that it just centres the result around zero frequency.

which is more resembling what I would expect.

omega = linspace(-30,30,1000);

t = linspace(-10,10,1000);
y = exp(-t.^2./2)/sqrt(2*pi);
Yft = fft(y);

% Pre-allocate results of integration.
Y = zeros(size(omega));

%Integrate.
for k=1:length(omega)
Y(k) = trapz(t, exp(-t.^2./2)/sqrt(2*pi).*exp(1j*omega(k).*t));
end

figure()
plot(omega, real(Y), omega, imag(Y));

figure()
plot(omega, real(Yft), omega, imag(Yft));

• To start with matlab puts frequency components in a somewhat non-intuitive way where the zero frequency components are at the edges. Use fftshift to swap it so the zero frequency components are centered. However, I think you have other problems than just this. Oct 14, 2014 at 9:57
• you should use fftshift on your second figure (which is created by the fft function) not the first. Oct 14, 2014 at 10:29

There are two reasons why you get different results. The first is that you define a vector t between $-10$ and $10$, but the fft command doesn't know that. So the data are interpreted as starting at $0$. This means the first difference is that the FFT computes the transform of a delayed Gaussian impulse, not of an impulse centered at time zero. This is why you get a non-zero imaginary part in the result of the FFT.
• @Jack: As pointed out by nivag, check out fftshift. To be honest, I never use it but it should do the job. Oct 14, 2014 at 10:49