http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363
MATLAB Central Newsreader  Confusion over calculating Fourier transforms
Feed for thread: Confusion over calculating Fourier transforms
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 02 Dec 2009 09:30:21 +0000
Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699077
Mat Hunt
Hi,<br>
<br>
I need to calculate the Fouier transform of some rather complicated functions. Naively, I would do the following (to compute the Fourier transform of a Gaussian say):<br>
<br>
>x=3:0.001:3;<br>
>y=exp(x.^2);<br>
>f=abs(fft(y));<br>
>plot(f);<br>
<br>
I just set something which is completely wrong, what am I doing wrong here?<br>
<br>
Mat

Wed, 02 Dec 2009 10:08:04 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699085
TideMan
On Dec 2, 10:30 pm, "Mat Hunt" <hunt_...@hotmail.com> wrote:<br>
> Hi,<br>
><br>
> I need to calculate the Fouier transform of some rather complicated functions. Naively, I would do the following (to compute the Fourier transform of a Gaussian say):<br>
><br>
> >x=3:0.001:3;<br>
> >y=exp(x.^2);<br>
> >f=abs(fft(y));<br>
> >plot(f);<br>
><br>
> I just set something which is completely wrong, what am I doing wrong here?<br>
><br>
> Mat<br>
<br>
Did you read the response when you posted this exact same question 20<br>
hours ago?<br>
<a href="http://groups.google.com/group/comp.softsys.matlab/browse_thread/thread/6d92edc013ea98bf#">http://groups.google.com/group/comp.softsys.matlab/browse_thread/thread/6d92edc013ea98bf#</a><br>
<br>
Did you try what Matt J suggested?<br>
And why did you start a new thread, rather than continue with the old<br>
one?

Wed, 02 Dec 2009 10:27:20 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699088
Rune Allnor
On 2 Des, 10:30, "Mat Hunt" <hunt_...@hotmail.com> wrote:<br>
> Hi,<br>
><br>
> I need to calculate the Fouier transform of some rather complicated functions. Naively, I would do the following (to compute the Fourier transform of a Gaussian say):<br>
><br>
> >x=3:0.001:3;<br>
> >y=exp(x.^2);<br>
> >f=abs(fft(y));<br>
> >plot(f);<br>
><br>
> I just set something which is completely wrong, what am I doing wrong here?<br>
<br>
The computed result is correct. The FFT function might not<br>
compute what you think it does, though. Read up on the various<br>
flavours of the Fourier transform in general (there are at least<br>
four of them), and the DFT in particular.<br>
<br>
Rune

Wed, 02 Dec 2009 14:02:20 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699124
Mat Hunt
Hi,<br>
<br>
I couldn't find the previous post, my apologies.<br>
<br>
To give you an idea where my problem comes from. I am calculating the shape of a free surface of a train of waves, i wrote it as a fourier transform, \eta =\int_{\mathbb{R}}A(k)\exp (ikx)dk. I have calculated the function A(k) which is rather nasty, so i was thinking about using matlab to compute it numerically. <br>
<br>
I was using the Gaussian to get the feel of things with matlab.<br>
<br>
Mat

