1

I am looking for a mathematical method of determining how relatively close a data set is, to fitting a generic sine wave (in other words, $\sin(x)$). In my mind, this would be something like an R squared value for a linear regression, except for a sine wave. I would like to be clear that I am not looking for a way to fit data to a sinusoidal function.

So if my data looked something like the following, how close is that to being ($\sin(x)$). Again, I am not looking to do a curve fit for this data. Also, the data was just plucked from Google images as an example of what I am talking about.

Read the comments below for some additional clarification.

enter image description here

Snoop
  • 125
  • 8
    Do a Fourier analysis and compare the magnitudes of the "overtones" to the "fundamental." – David G. Stork Dec 29 '20 at 16:36
  • A Discrete Fourier Transform (DFT) makes sense if your sine wave has an integer frequency of the signal length. But if it doesn't, then you would do better with a Fourier frame encoding. – mathreadler Dec 29 '20 at 17:50
  • Here is an old work of mine how you can construct Fast Fourier Frames https://math.stackexchange.com/questions/2917088/fast-fourier-frames-fff-do-they-exist-and-if-so-how-to-calculate-them – mathreadler Dec 29 '20 at 17:52
  • Without a lot more details I doubt that there is mathematically valid way to compute what you ask for. – Somos Dec 29 '20 at 18:07
  • To clarify a little more, I ultimately am looking for something that can test how "sinusoidal" something is. – Snoop Dec 30 '20 at 04:31
  • A relative degree of sinusoidalness, if you will... – Snoop Dec 30 '20 at 04:31
  • @mathreadler Another way I might ask this question, is to say "is this data a candidate for a sinusoidal regression?" Just by looking at it, we can see the pattern... but is there a way to mathematically verify this? Hopefully that makes more sense. – Snoop Dec 30 '20 at 04:39
  • 1
    @Snoop As far as I know there is no good way to answer "is this a candidate for ... regression"? without trying to do the regression and seeing how it goes. If it goes badly, then either the answer is "no" or "oh, we screwed up". Not good because "oh" and "no" sound so much alike. – mathreadler Dec 30 '20 at 11:29

2 Answers2

2

When data is this irregular, I first do some fairly simple-minded smoothing such as a moving average. Estimate the location of the peaks. If irregular data causes two peaks to be too close, take their average as the peak location.

I would then look at the distances between successive peaks. If this are approximately equal, this gives an estimate of the period. Call this $p$.

I would then look at the maximum and minimum values - call them $a$ (min) and $b$ (max).

Then, with $h = (b-a)/2$ being an estimate for the amplitude and $c = (b+a)/2$ being an estimate for the center, an initial estimate for the fitting curve is $c+h\cos(2\pi\frac{t-t_0}{p}) $ where $t$ is the time (x-axis) and $t_0$ is the location of the first peak.

Finally, put these parameters ($c, h, t_0, p$) into a nonlinear fitting routine (least squares is probably reasonable and available) as the initial values for fitting that model function to the actual (unsmoothed) data.

Then look at the fit.

All this is moderately ad hoc, but I have done similar things in the past successfully.

marty cohen
  • 107,799
0

One simple approach is to fit it to a sine wave with unknown frequency, amplitude and phase. That is a three dimensional nonlinear minimization problem. Depending on your definition of sinusoidal, you might have a constant term as well, for four parameters. You can then look at what fraction of the variation is explained by the sine wave you found. How does the amplitude of the error compare to the amplitude of the function you found? In your figure, the peak to peak amplitude appears to be about $6$. There is clearly noise in the range of $0.5-1$ peak to peak. A bad fit to a sine wave might make this worse. This approach cannot tell the difference between noise and a repetitive signal that is not shaped like a sine wave. Is that important to you? You can certainly do a full FFT and see if the energy is in high frequency bins like noise or in low frequency bins because the wave shape is not sinusoidal.

Ross Millikan
  • 374,822
  • You and I can look at that figure and we can both tell that it is a sine wave, why is that? Is it because of the period? The amplitude? I'd like to discard the amplitude and look at the period (which here, is relatively consistent) and say, hey this looks kind of like a sine wave. For my application, I can ignore the amplitude to some extent. Is there anything relatively simpler than an FFT that can achieve this result? – Snoop Dec 30 '20 at 05:15
  • I was thinking of measuring variance of the periods. Is it possible that this might work? – Snoop Dec 30 '20 at 05:17
  • But a sine wave is much more than consistent amplitude. A triangle wave also has consistent amplitude and period. Whether the data fits a sine wave depends on that shape sensitively. This one might fit, or the bottom peaks might be consistently narrower than the top peaks, for example. – Ross Millikan Dec 30 '20 at 05:17
  • Okay, that's fair. Maybe I should rephrase this question and say "periodic" or something like that? Do you see what I am asking? – Snoop Dec 30 '20 at 05:18
  • You can certainly try to align a number of periods and see the variation between the wave shape from one period to another. Defining where periods start and stop is hard in the presence of noise-you can't find zero crossings or peaks reliably. Here the period seems to be about 25, but I don't know how to find a better value without fitting a functional form of some sort. How accurately do you think you can find the peaks in your example in order to check the variation in time between them? I think it is very hard and you need to think carefully about what questions you want to ask. – Ross Millikan Dec 30 '20 at 05:23
  • You are absolutely right, I did need to think further. Although not ideal for stack exchange, I asked my question, and I learned what I was really looking for, through conversation. – Snoop Dec 30 '20 at 05:27