The easiest way to determine the frequency of data is autocorrelation . It is also quite simple to implement. To get autocorrelation in i , you simply multiply each data point of your data by each data point shifted by i . Here are a few pseudo codes:
for i = 0 to length( data ) do autocorrel[ i ] = 0; for j = 0 to length( data ) do autocorrel[ i ] += data( j ) * data( ( j + i ) mod length( data ) ) done done
This will give you an array of values. The highest “frequency” is the index with the highest value. Thus, you can extract any periodic parts (usually there are more than one).
Also, I suggest you not try to implement your own FFT in the application. Although this algorithm is very good for learning, there are many errors that are difficult to verify, and it is also likely that your implementation will be much slower than those that are already available. If this is possible on your system, I would suggest you use FFTW , which cannot be surpassed in any respect when it comes to implementing FFT.
EDIT:
An explanation of why this works even for values that are not completely repeated:
The usual and completely correct way to calculate autocorrelation is to subtract the average from your data. Let's say you have [1, 2, 1.2, 1.8 ] . You can then extract 1.5 from each sample, leaving you with [-.5, .5, -.3, .3 ] . Now, if you multiply this by yourself with zero, the negatives will be multiplied by negative and positive positive values, giving (-.5)^2 + (.5)^2 + (-.3)^2 + (.3)^2=.68 . If one negative is shifted, it will be multiplied by positive values giving (-.5)*(.5) + (.5)*(-.3) + (-.3)*(.3) + (.3)*(-.5)=-.64 . When you shift two again, the negatives will be multiplied by negative and positive positives. With the displacement of three, something similar happens for the displacement of one. As you can see, you get positive values at offsets 0 and 2 (periods) and negative values at 1 and 4.
Now, to determine only the period, there is no need to subtract the average value. If you just leave the samples as they are, an average will be added each time you add. Since the same value will be added for each coefficient calculated, the comparison will give the same results as you if you subtract the average first. In the worst case, your data type may end (if you use some kind of integral type), or you can bypass errors when the values start to get large (in case you use a float, this is usually not a problem). In case this happens, first subtract the average and try if your results improve.
The biggest drawback of using autocorrelation compared to some kind of fast Fourier transform is speed. Autocorrelation takes O(n^2) , where only O(n log(n)) is required as FFT. If you need to very often calculate the period of very long sequences, autoscaling may not work in your case.
If you want to know how the Fourier transform works, and that all these things are about the real part and the imaginary part, magnitude and phase (see, for example, the code sent to Manu), I suggest you have a look at this book .
EDIT2:
In most cases, the data is neither completely periodic nor completely chaotic and aperiodic. Usually your data consists of several periodic additions with different strengths. A period is a time difference with which you can move your data to make it look like yourself. Autocorrelation calculates how similar the data is if you change it by a certain amount. Thus, it gives you the power of all possible periods. This means that there is no “repeating value index” because when the data is completely periodic, all indices will be repeated. The index with the strongest value gives you a shift at which the data is most similar to itself. So this index gives a temporary offset, not an index in your data. To understand this, it is important to understand how time series can be considered as consisting of a sum of perfectly periodic functions (sinusoidal basic functions).
If you need to detect this for very long time series, it is usually best to shift the window above your data and simply check the period of this smaller data frame. However, you should be aware that your window will add additional periods to your data that you should be aware of.
More details in the link that I posted in the last editor.