Wed, 02 Dec 2009 15:51:03 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699165
Matt J
"Mat Hunt" <hunt_mat@hotmail.com> wrote in message <hf5c3c$1f5$1@fred.mathworks.com>...<br>
> Hi,<br>
> <br>
> I need to calculate the Fouier transform of some rather complicated functions. Naively, I would do the following (to compute the Fourier transform of a Gaussian say):<br>
> <br>
> >x=3:0.001:3;<br>
> >y=exp(x.^2);<br>
> >f=abs(fft(y));<br>
> >plot(f);<br>
> <br>
> I just set something which is completely wrong, what am I doing wrong here?<br>
<br>
You need to do plot(fftshift(f)) to see the Gaussian as a centered peak (it will be very narrow, so you'll need to zoom in). Right now, you're seeing a circulant shift of this peak.

Wed, 02 Dec 2009 16:57:01 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699191
Mat Hunt
"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hf62d6$k7a$1@fred.mathworks.com>...<br>
> "Mat Hunt" <hunt_mat@hotmail.com> wrote in message <hf5c3c$1f5$1@fred.mathworks.com>...<br>
> > Hi,<br>
> > <br>
> > I need to calculate the Fouier transform of some rather complicated functions. Naively, I would do the following (to compute the Fourier transform of a Gaussian say):<br>
> > <br>
> > >x=3:0.001:3;<br>
> > >y=exp(x.^2);<br>
> > >f=abs(fft(y));<br>
> > >plot(f);<br>
> > <br>
> > I just set something which is completely wrong, what am I doing wrong here?<br>
> <br>
> You need to do plot(fftshift(f)) to see the Gaussian as a centered peak (it will be very narrow, so you'll need to zoom in). Right now, you're seeing a circulant shift of this peak.<br>
<br>
Okay, I have done this BUT it still doesn't account for getting the amplitude incorrect, the height should be less than 10 but the graph shows that it is in the thousands. Something has gone wrong here and I am not too sure what. As I said before, I want to plot the transform \hat{f}(k) as a function of k. So I need to find k also.

Wed, 02 Dec 2009 18:12:03 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699217
Matt J
"Mat Hunt" <hunt_mat@hotmail.com> wrote in message <hf668t$pak$1@fred.mathworks.com>...<br>
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hf62d6$k7a$1@fred.mathworks.com>...<br>
> > "Mat Hunt" <hunt_mat@hotmail.com> wrote in message <hf5c3c$1f5$1@fred.mathworks.com>...<br>
> > > Hi,<br>
> > > <br>
> > > I need to calculate the Fouier transform of some rather complicated functions. Naively, I would do the following (to compute the Fourier transform of a Gaussian say):<br>
> > > <br>
> > > >x=3:0.001:3;<br>
> > > >y=exp(x.^2);<br>
> > > >f=abs(fft(y));<br>
> > > >plot(f);<br>
> > > <br>
> > > I just set something which is completely wrong, what am I doing wrong here?<br>
> > <br>
> > You need to do plot(fftshift(f)) to see the Gaussian as a centered peak (it will be very narrow, so you'll need to zoom in). Right now, you're seeing a circulant shift of this peak.<br>
> <br>
> Okay, I have done this BUT it still doesn't account for getting the amplitude incorrect, the height should be less than 10 but the graph shows that it is in the thousands. Something has gone wrong here and I am not too sure what. As I said before, I want to plot the transform \hat{f}(k) as a function of k. So I need to find k also. <br>
=====================<br>
<br>
As I mention in the duplicate post, the height is not what you expect because the fft is a sum whereas the continuous Fourier Transform is an integral. To approximate integrals, discrete sums must be multiplied by sampling intervals (giving you a Riemman sum).<br>
<br>
You also need to be aware of what formula MATLAB uses for the fft (see "help fft"). Additional scale factors of 1/sqrt(pi) might be necessary according to whether you follow the Fourier transform conventions of physicists or of engineers.

Thu, 03 Dec 2009 02:02:04 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699339
Sadik
Right, just like Matt J says.<br>
<br>
If you look at the formula, you will realize that your max in the spectrum [which occurs at DC] is given mathematically by:<br>
<br>
maxSpectrum = X(k=0) = sum(exp(x.^2))<br>
<br>
where x is just like you have defined at the very beginning [3:0.001:3].<br>
<br>
Look at this sum and compare it to<br>
<br>
max(f)<br>
<br>
You will see what I mean.

Thu, 03 Dec 2009 11:28:02 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699443
Mat Hunt
It's been a while since I last did Fourier analysis at university as a pure maths course and coming back now is a bit hard. I define the transform as:<br>
<br>
\hat{f}(\xi)=\int_{\infty}^{\infty}f(x)e^{i\xi x}dx<br>
<br>
Does Matlab define it differently? if so what? I have had a look as all it gives is the discrete Riemann sum and I am not too sure about what they're doing.<br>
<br>
I searched online and I came across the following example:<br>
<br>
N=128;<br>
t=linspace(0,3,N);<br>
f=2*exp(3t)<br>
Ts=t(2)t(1);<br>
Ws=2*pi/Ts;<br>
F=fft(f);<br>
Fc=fftshift(F)*Ts;<br>
<br>
W=Ws*(N/2:(N/2)1)/N;<br>
Fa=2./(3+W*i);<br>
plot(W,abs(Fa),W,abs(Fc),'.');<br>
xlabel('frequency, Rad/s');<br>
ylabel('F(\omega)');<br>
title('Fourier Transform Approximation');<br>
<br>
This worked quite well, but when I changed f to f=exp(t.^2) and likewise the Fourier transform Fa to Fa=sqrt(pi)*exp(0.25*W.^2), the comparison is very very bad.<br>
<br>
Again it seem very odd to me.<br>
<br>
Mat

Thu, 03 Dec 2009 11:35:24 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699445
Sadik
The formula is here:<br>
<br>
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fft.shtml">http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fft.shtml</a><br>
<br>
You should substitute k = 1 in X(k).

Thu, 03 Dec 2009 11:51:03 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699450
Greg Heath
On Dec 2, 4:30 am, "Mat Hunt" <hunt_...@hotmail.com> wrote:<br>
> Hi,<br>
><br>
> I need to calculate the Fouier transform of some rather complicated functions. Naively, I would do the following (to compute the Fourier transform of a Gaussian say):<br>
><br>
> >x=3:0.001:3;<br>
> >y=exp(x.^2);<br>
> >f=abs(fft(y));<br>
> >plot(f);<br>
><br>
> I just set something which is completely wrong, what am I doing wrong here?<br>
<br>
1. Matlab defines the FFT over the nonegative domain<br>
<br>
x = dx*(0:N1) = 0:dx:Xdx = linspace(0,Xdx,N)<br>
<br>
where X = N*dx.<br>
<br>
2. The proper vertical scaling is given by<br>
<br>
Y = fft(y)/N;<br>
<br>
The best way to verify this is via a sinusoid.<br>
<br>
3. An alternate to using ifftshift(x) is to center y via<br>
<br>
y = exp((x3).^2) for 0 <= x <=6<br>
<br>
Hope this helps.<br>
<br>
Greg

Thu, 03 Dec 2009 16:20:07 +0000
Re: Confusion over calculating Fourier transforms
http://www.mathworks.com/matlabcentral/newsreader/view_thread/267363#699558
Matt J
"Mat Hunt" <hunt_mat@hotmail.com> wrote in message <hf87c2$3gp$1@fred.mathworks.com>...<br>
> It's been a while since I last did Fourier analysis at university as a pure maths course and coming back now is a bit hard. I define the transform as:<br>
> <br>
> \hat{f}(\xi)=\int_{\infty}^{\infty}f(x)e^{i\xi x}dx<br>
> <br>
<br>
<br>
Then you need be scaling the output of the fft by dx. Also, you need to plot the result on the following axis<br>
<br>
Axis= ceil((N1)/2):N1ceil((N1)/2)*2*pi/(N*dx)<br>
<br>
To better understand wha't going on, it would be a helpful exercise for you to discretize your above integral by making the substitutions<br>
<br>
xi=k*(2*pi/(N*dx)) <br>
x=n*dx <br>
<br>
where k and n are discrete variables ranging over<br>
<br>
Axis= ceil((N1)/2):N1ceil((N1)/2)<br>
<br>
You should find that the integral discretizes to the following MATLAB operations:<br>
<br>
\hat{F}=ifftshift( fft(fftshift(F) ) )*dx<br>
<br>
where<br>
<br>
F=f(Axis*dx)<br>
<br>
\hat{F}=\hat{f}(2*pi*Axis/(N*dx))