How Do You Fit a Sine Wave to Data Points?

Click For Summary

Discussion Overview

The discussion revolves around fitting a sine wave to a set of data points, specifically how to determine the parameters of the sine function (frequency, amplitude, and phase) that best fit the data. The conversation includes aspects of statistical methods, programming, and the mathematical foundations of curve fitting.

Discussion Character

  • Exploratory, Technical explanation, Conceptual clarification, Debate/contested

Main Points Raised

  • One participant seeks guidance on fitting a sine wave to data points and expresses uncertainty about the process.
  • Another participant suggests using a least squares fit to minimize the sum of squared deviations to find the parameters a, f, and p.
  • It is noted that the frequency in the sine function should be multiplied by 2 pi.
  • A participant acknowledges confusion about the least squares method and requests resources for better understanding.
  • One reply mentions that statistics textbooks typically cover least squares fitting and that the basic concept is not complicated, though implementation for complex functions can be challenging.
  • Another participant introduces the idea of using Fourier transforms (FFT, DFT) as an alternative method for fitting sine waves.
  • Discussion includes mention of nonlinear optimization algorithms for fitting nonlinear functions, emphasizing the use of gradients to minimize error in parameter estimation.

Areas of Agreement / Disagreement

Participants generally agree on the use of least squares fitting as a method for determining the sine wave parameters, but there are multiple approaches discussed, including the use of Fourier transforms and nonlinear optimization techniques. The discussion remains unresolved regarding the best method to implement for the participant's specific needs.

Contextual Notes

Participants express varying levels of familiarity with statistical methods and programming, indicating potential limitations in understanding the underlying concepts and algorithms. There is also a mention of the complexity involved in fitting nonlinear functions compared to linear models.

Simoyd
Messages
3
Reaction score
0
So my question is, how does this work (hopefully I'm allowed to do hyperlinks):
https://www.desmos.com/calculator/zlvrts7mul

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.
 
Physics news on Phys.org
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.
 
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?
 
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.
 
And, of course, you can use some form of a Fourier transform, FFT, DFT,...
 
For linear models, a typical statistics book would cover this process. For nonlinear functions with parameters like you have, nonlinear quadratic optimization algorithms have been developed. They use the gradient of the quadratic error function to find the changes of parameters a, f, and p that will lead it (usually step-by-step) to values a=a0, f=f0, p=p0, that give a minimum error. It is a studied in nonlinear programming.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 30 ·
2
Replies
30
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
7
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
3
Views
2K