Mathematica Fourier, Code For Fourier Transform
Mathematica Fourier, Code For Fourier Transform
young.1022@osu.edu
The Ohio State University
Department of Physics
MyTutorial.nb
Out[139]=
Re@EHtLD
0.6
0.4
0.2
0.0
-0.2
-10
-5
0
t
10
Out[140]=
Im@EHtLD
-0.2
-0.4
-0.6
-10
-5
0
t
10
MyTutorial.nb
Out[141]=
Intensity
0.8
0.6
0.4
0.2
0.0
-10
-5
0
t
10
Dt = Hdata@@2DD@@1DD - data@@1DD@@1DDL
0.1
Compute the Fourier transform E() using the built-in function. Important! The sample data array is
ordered from negative times to positive times. However, Mathematica requires that the array
passed to the Fourier function be ordered starting with the t=0 element, ascending to positive time
elements, then negative time elements. To account for this, we run the RotateLeft command on our
data array. The array that results after taking the transform is ordered starting from zero-frequency,
increasing to the maximum frequency; then it jumps to the most negative frequency, and increases
to zero. Therefore, when we want to plot the result of the transform in frequency space, we need to
shift the array again after the transform. We use RotateRight (by the same amount) before plotting,
so that our data is ordered from negative frequencies to positive frequencies, with the zero
frequency component in the middle.
In[144]:=
D =
n Dt
Out[146]=
0.0390625
Create a list of frequency-axis values (in angular Hz) to use in the plot of E().
MyTutorial.nb
Create a list of frequency-axis values (in angular Hz) to use in the plot of E().
In[147]:=
Out[148]=
Re@EHLD
10
-4
-2
Out[149]=
Im@EHLD
-5
-10
-4
-2
MyTutorial.nb
Out[150]=
Intensity
150
100
50
0
-4
-2
phase = Arg@FFTValuesD;
phase = FoldList@Round@H1 - 2L H2 PiLD 2 Pi + 2 &, First@phaseD, Rest@phaseDD;
ListPlot@Table@8frequencyValues@@iDD, phase@@iDD<, 8i, 1, n<D,
Joined True, PlotRange All, FrameLabel 8"", ""<, Frame TrueD
-5
-10
Out[153]=
-15
-20
-25
-4
-2
MyTutorial.nb
originalFFTValues =
InverseFourier@RotateLeft@FFTValues, n 2 - 1D, FourierParameters 81, - 1<D;
Plot the intensity versus time (remembering to shift the array before plotting...you should be good
at this by now)
In[155]:=
0.8
Intensity
Out[156]=
0.6
0.4
0.2
0.0
-10
0
t
-5
10
As expected, we get the same pulse back as before, since we did not apply any transfer function to
it in the frequency domain. This is a good test to see if your software is working.
4a
Out[157]=
In[158]:=
4a
InverseFourierTransformB
Out[158]=
-a t
You can use these methods to create discrete data sets from a given analytic expression, for
example, by sampling at specified times and frequencies.
MyTutorial.nb
You can use these methods to create discrete data sets from a given analytic expression, for
example, by sampling at specified times and frequencies.
Possible Issues
Here are some common issues you might run into when using Mathematica, as well as general
things to keep in mind when performing Fourier transforms.
When working with analytic functions, be aware of the sign convention used for plane waves traveling to
the left and those traveling to the right.
You are free to choose the sign convention you prefer when distinguishing between forward and reverse
transforms, as long as you are consistent. Mathematica' s convention is different from that used in class,
but you can force Mathematica to use a specified convention by setting the FourierParameters property in
the Fourier and InverseFourier functions. I have set my transforms to use the sign convention used in
class. See the documentation for more information.
When generating sample data to work with, make sure your sample window is large enough in order to
accomodate the entire signal of interest. This may involve using more or less data points, or setting
different temporal and spectral spacings.
It is easy to get the array rotations mixed up when preparing your array for forward and reverse
transforms, and in plotting. However, it is easy to tell if your array ordering is incorrect; your data will
appear to be discontinuous. Perform another rotation and try plotting it again.
In order to avoid array rotation problems when you make a change in your notebook, such as in a transfer
function, it is useful to re-evaluate the entire notebook from start to end in order to properly reset your
arrays.
Make sure you are using frequency values or angular frequency values, as appropriate.
It is often convenient to work only with the pulse envelope. Therefore, be sure to distinguish between
instantaneous and carrier frequencies in your calculations.
Check the scale of your units to be sure everything matches up! (e.g. Hz vs. THz)
Summary
With these tools, you should now be able to perform forward and reverse Fourier transforms of
discrete and continuous data sets in Mathematica. You have seen that the Table function is one of
the most useful functions in Mathematica to create and manipulate arrays. You have also seen
some of the ways that you can visualize your data, by way of examining the magnitudes and
phases of complex-valued fields, as well as their frequency spectra. The best way to learn about
Mathematica's capabilities is to simply start using the software and try as many examples as
possible. The documentation is quite helpful when you need a description of what certain functions
do, what their assumptions are, and what parameters they can take. Most manual pages give you
examples to work with that you can modify for your own purposes. The web is also a great
resource for finding examples. Have fun transfoming, please email me if you find any errors in this
document, and remember, transforms are just robots in disguise!