# Least squares regression of sine wave

Let's say that a sine-like function of a fixed frequency and zero-mean can only vary in amplitude and offset.

That is, for variables $$a \in R^+$$ and $$z \in [0..2\pi]$$

$$f(t) = a(\sin(kt + z))$$

(where $$k$$ is a constant proportional to the fixed frequency)

For some set of $$n$$ samples $$(t_1,y_1),(t_2,y_2)...(t_n, y_n)$$ we want to fit $$a$$ and $$z$$ such that they minimize:

$$L(a,z) = \sum(f(t_i)-y_i)^2$$

If f was a linear function we could just use ordinary least squares regression.

Is there some similar technique for the above function? What approach would you use to minimize $$L$$ ?

• Are you meaning that $k$ has a known fixed value ? Commented Nov 28, 2020 at 10:25
• @ClaudeLeibovici: Right, k is constant. The frequency is fixed. Commented Nov 28, 2020 at 10:43

In general because of the presence of periodic functions, this is not the simplest problem in the domain of curve fitting. However, this case is simple because $$k$$ is not a tunable parameter but a fixed constant.

You have $$n$$ data points $$(t_i,y_i)$$ and you want to perform a least square fit based on the model $$y=a \sin(kt+z)$$ Rewrite is as $$y=a \cos (z) \sin (k t)+a \sin (z) \cos (k t)$$ and define $$A=a \cos (z)\qquad B=a \sin (z)\qquad S_i=\sin (k t_i)\qquad C_i=\cos (k t_i)$$ which makes the model to be $$y=A \,S+B \,C$$ which is just a bilinear regression without intercept.

When solved for $$(A,B)$$, you have $$a^2=A^2+B^2\qquad\qquad\text{and}\qquad\qquad z=\tan ^{-1}\left(\frac{B}{A}\right)$$

I tried an example : using $$n=21$$ and $$k=2$$, I generated the following table $$\left( \begin{array}{cc} t & y \\ 0.0 & 42 \\ 0.5 & 121 \\ 1.0 & 89 \\ 1.5 & -25 \\ 2.0 & -115 \\ 2.5 & -99 \\ 3.0 & 8 \\ 3.5 & 108 \\ 4.0 & 109 \\ 4.5 & 10 \\ 5.0 & -98 \\ 5.5 & -115 \\ 6.0 & -27 \\ 6.5 & 87 \\ 7.0 & 121 \\ 7.5 & 44 \\ 8.0 & -73 \\ 8.5 & -123 \\ 9.0 & -59 \\ 9.5 & 59 \\ 10.0 & 124 \end{array} \right)$$

This gives $$A=116.1789094\qquad\qquad\text{and}\qquad\qquad B=41.88715050$$ from which $$a=123.4992808\qquad\qquad\text{and}\qquad\qquad z=0.3460335933$$

In fact the data were generated using $$y_i=\lceil 123.456 \sin (2 t+0.345678)\rceil$$

• I have tried to improve the readability of your question by improving the $\rm \LaTeX$ code. It is possible that I unintentionally changed the meaning of your question. Please proofread the question to ensure this has not happened. Nice use of partial derivatives! How did you input the tall table in the middle? I wonder if you typed it by hand. Commented Nov 28, 2020 at 15:55
• @GNUSupporter8964民主女神地下教會. Thank you ! If I may ask, what is the meaning of $民主女神地下教會$ ? Whatever it means, it is beautiful. Commented Nov 28, 2020 at 16:15
• 民主女神 = 🗽 statue of liberty, 地下 = underground, 教會 = ⛪ church. Thanks SE network for the freedom of our choice of user name. Inspired by Ciro Santilli, I'm choosing these words because they are sensitive words in a big country who imposes an Internet censorship on its territory. If that country stops programmers from browsing the SE network, they might do something to shake the barrier. Commented Nov 28, 2020 at 16:24
• @GNUSupporter8964民主女神地下教會. I then confirm : it is beautiful ! Cheers. By the way, my family name means wolf son Commented Nov 28, 2020 at 16:27
• Thanks for telling me. Cheers. Commented Nov 28, 2020 at 16:31