7

I have a periodic signal I would like to find the period. Raw signal

Since there is border effect, I first cut out the border and keep N periods by looking at the first and last minima.

Signal

Then, I compute the FFT.

Code:

import numpy as np

from matplotlib import pyplot as plt

# The list of a periodic something
L = [2.762, 2.762, 1.508, 2.758, 2.765, 2.765, 2.761, 1.507, 2.757, 2.757, 2.764, 2.764, 1.512, 2.76, 2.766, 2.766, 2.763, 1.51, 2.759, 2.759, 2.765, 2.765, 1.514, 2.761, 2.758, 2.758, 2.764, 1.513, 2.76, 2.76, 2.757, 2.757, 1.508, 2.763, 2.759, 2.759, 2.766, 1.517, 4.012]
# Round because there is a slight variation around actually equals values: 2.762, 2.761 or 1.508, 1.507
L = [round(elt, 1) for elt in L]

minima = min(L)
min_id = L.index(minima)

start = L.index(minima)
stop = L[::-1].index(minima)

L = L[start:len(L)-stop]

fft = np.fft.fft(np.asarray(L))/len(L)
fft = fft[range(int(len(L)/2))]

plt.plot(abs(fft))

I know how much time I have between 2 points of my list (i.e. the sampling frequency, in this case 190 Hz). I thought that the fft should give me a spike at the value corresponding to the number of point in a period, , thus giving me the number of point and the period. Yet, that is not at all the output I observed:

FFT

My current guess is that the spike at 0 corresponds to the mean of my signal and that this little spike around 7 should have been my period (although, the repeating pattern only includes 5 points).

What am I doing wrong? Thanks!

Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • Maybe it has something to do with the unbalanced structure of the signal? (4 high value for each 1 low value)? – Shir Mar 28 '18 at 11:16
  • It is unbalanced but still periodic. My courses about signal processing are a bit far behind me, but an fft should give me spike at the frequency of the signal and at its harmonics. – Mathieu Mar 28 '18 at 12:27

3 Answers3

5

Your data is correct, it's just that you are not preprocessing it correctly:

  1. The first giant peak is the DC/average value of your signal. If you subtracted it before taking the DFT, it would disappear
  2. Not windowing the signal before taking the DFT will produce ringing in the DFT spectrum, lowering the peaks and raising the "non-peaks".

If you include these two steps, the result should be more as you expect:

import numpy as np
import scipy.signal

from matplotlib import pyplot as plt

L = np.array([2.762, 2.762, 1.508, 2.758, 2.765, 2.765, 2.761, 1.507, 2.757, 2.757, 2.764, 2.764, 1.512, 2.76, 2.766, 2.766, 2.763, 1.51, 2.759, 2.759, 2.765, 2.765, 1.514, 2.761, 2.758, 2.758, 2.764, 1.513, 2.76, 2.76, 2.757, 2.757, 1.508, 2.763, 2.759, 2.759, 2.766, 1.517, 4.012])
L = np.round(L, 1)
# Remove DC component
L -= np.mean(L)
# Window signal
L *= scipy.signal.windows.hann(len(L))

fft = np.fft.rfft(L, norm="ortho")

plt.plot(L)
plt.figure()
plt.plot(abs(fft))

You will note that you will see a peak at around 8, and another one at twice that, 16. This is also expected: A periodic signal is always periodic after n*period samples, where n is any natural number. In your case: n*8.

Nils Werner
  • 34,832
  • 7
  • 76
  • 98
5

Once the DC part of the signal is removed, the function can be convolved with itself to catch the period. Indeed, the convolution will feature peaks at each multiple of the period. The FFT can be applied to compute the convolution.

fft = np.fft.rfft(L, norm="ortho")

def abs2(x):
    return x.real**2 + x.imag**2

selfconvol=np.fft.irfft(abs2(fft), norm="ortho")

The first output is not that good because the size of the image is not a multiple of the period.

self convolution of periodic signal by fft

As noticed by Nils Werner, a window can be applied to limit the effect of spectral leakage. As an alternative, the first crude estimate of the period can be used to trunk the signal and the procedure can be repeated as I answered in How do I scale an FFT-based cross-correlation such that its peak is equal to Pearson's rho.

