9
votes

Is there an algorithm that can be used to determine whether a sample of data taken at fixed time intervals approximates a sine wave?

4
I would be fairly sure that given any finite set of data (with distinct X values), you can find a sine wave that fits exactly, if you don't place some kind of constraints on the frequency (as the frequency increases, the sine wave approaches a space-filling curve).AakashM
That is the definition of the continuous Fourier transform. In practice, you can only take the discrete Fourier transform, and you will be limited to finite frequencies (as the expansion of the Fourier integral goes on to infinity in the continuous case). Most "curved" functions can be fit very well using the Fourier transform, but "rectangular" functions will always have distortion at the edges, even when the expansion is continued for a long time. (Vertical lines are similar to the Dirac delta, which is 1 for ALL frequencies).CookieOfFortune
The best way would depend on how you expect the data to depart from sineness. Random noise? Distorted waveshape (e.g. triangular or clipped-off peaks)? A sum of several sines vs. one pure sine?DarenW
This is NOT necessarily the same as taking a Fourier transform. It is also NOT just curve fitting. It is a classifier for something being sine versus non-sine. There are not enough constraints for this. Do we care about phase? Do we care about noise in the amplitude direction? Do we care about amplitude or frequency drift? Do we care about deltas (that the differential is enough like a cosine)? Is it just one-class classifier or are their other classes of non-sine waves we have to determine? This is a late comment, but it needs to be stated that an FFT is not necessarily the solution.Anne van Rossum

4 Answers

17
votes

Take the fourier transform which transforms the data into a frequency table (search for fft, fast fourier transformation, for an implementation. For example, FFTW). If it is a sinus or cosinus, the frequency table will contain one very high value corresponding to the frequency you're searching for and some noise at other frequencies.

Alternatively, match several sinussen at several frequencies and try to match them using cross correlation: the sum of squares of the differences between your signal and the sinus you're trying to fit. You would need to do this for sinussen at a range of frequencies of course. And you would need to do this while translating the sinus along the x-axis to find the phase.

7
votes

You can calculate the fourier transform and look for a single spike. That would tell you that the dataset approximates a sine curve at that frequency.

0
votes

Shot into the blue: You could take advantage of the fact that the integral of a*sin(t) is a*cos(t). Keeping track of min/max of your data should allow you to know a.

0
votes

Check the least squares method.

@CookieOfFortune: I agree, but the Fourier series fit is optimal in the least squares sense (as is said on the Wikipedia article).

If you want to play around first with own input data, check the Discrete Fourier Transformation (DFT) on Wolfram Alpha. As noted before, if you want a fast implementation you should check out one of several FFT-libraries.