# Using mathematica to take fourier transform of data

I'm trying to use Mathematica to take a discrete Fourier transform of 68 data points (0 to 67) to perform further analysis.

My data points, when plotted, look like this:

Pretty straightforward.

Now, I need to take the DFT. These points are in a variable as

t={0, 0, 0.0147059, 0.0294118, 0.0441176, 0.0588235, 0.0735294,
0.0882353, 0.102941, 0.0441176, -0.0147059, -0.0735294, -0.191176,
-0.308824, -0.426471, -0.544118, -0.691176, -0.838235, -0.985294,
-1.13235, -1.27941, -1.42647, -1.57353, -1.72059, -1.86765, -1.01471,
-1.16176, -1.30882, -1.44118, -1.57353, -1.70588, -1.83824, -1.97059,
-2.10294, -2.23529, -2.36765, -2.5, -2.63235, -2.76471, -2.89706,
-3.01471, -3.13235, -3.25, -3.36765, -3.48529, -3.60294, -3.72059,
-3.83824, -3.95588, -4.07353, -4.19118, -4.30882, -4.42647, -6.54412,
-6.66176, -6.77941, -6.89706, -7.01471, -7.13235, -7.25, -7.36765,
-7.48529, -7.60294, -7.72059, -7.83824, -7.95588, -8.07353, -8.19118}


First question: should I have this in a vector of something like t={{x1,y1},{x2,y2},...}?

Because when the transform is taken, I'm not sure if mathematica will convert the time values (0-67) into the frequency domain. I do know that for this particular graph, the points should each be 2*Pi/N radians apart, where N is the fundamental period of this function (68).

Second Question: When the transform is taken, is the frequency data in radian format? So that I can plot the data from -Pi radians to Pi radians? I assume this has something to do with the FourierParameters[] function but I'm not completely sure what all the different combinations are. The documentation is not very good.

So to sum up, I guess, what is the best format for my input data to be in to take the Fourier transform, and then how is the best way to plot the output data (Abs value plot and complex phase plot)

I have put some basic information on numerical Fourier transforms here where you will find answers to all your questions. For your specific example we proceed as follows

ft = Fourier[t, FourierParameters -> {-1, -1}];
sr = 250;
ff = Table[(n - 1) sr/Length[ft], {n, Length[ft]}] // N;
ListLinePlot[Transpose[{ff, Abs[ft]}], PlotRange -> All,
Frame -> True, FrameLabel -> {"Frequency/Hz", "Absolute Value"}]
ListLinePlot[Transpose[{ff, Arg[ft]}], PlotRange -> All,
Frame -> True, FrameLabel -> {"Frequency/Hz", "Phase"}]


I do not know your sample rate so I have assumed a sample rate (sr) of 250 points per second.

1. Do not include the abscissae in the input to Fourier.
2. If you want radians per second for the ordinates of the spectra then multiply my frequency values above (ff) by 2 Pi.

Hope that helps.

Edit. Frequency axis going from negative to positive values

The output of Fourier is periodic so going from negative frequencies to positive frequencies is a shift of the origin which is easily obtained using RotateRight. Care must be taken to ensure that the first point of the output, the zero frequency spectral value, ends up at the origin. The only tricky bit is that slightly different codes are needed depending on whether the number of points in the spectrum is even or odd. If the number of points is even then there is a value at SR/2. This point is missing for an odd number of points.

Below I do the general case where there can be any sample rate. In your case the sample rate is 1.

For an even number of points (which you have in your example) work as follows:

nn = Length@t;
ft = Fourier[t, FourierParameters -> {-1, -1}];
sr = 1.;
ff = Table[2 π (n - 1) sr/nn, {n, (-nn + 4)/2, (nn + 2)/2}];
ft1 = RotateRight[ft, (nn - 2)/2];
ListLinePlot[Transpose[{ff, Abs[ft1]}], PlotRange -> All,
Frame -> True, FrameLabel -> {"Frequency/Hz", "Absolute Value"}]
ListLinePlot[Transpose[{ff, Arg[ft1]}], PlotRange -> All,
Frame -> True, FrameLabel -> {"Frequency/Hz", "Phase"}]


For an odd number of points the code is as follows:

Use the same data but drop the last point to give an odd number of points.

t1 = Most@t;
nn = Length@t1;
ft = Fourier[t1, FourierParameters -> {-1, -1}];
sr = 1.;
ff = Table[2 π (n - 1) sr/nn, {n, (-nn + 3)/2, (nn + 1)/2}];
ft1 = RotateRight[ft, (nn - 1)/2];
ListLinePlot[Transpose[{ff, Abs[ft1]}], PlotRange -> All,
Frame -> True, FrameLabel -> {"Frequency/Hz", "Absolute Value"}]
ListLinePlot[Transpose[{ff, Arg[ft1]}], PlotRange -> All,
Frame -> True, FrameLabel -> {"Frequency/Hz", "Phase"}]


• Thank you very much for the prompt reply, very much appreciated. These look very similar to what I got on my own. I'm not sure what you mean by sampling rate -- like I said above, each point needs to occur at 2*Pi/N where N is the period but I'm not sure if that is what you are talking about. What is n in your code? In the third line? – amantonas Nov 21 '16 at 20:57