10
$\begingroup$

CONTEXT

I read 5 different related posts but I do not understand how to interpret mathematica's output for my situation.

data=(Uncompress@*FromCharacterCode@*Flatten@*(ImageData[#1, "Byte"] &)@*
   Import)["http://ooo.0o0.ooo/2016/10/16/5803c2e35b8ac.png"]

I have 300 measurements (taken every 1 second);

I am told that the data is in the form $$y = A \sin{2\pi f t}$$

I want to find the frequency $f$ using a fourier transform

ListLinePlot[data]

enter image description here

plotting the data it seems (using my fallible human perception) that the period is not far of 18 seconds

Show[
     ListLinePlot[data, AxesLabel -> {"s"}], 
     Plot[.03 Sin[2 \[Pi] (1/18) t] + Mean[data], {t, 0, 300},    
          PlotStyle -> Directive[Orange, Dashed]
     ]
]

enter image description here

note this code could be incorrect perhaps I need to include FourierParameters

powerSpectrum = Abs[Fourier[data[[All, 2]]]]^2;
ListLinePlot[powerSpectrum[[1 ;; 100]], PlotStyle -> Red, PlotRange -> {All, {0, .012}}]

QUESTION

Ignoring the outliers on the extreme left it appears that the highest spike is near x = 18

How does one convert this into a frequency (number of oscillations in 1 second)?

Given that my sample length is 300 seconds?

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ You might synthesize some data where you know the frequency and you have less noise and see if your code gives the answer you expect. You might read en.wikipedia.org/wiki/Window_function on how to window your data and perhaps get better results. $\endgroup$
    – Bill
    Sep 24, 2016 at 5:13
  • $\begingroup$ thankyou will try to do that now $\endgroup$
    – conor
    Sep 24, 2016 at 5:19

1 Answer 1

11
$\begingroup$

Have a look at my post on using Fourier here you will see that the frequency axis has an increment of the sample rate divided by the number of points and goes from zero to one less than the sample rate.

I also note that your data has a large mean value. It is worth removing this as it dominates the first point of the fourier transform. You tell us that the sample rate is 1 sample per second.

a = data[[All, 2]];
b = a - Mean[a];
sr = 1;  (* sample rate*)
nn = Length@data;
ff = Table[(n - 1) sr/nn, {n, nn}] // N;
ft = Fourier[b, FourierParameters -> {-1, -1}];
ListLinePlot[Transpose[{ff, Abs[ft]}][[1 ;; 40]], PlotRange -> All]

Mathematica graphics

There is a peak at point 18 corresponding to a frequency of 0.056666 Hz or a period of 17.6 sec. However, the data is very noisy and nothing like a sine wave. I would recommend trying to get better data. Trying to estimate an exact sine wave from Fourier is difficult even if the data is good. Look here for an extensive discussion on this topic.

Hope that helps.

$\endgroup$
6
  • 1
    $\begingroup$ Just a side note, you can use Periodogram instead of the Fourier + ListLinePlot combination. $\endgroup$
    – xzczd
    Sep 24, 2016 at 10:56
  • 1
    $\begingroup$ or this? ListLinePlot[PeriodogramArray[b], PlotRange -> {{0, 30}, {0, 0.015}}] $\endgroup$
    – user36273
    Sep 24, 2016 at 13:44
  • $\begingroup$ The sample rate is 1 not 300 ("I have 300 measurements (taken every 1 second)" $\endgroup$ Sep 25, 2016 at 9:59
  • $\begingroup$ @SimonWoods I took your quoted comment to mean that there is 300 measurements in one second. If we take it as 1 per second then that is a very different position. The suggested 18 Hz is then horribly aliased. However your interpretation is supported by the abscissa of the data which increment by 1 for each point. We need to ask the question from the poster. $\endgroup$
    – Hugh
    Sep 25, 2016 at 17:20
  • 1
    $\begingroup$ The OP mentioned a period of 18s, not a frequency of 18Hz. $\endgroup$ Sep 25, 2016 at 18:05

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.