[Math] How to fit a curve to a sinusoidal wave

regressionsignal processing

I am wondering how to fit a sinusoidal wave (approximation). I would like to fit it in the form: $y = A\sin(Bx + C) + D$ where $A,\,B,\,C$ and $D$ are constants. The only constants I really care about is A and B so that I can get the amplitude and time period..

Edit:

I think I should clarify.. The data that I will be fitting will likely be noisy, so I was thinking that sine fit might give me a more accurate reading for the amplitude than just taking the max and min values from the raw data..

Best Answer

You need a nonlinear least-squares routine, available in any numerical analysis book. Obsolete versions of Numerical Recipes are available for free. You can get pretty good starting values by setting D as the average of all the data, A as the range of the data, and B and C from an FFT. It might be a little simpler to expand the sine to $A\sin(Bx)\cos C+A\cos(Bx)\sin C$ and define $E=A\cos C, F=A\sin C$, but I'm not sure.