Fitting trigonometric function

61 Views Asked by At

Let $y$ be a measured time series, Signal and $\hat{y}(t) = A\cos(\omega t +\phi)$. How to find $A$, $\omega$, $\phi$ that minimizes

$$\sum_{k = 0}^{N - 1}|\hat{y}(k) - y_k|^2$$

The amplitude spectrum looks like Spectrum. Thus, it is not viable to try to find a particular peak in the spectrum. I tried to find $\omega$ by computing the center of mass of the spectrum, $$ \frac{\sum_{k=0}^{N/2}k|Y_k|}{\sum_{k=0}^{N/2}|Y_k|} $$, where $Y_k$ is the elements in the DFT, i get around 46 periods within the time series, which is way too high. By ocular inspection, it there should be around 5.75 periods, with $\phi\approx\pi$.

1

There are 1 best solutions below

2
On BEST ANSWER

Hint.

This can be done by simply calculating

$$ \min_{A,\omega,\phi,y_0}\sum_k\left(f(t_k,A,\omega,\phi,y_0)-f_k\right)^2,\ \ \text{s.t.}\ \ \omega_{min}<\omega<\omega_{max} $$

My calculations gave

enter image description here

where

$$ f(t,A,\omega,\phi,y_0) = y_0 + A\cos(\omega t+\phi) $$

NOTE

Due to the constraints, the best way to solve this minimization problem, is by using a proper solver. Follows a MATHEMATICA script with this purpose

Clear[f]
f[A_, t_, omega_, phi_, y0_] := y0 + A Cos[omega t + phi]
obj = Sum[(f[A, data[[k, 1]], omega, phi, y0] - data[[k, 2]])^2, {k, 1, Length[data]}];
sol = Minimize[{obj, omegamin < omega < omegamax}, {A, omega, phi, y0}]

f0 = f[A, t, omega, phi, y0] /. sol[[2]]
gr0 = ListPlot[data, PlotStyle -> Red]
gr1 = Plot[f0, {t, 0, tmax}, PlotStyle -> {Thick, Black}];
Show[gr1, gr0, PlotRange -> All, AspectRatio -> 1, Axes -> False]

Here data contains $\{t_k, f_k\}_{k=1,\cdots,n}$ tmax$\approx 1030$. The limits in $\omega$ can be established observing that for a single sinusoidal we have $5\times 2 \pi \approx \omega\times 1030$