Fitting a sine wave without stretching it

181 Views Asked by At

I want to fit a sine wave without stretching it - that is I want its frequency and amplitude to remain relatively the same. So all the transformations I can make to the sine wave to fit it to my data are scaling it (increasing frequency and amplitude together) and panning it (moving it up or down, left or right).

Can this be done with cure_fit from scipy or any other fit from sklearn?

I have some starter code that works to fit a sinewave to data, but it stretches its frequency and amplitude separately (doesn't scale statically). I can include it here but its essentially the same as the code from this answer:

SciPy leastsq fit to a sine wave failing

Let me just give one conceptual example of what I mean so it's very clear. Say I have some data spanning from y=100 to y=1000. Sine waves go from y=-1 to y=1. What should the fit look like? Well...

Firstly I know I'll have to scale it up. How much depends on the least-squares fit (perhaps y=100 to y=1000, being the min and max of the data, are outliers).

Secondly, I know I'll have to move it up so it's not centered around y=0, but rather centered at y=500 or wherever the least-squares fit suggests.

Furthermore, perhaps the data looks like an S-curve. in that case, the scaling of the sine wave must be quite large, and I'll have to pan it to the left to line up the bottom of the wave with the bottom of my data. Makes sense?

It seems like this should be pretty elementary but I haven't been able to figure it out manually. How do I fit a sine wave, while keeping the wave strictly a sine wave without any distortion?

0

There are 0 best solutions below