Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

I Regression of a sine wave

  1. Aug 10, 2017 at 2:12 PM #1
    So my question is, how does this work (hopefully I'm allowed to do hyperlinks):

    Given a table of x and y coordinates, how do I find the sin wave of best fit. I need to get f (frequency), a (amplitude), and p (phase) for the function in this form f(x) = a ⋅ sin( x ⋅ f + p )

    My end goal is to have a software function that can do this. I'm really having difficulty figuring out where to start :(. I'd like to also be able to do this for cos(), but I'm not sure if I can figure out cos based on how sin works.

    I'd like to understand how it's done, but at the same time I'm a bit scared that this might be way over my head. Any help would be appreciated.
  2. jcsd
  3. Aug 10, 2017 at 2:19 PM #2


    User Avatar
    2016 Award

    Staff: Mentor

    It depends a bit on your data, the typical approach would be a least squares fit where you find parameters a,f,p that minimize the sum of squared deviations (or squared [deviations divided by the uncertainty], if known for each entry). There are programs to do that automatically.
    It doesn't matter if you fit a sine or a cosine or any other reasonable function, the algorithm is the same.

    f in your formula is the frequency multiplied by 2 pi, by the way.
  4. Aug 10, 2017 at 2:21 PM #3
    oh yes, 2 pi I forgot.

    I guess I know that there are programs that do this. I want to have my own program do this. I'm very confused with how least squares works. I've looked it up on google a bunch but just get confused. Is there maybe a resource that you would suggest?
  5. Aug 10, 2017 at 9:43 PM #4


    User Avatar
    2016 Award

    Staff: Mentor

    Every statistics textbook should cover it, and various websites should have good descriptions. The basic idea is not complicated, doing that efficiently for more complex functions can be challenging but with a sine wave it should be fine.
  6. Aug 14, 2017 at 2:27 PM #5


    User Avatar
    Science Advisor

    And, of course, you can use some form of a fourier transform, FFT, DFT,...
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: Regression of a sine wave
  1. Sine waves (Replies: 6)

  2. Amplitude of sine wave (Replies: 2)

  3. Sine Waves Sine Angles (Replies: 7)