Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 5 of 5
Thread: Cosine fitting function

08212007, 10:16 PM #1
 Join Date
 Aug 2007
 Posts
 2
 Thanks
 0
 Thanked 0 Times in 0 Posts
Cosine fitting function
I am looking for an algorithm that will take data points and fit a cosine function to them. Anyone know of an algorithm that can fit data to a cosine function in general? The algorithm can be in any language (hence the general) but ultimately I will need it in C. Thanks for your help!
08232007, 11:28 PM
#2
 Join Date
 Aug 2007
 Posts
 2
 Thanks
 0
 Thanked 0 Times in 0 Posts
Any help?
08242007, 06:59 AM
#3
Do you know anything about the distribution that the data points are sampled from? Without that, you would have to do a leastsquares regression...
Ultimately, any sinusoidal curve should be represented as:
y=A*cos(omega*t+phi)
Thus, you take 3 partial derivatives of the least squares value and set equal to zero. I did this out
I just did out some of the calculus, and the 3 resulting equations cannot be analytically solved for (A,omega,phi)
You could run a solver for the 3 variables and get approximations. Alternatively, you could try doing the following:
1. Guess an amplitude. Divide all your data points by it, then take the inverse cosine of these new values. Run a linear regression on this new data set to get omega and phi.
2. Adjust amplitude, repeat (1), and compare errors. The L_2 error is a continuous function of A, so you should be able to find a local minimum without too much work.
You could even creatively guess at A... perhaps the average of the two most extreme values, or the average of all the values. Those are the two best possibilities that come to mind.
08242007, 07:02 AM
#4
Incidentally, depending on how tightly grouped on the scale you are examining the values you are sampling are, you might be able to replace the cosine function with the first few terms of its Taylor expansion; the polynomial approximation should be much easier to regress on.
08242007, 07:33 AM
#5
And oops, this was assuming that the sinusoidal curve's offset is 0 along the yaxis (which is not necessarily accurate at all; if that isn't the case, you need to add another parameter to the leastsquares analysis).