self convolution of periodic signal by fft(2)

From there, getting the period boils down to finding the first maximum. Here is a way it could be done:

import numpy as np
import scipy.signal

from matplotlib import pyplot as plt

L = np.array([2.762, 2.762, 1.508, 2.758, 2.765, 2.765, 2.761, 1.507, 2.757, 2.757, 2.764, 2.764, 1.512, 2.76, 2.766, 2.766, 2.763, 1.51, 2.759, 2.759, 2.765, 2.765, 1.514, 2.761, 2.758, 2.758, 2.764, 1.513, 2.76, 2.76, 2.757, 2.757, 1.508, 2.763, 2.759, 2.759, 2.766, 1.517, 4.012])
L = np.round(L, 1)
# Remove DC component, as proposed by Nils Werner
L -= np.mean(L)
# Window signal
#L *= scipy.signal.windows.hann(len(L))

fft = np.fft.rfft(L, norm="ortho")

def abs2(x):
    return x.real**2 + x.imag**2

selfconvol=np.fft.irfft(abs2(fft), norm="ortho")
selfconvol=selfconvol/selfconvol[0]

plt.figure()
plt.plot(selfconvol)
plt.savefig('first.jpg')
plt.show()


# let's get a max, assuming a least 4 periods...
multipleofperiod=np.argmax(selfconvol[1:len(L)/4])
Ltrunk=L[0:(len(L)//multipleofperiod)*multipleofperiod]

fft = np.fft.rfft(Ltrunk, norm="ortho")
selfconvol=np.fft.irfft(abs2(fft), norm="ortho")
selfconvol=selfconvol/selfconvol[0]

plt.figure()
plt.plot(selfconvol)
plt.savefig('second.jpg')
plt.show()


#get ranges for first min, second max
fmax=np.max(selfconvol[1:len(Ltrunk)/4])
fmin=np.min(selfconvol[1:len(Ltrunk)/4])
xstartmin=1
while selfconvol[xstartmin]>fmin+0.2*(fmax-fmin) and xstartmin< len(Ltrunk)//4:
    xstartmin=xstartmin+1

xstartmax=xstartmin
while selfconvol[xstartmax]<fmin+0.7*(fmax-fmin) and xstartmax< len(Ltrunk)//4:
    xstartmax=xstartmax+1

xstartmin=xstartmax
while selfconvol[xstartmin]>fmin+0.2*(fmax-fmin) and xstartmin< len(Ltrunk)//4:
    xstartmin=xstartmin+1

period=np.argmax(selfconvol[xstartmax:xstartmin])+xstartmax

print "The period is ",period
jtlz2
  • 7,700
  • 9
  • 64
  • 114
francis
  • 9,525
  • 2
  • 25
  • 41
  • Thanks for the solution. However, I can't produce the second graph without frequency leakage. – Mathieu Mar 29 '18 at 08:52
  • And second part I don't get is the trunk: `Ltrunk == L` with the way you defined it – Mathieu Mar 29 '18 at 09:04
  • The length of `Ltrunk` is `(len(L)/multipleofperiod)*multipleofperiod`. But it is an integer division : as soon as `len(L)` is not a multiple of `multipleofperiod`, `len(Ltrunk)` becomes different from `len(L)`. Indeed, it is a multiple of `multipleofperiod`, that is a multiple of the period, if the `multipleofperiod` stemming from the first pass is accurate. Then, spectral leakage is removed. Otherwise, if the estimated `multipleofperiod` is significantly different from the period, the second estimate will also be plagued by spectral leakage. – francis Mar 29 '18 at 16:32
  • Maybe it is only a problem from my python, but I can't use list[a:b] if a and b are not integers... It raises an error every time. – Mathieu Mar 29 '18 at 20:17
  • I think I got it! See https://stackoverflow.com/questions/1282945/python-integer-division-yields-float You are likely using python 3, where `/` denotes a floating point division. Could you try to use `//`? – francis Mar 29 '18 at 21:51
3

The peak in an FFT magnitude result represents frequency, which is the reciprocal of the period. Multiply the frequency index reciprocal by the FFT window length to get the period result in the same units at the window length.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153