[Math] Fitting a sine wave of known frequency through three points

statistics

We have a computationally expensive function of a large set of data and an angle, that is known to result in a sine wave:

$$ f(\text{data}, \theta) \approx a \sin (\theta + b) + c$$

We want to find the constants $a$, $b$, and $c$, executing the function as few times as possible.

Best Answer

Yes, you can do it in 3 points, considering $f(0)$, $f(\pi)$, and $f(\frac{\pi}{2})$.

$$ \begin{aligned} f(0) &= a\sin b + c & \\ f(\tfrac{\pi}{2}) &= a\sin(b + \tfrac{\pi}{2}) + c & &= a\cos b + c \\ f(\pi) &= a\sin(b + \pi) + c & &= - a\sin b + c \end{aligned} $$

To find $c$:

$$ \begin{aligned} f(0) + f(\pi) &= 2c \\ \implies c &= \frac{f(0) + f(\pi)}{2} \end{aligned} $$

To find $a$:

$$ \begin{aligned} (f(0) - c)^2 + (f(\tfrac{\pi}{2}) - c)^2 &= a^2\sin^2 b + a^2\cos^2 b= a^2 \\ \implies a &= \sqrt{(f(0) - c)^2 + (f(\tfrac{\pi}{2}) - c)^2} \end{aligned} $$

And finding $b$:

$$ \begin{aligned} \frac{f(0) - c}{f(\tfrac{\pi}{2}) - c} &= \frac{a\sin b}{a\cos b} = \tan^{-1} b \\ \implies b &= \operatorname{atan2}\left(f(0) - c, f(\tfrac{\pi}{2}) - c\right) \end{aligned} $$