11

Assume I have a time series t with one hundred measurements, each entry representing the measured value for each day. I assume there is some periodicity in the signal -- it might repeat daily, weekly or monthly.

Translating the time series into the Fourier domain might help to find such a periodicity?

How could I use numpy's fft module to find the likeliest period for my time series?

Marcel
  • 335
  • 1
  • 2
  • 10
  • Do a FFT to the data and then see which is the periodicity with the biggest amplitude. – Ignacio Vergara Kausel Jun 28 '17 at 13:01
  • You mean the FFT's array index with the largest entry (amplitude) is the periodicity? – Marcel Jun 28 '17 at 13:02
  • Is the periodicity that most influences your data. In principle any only amplitudes equal to 0 are periodicities that do not contribute. – Ignacio Vergara Kausel Jun 28 '17 at 13:05
  • I don't understand what is the relationship between fft and fftfreq functions? – Marcel Jun 28 '17 at 13:25
  • RE `fftfreq`, I have written [a nice answer](https://stackoverflow.com/a/27191172/2749397) about its use in relation to `numpy.fft.fft` — of course the [question I answered](https://stackoverflow.com/q/3694918/2749397) received other good, or better, answers... – gboffi Jun 28 '17 at 13:45
  • 1
    If you want to find year long periods (and especially locations of min/max) you need a sufficiently long data series. – gboffi Jun 28 '17 at 13:50
  • Ooops, you didn't speak of year long periods, sorry for the confusion. However 100 points to estimate periods of about a month could be not enough. – gboffi Jun 28 '17 at 14:06
  • Rather than doing a crude DFT I'd try to read about _periodogram_ The Scipy docs have [a nice example](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.signal.periodogram.html) of its use to detect a hidden periodicity (2nd part of web page). – gboffi Jun 28 '17 at 14:12
  • I think your best bet is to find the local extremes (local minima/maxima) and then finding the harmonic behavior there. I wrote a sample code [here](https://stackoverflow.com/a/50836425/4999991) – Foad S. Farimani Jun 13 '18 at 19:48

3 Answers3

6

I will aim to answer my own question. You may correct me where appropriate.

Asume our time series t is t = [2,1,0,1,2,3,2,1,0,1,2,3,2,1,0,1,2,3] with 18 measurements. A rather simple example: It seems likely that the length of the period is six time units.

Taking this time series into the Frequency Domain yields us:

    w = numpy.fft.fft(t)
    print numpy.absolute(w)
    array([27.000000, 0.000000, 0.000000, 12.000000, 0.000000, 0.000000,
   0.000000, 0.000000, 0.000000, 3.000000, 0.000000, 0.000000,
   0.000000, 0.000000, 0.000000, 12.000000, 0.000000, 0.000000])

We ignore frequency 0 and observe that the value is largest for index 3 -- this indicates that within our time series t the signal repeats 3 times. Hence the length of the signal -- the period -- would be 18/3 = 6. And indeed:

numpy.fft.fftfreq(18)
array([ 0.      ,  0.055556,  0.111111,  0.166667,  0.222222,  0.277778,
    0.333333,  0.388889,  0.444444, -0.5     , -0.444444, -0.388889,
   -0.333333, -0.277778, -0.222222, -0.166667, -0.111111, -0.055556])

The frequency at index 3 is exactly 1/6, i.e. the frequency for one time unit is 1/6, meaning the signal takes six time units for a full period.

Please let me know if my understanding is correct.

Marcel
  • 335
  • 1
  • 2
  • 10
1

Note than an FFT finds a sinusoidal decomposition, which is different from a periodicity estimator (as any fundamental period can be completely missing from a periodic signal's frequency spectrum. See missing fundamental )

So you will need to post-process your FFT result with something like a cepstrum (using complex cepstral analysis, etc.), or perhaps use a Harmonic Product Spectrum estimator.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
1

There is another method that does not rely on Fourier Series. This method helps you identify if the signal is periodic. Ideally, the time series, in this case, should be binary:

[0,1,0,0,0,1,1,0,0,0,1,0,0,1]

You first compute the distribution of the distances between the locations of consecutive peaks. Then, you compute the factor

-1<B<1:

B = (var - mean)/(var + mean)

With mean and varrespectively the mean and the variance of the computed distribution. The closerBto-1the more periodic the signal is. IfB` close to 0, then there is no periodicity in the signal and the peaks are located randomly in the time series.

For further information, look up for the keyword Burstiness.

kkuilla
  • 2,226
  • 3
  • 34
  • 37
alpha027
  • 302
  • 2
  • 13
  • Is there any reference to this theory? Looking for simple burstiness does not help much [example] (https://en.wikipedia.org/wiki/Burstiness). Thanks in advance. – Soumyajit Chatterjee Feb 08 '21 at 20:01
  • 1
    Yes, here is the link https://iopscience.iop.org/article/10.1209/0295-5075/81/48002/pdf Figure 1 is a good illustration – alpha027 Feb 09 '21 at 13:04