# How to read the number of periods of this complicated graph?

I have two data sets that are quasi-periodic. They have the same period and can be seen clearly by eye. For example when $$x\in(100,200)$$, both of them have about 32 periods. Below is a zoom-in of the data: You can see that the two data sets have quite different nature.

Question is, how can I count the exact number of periods(as what we count by eye) by computer? It is best that the algorithm/program can deal with these two different behaved data sets simultaneously. I'm using python or c most. If available, code in these two language is preferred.

The data is here. It is about 15mb.

• Can you compute the Fourier Transform and find the lowest peak? Dec 13, 2015 at 18:18

You should use a Fourier transform to compute the power spectrum of your signal. Peaks correspond to major frequency components. For an exmaple, see the single sided amplitude spectrum plots here: http://www.mathworks.com/help/matlab/ref/fft.html

Edit:
It seems I misread your question. If you just want the number of periods that occur in the data set, the relevant frequency is clearly visible in the power spectrum. You are probably interested in the peak corresponding to the lowest frequency present (there are also many integer multiples of this frequency present which may even have larger amplitudes). You can pull out that frequency using some threshold to detect it and multiply by the total duration of the data to get the number of periods in the data.

In general, the overall frequency for the data to exactly repeat itself is equal to the greatest common factor of all frequencies present. Please see the frequencies obtained below, the lowest frequency peak is what you are looking for.  • this is not a good choice, you will see many peaks, i can provide the frequency spectrum when i am at computer. Dec 13, 2015 at 15:17
• Perhaps I misunderstood your question. I looks like you're trying to get total number of periods rather than the principal frequencies. See my edit. Dec 13, 2015 at 15:47
• After getting the lowest frequency you can obtain the period for this component and the number of periods given the total time. Dec 13, 2015 at 21:38
• @nicoguaro This actually works! Dec 14, 2015 at 8:12

You seem to have different "types" of periods, and different types of maxima and minima. One idea to try is to just compute the set of local extrema. Call the sequence of extremal values $v_j$ at locations $x_j$.

You might be able to distinguish the different types of maxima and minima by looking at differences in heights between, say, a minimum and the two neighbouring maximums, $|v_j-v_{j-1}|+|v_j-v_{j+1}|$, as well as looking at the size of the "basin" associated to a minimum, $x_{j+1}-x_{j-1}$. The difference in heights would be really high when the data jumps from top to bottom, and you would also get a much smaller difference for the "minor" maxima on the bottom and the top. This can also help get rid of noise if the differences are negligible.

Computing the number of periods would be as simple as counting the number of maxima of the right type, or maybe computing the distance between them as $x_{j+2}-x_j$. This would also allow you to distinguish separately the high-amplitude and low-amplitude oscillations, if necessary.

I am reminded of the literature in scientific visualization on using Morse-Smale complexes to extract features from 3d scalar fields (e.g. http://dx.doi.org/10.1109/TVCG.2007.70552), but that's overkill here as the 1d case is really simple on its own.