0

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 sinewave 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?

So, I hope that the conceptual example 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?

danronmoon
  • 3,814
  • 5
  • 34
  • 56
MetaStack
  • 3,266
  • 4
  • 30
  • 67
  • Sorry if I come off like a complete newbie here, but isn't both the `x` and `y` scale able by the same factor? Meaning they can both be multiplied by the same value to achieve what you desire? – Torxed May 22 '20 at 14:48
  • @Torxed yes, but the question is how do I find that factor? how do I fit it automatically? the only ways I can find to fit it fit amplitude and frequency separately. – MetaStack May 22 '20 at 14:50
  • I'd say you'll have to choose one vector. I think this is why most oscilloscopes for instance can either scale on X or on Y, depending on what you need. If you need to see the amplitude - usually the frequency isn't that important and so you scale it on `Y`. But if frequency matters more to you - you'll scale it according to `X`. Does that make sense? (Depends on your needs ofc) – Torxed May 22 '20 at 14:54
  • naively, i would try to fit `a*np.sin(a*x +b) +c`. That should preserve the ratio. – warped May 22 '20 at 15:02
  • @warped thanks I'll try to incorporate that – MetaStack May 22 '20 at 15:47
  • @Torxed I don't know if that makes sense to me. lets say I scale y to get the best fit, then I scale x by the same factor, well, I'll probably have to go back to y again and scale it to a new better fit, then apply it to x, and I don't know, though it seems like it would, if it would converge... thanks for your thoughts. – MetaStack May 22 '20 at 15:49

0 Answers0