Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    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!

  • #2
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Any help?

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Do you know anything about the distribution that the data points are sampled from? Without that, you would have to do a least-squares 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.

  • #4
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    And oops, this was assuming that the sinusoidal curve's offset is 0 along the y-axis (which is not necessarily accurate at all; if that isn't the case, you need to add another parameter to the least-squares analysis).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •