I have data that I believe is sinusoidal, but I don't have an integral
number of periods. How do I find the "best fit" Sin/Cos
function,
compensating for this and for the ugly constant that appears? EXAMPLE:
Here is some data that follows a sinusoidal pattern (Mathematica format)
t4 = N[Table[Sin[3.17*2*Pi*x/200], {x,1,200}]];
Now, using just
t4
, I want to get backSin[3.17*2*Pi*x/200]
or the equivalent.Note that
Mean[t4]
is non-zero (it's about0.0281886
). The analyses I've tried so far "pull out" this mean (like "0.0281886 + ...
"). This is bad because it's unlikely I'll get back to my original form with that constant pulled out.Using j0ker5's excellent technique from https://stackoverflow.com/questions/4463481/continuous-fourier-transform-on-discrete-data-using-mathematica I can compensate for the non-integral period and get:
0.0281886 + 0.983639 Cos[1.49867 - 0.0992743 x]
Note that the x
term is 3.16*2*Pi*x/200
, very close to my original.
- I modified j0ker5's technique slightly. The actual function I used to get the above:
superfourier2[data_] :=Module[
{pdata, n, f, pos, fr, frpos, freq, phase, coeff},
pdata = data;
n = Length[data];
f = Abs[Fourier[pdata]];
pos = Ordering[-f, 1][[1]] - 1;
fr = Abs[Fourier[pdata*Exp[2*PiIposRange[0,n-1]/n],
FourierParameters -> {0, 2/n}]];
frpos = Ordering[-fr, 1][[1]];
freq = (pos + 2(frpos - 1)/n);
phase = Sum[Exp[freq*2*PiIx/n]*pdata[[x]], {x,1,n}];
coeff = N[{Mean[data], 2*Abs[phase]/n, freq*2*Pi/n, Arg[phase]}];
Function[x, Evaluate[coeff[[1]] + coeff[[2]]*Cos[coeff[[3]]*x - coeff[[4]]]]]
]
In addition to the bad constant term, note that adding "
0.983639*Cos[1.49867 - 0.0992743 x]
" forx=1..200
yields0.0279175*200
, which I'm convinced makes things worse, not better.I believe the
0.0279175*200
sum from the cosine and the200*0.0281886
from the mean can somehow "cancel" to yield back my pureSin[]
function.
Thoughts?