2

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

enter image description here

the result of using Matlab's FFT is:

enter image description here

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.

enter image description here

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(2pi).exp(1jomega(k).t)); end

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

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

Dipole
  • 255
  • 3
  • 11
  • 1
    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. – nivag Oct 14 '14 at 09:57
  • 1
    you should use fftshift on your second figure (which is created by the fft function) not the first. – nivag Oct 14 '14 at 10:29

1 Answers1

2

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.

The second problem is - as pointed out by nivag in a comment - that the first index of the result of the FFT corresponds to frequency zero, and the negative frequencies appear in the second half of the resulting vector.

Matt L.
  • 89,963
  • 9
  • 79
  • 179
  • Thanks, so how would I tell the fft that the samples start at negative time? – Dipole Oct 14 '14 at 10:33
  • @Jack: You can't. Since the FFT data are always interpreted as being periodic, you just need to shift the data such that the negative times appear at the end of the data vector. I.e., imagine your data as being continued periodically and just use the first full period starting at time zero instead of the period with center zero. – Matt L. Oct 14 '14 at 10:39
  • BTW, note that to get perfectly symmetric data you need an odd number of samples. – Matt L. Oct 14 '14 at 10:40
  • Great that actually makes sense. Does matlab have a fast way of doing this or do I need to manually slice my data array? – Dipole Oct 14 '14 at 10:44
  • @Jack: As pointed out by nivag, check out fftshift. To be honest, I never use it but it should do the job. – Matt L. Oct 14 '14 at 10:49
  • I had to use fftshift twice though to get the correct result. However I am still a bit puzzled by the normalisation of the result. Does the fft function insert a factor of $1/\sqrt{2\pi}$ because the results differ by that. Many thanks for your help! – Dipole Oct 14 '14 at 10:52
  • @Jack: You need fftshift twice because your time domain data are centered around zero (fist fftshift), and you want your frequency domain data also to be centered around zero (2nd fftshift). The scaling is equal to $1/T$ where $T$ is the sampling interval. – Matt L. Oct 14 '14 at 11:00
  • Hi Matt, I know this is an old answer but I have a question. You say the FFT interprets the data as starting at zero. Do you know if there is a way to show mathematically why fftshift does the job when you are using FFT to approximate the CTFT with time-centered data? I have asked a question about this recently, and tried to show it myself in there, but didn't manage to finish it yet: https://dsp.stackexchange.com/q/66716/38419 Any input you can provide would be great. – teeeeee Apr 26 '20 at 12:08