Determining if a dataset approximates a sine wave
Asked Answered
R

4

9

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

Rothwell answered 14/6, 2009 at 9:26 Comment(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).Supernal
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).Uncharitable
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?Quintic
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.Lambrecht
S
18

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.

Silk answered 14/6, 2009 at 9:35 Comment(3)
Plural seems to be sinus also... That's the problem when not typing in your mothers tongue. Sinai then :) ?Silk
A good estimate of the quality will be the height of the very high peak, divided by the sum of the heights of other peaks. Using this measure, 1 means a perfect fit, 0 means no fit at all.Horseman
Martijn, that is called THD (en.wikipedia.org/wiki/Total_harmonic_distortion) or THD+N, depending on what you include.Joerg
L
7

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.

Lacteal answered 14/6, 2009 at 9:36 Comment(2)
that does unfortunatley only work if the sine-wave has exactly the same frequency as one fft-bin. If it's somewhere between the bins you get a completely different spectrum.Longford
You will get a single prominent spike no matter what the frequency is. The question doesn't ask to determine the frequency; just to determine whether it's a sine or not.Joerg
C
0

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.

Carbonyl answered 14/6, 2009 at 9:31 Comment(1)
The sine wave probably has a phase offset, which would make using the least squares method more difficult to implement.Uncharitable
W
0

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.

Walkout answered 14/6, 2009 at 9:32 Comment(2)
∫(a·sin(k·t+m) + b) dt = (-a/k)·cos(k·t+m) + b·t + CIncrement
I'm not sure this is a fruitful direction to go in. However, there might be use in noting that the 2nd derivative of a sine is proportional to, and negative, of the original. d2/dx2)sin(kx) = -k^2 sin(x). To reduce effects of noise and roundoff error, use a smoothing derivative - read up on Savitzky-Golay filtering.Quintic

© 2022 - 2024 — McMap. All rights reserved.