0
$\begingroup$

I've been trying to find the FT of a more complicated function, but it seems my method is wrong. To check, I tried to calculate the FT of a Gaussian numerically. This is what I did:

tabexp = Table[Exp[-x^2], {x, 0, 100, 0.1}];
ListPlot[Abs[Fourier[tabexp]]]

This gives me a weird plot: enter image description here

What exactly am I doing wrong here? TIA!

$\endgroup$
6
  • 4
    $\begingroup$ I put some notes on using Fourier here. Does that help? $\endgroup$
    – Hugh
    Apr 12, 2020 at 21:16
  • 1
    $\begingroup$ For the equation you entered, this looks correct. What were you expecting it to look like? $\endgroup$
    – MassDefect
    Apr 13, 2020 at 3:18
  • $\begingroup$ @Hugh Thank you so much! That does help. $\endgroup$ Apr 13, 2020 at 23:48
  • $\begingroup$ @MassDefect Maybe I'm understanding the representation wrong. I was expecting a Gaussian because the FT of Exo[-x^{2}] is also a Gaussian. $\endgroup$ Apr 13, 2020 at 23:49
  • $\begingroup$ Another related post is this: mathematica.stackexchange.com/q/7016/1871 $\endgroup$
    – xzczd
    Apr 14, 2020 at 5:40

1 Answer 1

3
$\begingroup$

Mathematica performs its Fourier transform such that there are no negative frequencies. If you're coming from another computing language, you're probably more used to seeing the transform centred at 0 frequency. If you want that functionality, I use the fftshift function from this answer and it works really well.

The other potential issue is that you're only generating half of a Gaussian. I'm not sure if that's what you intended or not but it does give you a slightly different answer.

fftshift[dat_?ArrayQ, k : (_Integer?Positive | All) : All] := 
 Module[{dims = Dimensions[dat]}, 
  RotateRight[dat, 
   If[k === All, Quotient[dims, 2], 
    Quotient[dims[[k]], 2] UnitVector[Length[dims], k]]]]

tabexp = Table[Exp[-x^2], {x, 0, 100, 0.1}];
tabexp2 = Table[Exp[-(x - 50)^2], {x, 0, 100, 0.1}];

ListLinePlot[{
  tabexp,
  tabexp2
  },
 PlotRange -> Full
 ]

ListLinePlot[{
  fftshift[Abs[Fourier[tabexp]]^2],
  fftshift[Abs[Fourier[tabexp2]]^2]
  },
 PlotRange -> Full
 ]

This is what the plots of the Gaussians look like with your tabexp in blue, and a centred Gaussian in yellow:

Plot of Gaussian and centred Gaussian.

Plots of the Fourier transforms of the above Gaussians, with the FT of tabexp in blue and tabexp2 in yellow:

Plot of FT of Gaussian and centred Gaussian.

Oh, and I guess I squared the Abs. I just noticed you didn't in your code, but I'm too lazy to fix it now. One other thing to note, is that Fourier doesn't allow you to include any x-data. You have to provide it with the y-data and then add in the frequency data later if you want. The x-axes here range from 1 to 1000 because there are 1000 data points.

EDIT 01:

Manipulate[
  tabexp = Table[Exp[-x^2], {x, start, start + 100, 0.1}];
  ListLinePlot[{
      tabexp,
      fftshift[Abs[Fourier[tabexp]]^2]
    },
    PlotRange -> Full
  ],
  {{start, 0}, -50, 0, 1}
]

Animation of gaussian plot.

Here the blue curve is that Gaussian, and the yellow curve is the power of the shifted Fourier transform. Essentially as soon as the full Gaussian is displayed (start < 2 or so) then the FT doesn't change anymore.

Instead of starting your sampling at -50, you could also sample the equation Exp[-(x - 50)^2] from 0 to 100, they would be exactly equivalent in the end.

Manipulate is one of my favourite tools in Mathematica. If you're not familiar with it, I definitely recommend checking it out! It's helped me understand a lot about Fourier transforms, plotting, filtering, and all sorts of other things.

$\endgroup$
3
  • $\begingroup$ Ah. Thank you so much for clearing this up! What would be the difference if, instead I tabulated my data for Exp[-x^{2}] starting from a non-zero value instead of 0? Does that make sense? $\endgroup$ Apr 15, 2020 at 13:21
  • $\begingroup$ @123infinity Yes, you can change your sampling if you want to have the Gaussian centred. I've updated my answer to include some more information on that. $\endgroup$
    – MassDefect
    Apr 15, 2020 at 20:15
  • $\begingroup$ This has been really helpful. Thank you so much! :) $\endgroup$ Apr 16, 2020 at 12:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.