Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
898 views
in Technique[技术] by (71.8m points)

python - Find period of a signal out of the FFT

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...