1
$\begingroup$



I'm trying use Mathematica to compute the Discrete Fourier Transform (DFT) of a table of data points and then plot the result. As a test run, I took values from the function $x=f(t)=\sin(2\pi t) $ for $0\leq t \leq 2$ in increments of $\Delta t=1/32=0.03125$. Here is the code I put in Mathematica:

tval = Table[i, {i, 0, 2, 0.03125}]
xval = Table[Sin[2*Pi*i], {i, 0, 2, 0.03125}]
data = Transpose[{tval, xval}]
ListLinePlot[data, PlotRange -> All]
ListLinePlot[Abs[Fourier[data]], PlotRange -> All]


Here is the plot of data from the line ListLinePlot[data, PlotRange -> All]:

Plot of <code>data</code>

However, I get a weird looking plot for ListLinePlot[Abs[Fourier[data]], PlotRange -> All] :

enter image description here

I would expect to have a peak in the frequency domain at 1 Hz, since $f(t)$ is a sine wave with frequency 1 Hz. However, the second plot above doesn't peak at 1 Hz and the end of the plot goes up for some reason.

Am I doing something wrong? I have heard a little bit about "aliasing" when using the DFT, but I'm not sure if that is the case here, or what I could do about it if it was. Please keep in mind that I am pretty new to the concept of the Fourier Transform.

$\endgroup$
1
  • $\begingroup$ I have put some basic information on Fourier here with examples. JM has given you the correct answer you should not include time when using Fourier. $\endgroup$
    – Hugh
    Mar 29, 2018 at 8:57

3 Answers 3

3
$\begingroup$

I think the correct way to look at the FFT is

ListLinePlot[Abs[RotateRight[Fourier[xval], 32]], DataRange -> {-16, 16}, PlotRange -> All]

since the frequency range of the FFT is from 0 to 32 (or from -16 to 16). Also, there are two peaks because the sine wave is the superposition of two frequencies at 1Hz and -1Hz.

xval = Table[Sin[2 \[Pi] i], {i, 0, 2, 1/32}];

ListLinePlot[Abs[RotateRight[Fourier[xval], 32]], DataRange -> {-16, 16}, PlotRange -> All]

enter image description here

xval = Table[Sin[4 \[Pi] i], {i, 0, 2, 1/32}];

ListLinePlot[Abs[RotateRight[Fourier[xval], 32]], DataRange -> {-16, 16}, PlotRange -> All]

enter image description here

$\endgroup$
1
  • $\begingroup$ I think this is what I was looking for, thank you! $\endgroup$ Mar 29, 2018 at 11:15
4
$\begingroup$
  1. You're transforming both the abscissas and the values, when you should only been transforming the values.

  2. You need to shift the zero frequency term.

Using fftshift[] from this answer, we have:

xval = Table[Sin[2 π i], {i, 0, 2, 1/32}];

{ListLinePlot[xval, DataRange -> {0, 2}, PlotRange -> All], 
 ListLinePlot[Abs[fftshift[Fourier[xval]]], DataRange -> {0, 2}, PlotRange -> All]}
// GraphicsRow

plot of sine wave and its transform

$\endgroup$
3
  • $\begingroup$ 1. Oh! That makes more sense, thank you. 2. Can you explain what the zero frequency term is and why it needs to be shifted? Also, why are these peaks on either side of 1 Hz, instead of at 1 Hz? $\endgroup$ Mar 29, 2018 at 3:24
  • $\begingroup$ I want to make a plot that picks out the frequency of the original signal. If I increase the frequency of the original signal to 2 Hz using the code you provided, the second plot is still centered around 1 Hz... $\endgroup$ Mar 29, 2018 at 4:04
  • $\begingroup$ Hmm, let me get back to you on that (unless someone beats me to it); I am running something else at the moment. $\endgroup$ Mar 29, 2018 at 4:07
3
$\begingroup$

If you just want to plot, you can look into Periodogram.

Periodogram[data[[All, 2]], PlotRange -> All, 
 SampleRate -> (Length[data] - 1)/(Subtract @@ data[[{-1, 1}, 1]]), 
 ScalingFunctions -> "Absolute"]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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