# Discrete Fourier Transform: help on how to convert x axis in to the frequency when I have a set of data samples [duplicate]

I have a huge data set (400000 sample) and when I plot them

ListLinePlot[Au4, PlotRange -> {All, All}]


I used the following code to Fourier transform it

ListLinePlot[Abs[Fourier[Au4][[2 ;; 6000]]], PlotRange -> All,
DataRange -> {2, 6000}, PlotStyle -> Red]


And got this

My problem is how to convert the x axis here in to the frequency domain in Mathematica? I saw that I can use the DataRange for that but I could not figure out how to do that exactly. I failed all the attempts. (I also saw that I can only use Fourier to the sample data only but I don't understand what is resulted in X axis after doing the Fourier to samples) Can someone help me at this point?

## marked as duplicate by Sjoerd C. de Vries, MarcoB, user9660, m_goldberg, Jason B.Feb 2 '16 at 9:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

• There were quite a few similar questions on this site. For instance 44237 and 33149. Also, have a look at the built-in Periodogram with option SampleRate – Sjoerd C. de Vries Feb 1 '16 at 19:01
• In addition to those suggested by @SjoerdC.deVries, I'd suggest Hugh's answer to this question on the same topic: What do the X and Y axis stand for in the Fourier transform domain?. I found itwell-written and very educational. – MarcoB Feb 1 '16 at 19:07
• Thanks a lot for you help.Really appreciate that. – ravi3 Feb 2 '16 at 3:41
• – mrz Feb 3 '16 at 9:23

## 1 Answer

Based on the above referenced (and unreferenced) answers on mathematica.stackexchange, here is a short version of running code:

data=Au4;                      (* input *)
timeInterval = 10;             (* input *)
nSamples = Length@data;

dt=timeInterval/nSamples;
spectrumLength = Floor[0.5 nSamples];
powerSpectralDensity = Abs[Fourier[signal][[;; spectrumLength]]]^2;
freqs = Range[0, spectrumLength - 1]/(dt*nSamples);
plot = ListLogLogPlot[Transpose[{freqs,powerSpectralDensity}]]


Seeing is believing, so here is the code applied to a simple sine wave:

• Frequency is 0.5.
• Duration of sampled time interval is 20.
• Number of discrete samples in interval is 1000.

Code:

nSamples=1000;
timeInterval=20.0;
dt=timeInterval/nSamples;
f=0.5;
signal=Table[Sin[2.0 Pi f t dt],{t,nSamples}];
ListLinePlot[signal,Frame->True,DataRange->{0,timeInterval},FrameLabel->{"time","signal"},Mesh->All,MeshStyle->Directive[AbsolutePointSize[4],Black],PlotRangePadding->{None,Automatic},FrameStyle->Directive[Black,20,FontFamily->"Helvetica",AbsoluteThickness[2]]]


spectrumLength=Round[0.5 nSamples];
powerSpectralDensity=Abs[Fourier[signal][[;;spectrumLength]]]^2;
freqs=Range[0,0.5/dt,0.5/(dt(spectrumLength-1))];
ListLogLogPlot[Transpose[{freqs,powerSpectralDensity}],Frame->True,Joined->True,PlotRange->All,PlotRangePadding->None,GridLines->{{f},None},GridLinesStyle->Directive[Red,Dashed],FrameLabel->{"frequency f","power spectral density"},PlotRangePadding->{None,Automatic},FrameStyle->Directive[Black,20,FontFamily->"Helvetica",AbsoluteThickness[2]]]


• Thank you very much for your help but I am still confused on my set of data on how to relate what you mentioned above. I have 400000 data set but if I just use fourier for whole 400000 set it gives me two identical peaks (I guess that is because + and - data) . If I want to show one peak which is clear how should I do. If I use this will be correct. ListLinePlot[Abs[Fourier[Au10TD][[2 ;; 4000]]], PlotRange -> All, DataRange -> {2, 4000}, PlotStyle -> Red] I see a clear peak with this code but I can't understand it's physical meaning. Can you help me to understand this ? – ravi3 Feb 4 '16 at 1:00
• @ravi3 You need to know 2 things in advance before you start the algorithm: a) How many datapoints do you have? In your case it is 400,000. b) What is your real time sampling interval for the 400,000 points? For example, if you measure 400,000 datapoints in one hour or in 10 years; that will lead to very different physical frequencies. Problem is, the measurement interval is not included in your time series! That is why you need to specify the variable "timeInterval" in the code above. – Oscillon Feb 4 '16 at 9:52
• Thank you for your reply. I have 400000 points corresponds to 480fs. Then when I plot for the whole set I see two peaks (identical) in the two extreme comers of the graph. Then to get a good peak I tried the above mentioned code so I could only see one peak (I selected 6000 out of 400000 data set to get that) So 6000 steps means 7.2 fs. (dt =1.2as) But my question is is it correct to do so and what I am seeing is correct as a peak. – ravi3 Feb 4 '16 at 21:32
• Sure, you can just take a small part of the spectrum. However, if you want to see a peak close to zero better, I would suggest using a logarithmic x scale instead of cutting it. Also in your approach, you still need to calculate the maximum frequency for DataRange, which is 0.5/dt and not 4000. – Oscillon Feb 6 '16 at 12:05
• Thanks. I have a confusion with this ListLinePlot[Abs[Fourier[Au10TD][[2 ;; 4000]]], PlotRange -> All, DataRange -> {2, 4000}, PlotStyle -> Red] . This Au10TD list is having 400000 sample points. I thought when I use this code it only takes the 2 to 4000 data samples out of 400000 and Fourier transform this. Or is it taking all the 400000 data point samples and just show me the plot for 4000 samples? Can you clarify that to me. – ravi3 Feb 8 '16 at 18:29