経験的方法
ここで、この問題を楽しく取り上げます。あなたの質問のより一般的な形式は次のとおりです。
未知の長さの繰り返しシーケンスが与えられた場合、信号の周期を決定します。
繰り返し周波数を決定するプロセスは、フーリエ変換として知られています。あなたの例では、信号はきれいで離散的ですが、次のソリューションは連続したノイズの多いデータでも機能します! FFTは、いわゆる「波空間」または「フーリエ空間」で近似することにより、入力信号の周波数を複製しようとします。基本的に、この空間のピークは反復信号に対応します。信号の周期は、ピークとなる最長の波長に関連しています。
import itertools
# of course this is a fake one just to offer an example
def source():
return itertools.cycle((1, 0, 1, 4, 8, 2, 1, 3, 3, 2))
import pylab as plt
import numpy as np
import scipy as sp
# Generate some test data, i.e. our "observations" of the signal
N = 300
vals = source()
X = np.array([vals.next() for _ in xrange(N)])
# Compute the FFT
W = np.fft.fft(X)
freq = np.fft.fftfreq(N,1)
# Look for the longest signal that is "loud"
threshold = 10**2
idx = np.where(abs(W)>threshold)[0][-1]
max_f = abs(freq[idx])
print "Period estimate: ", 1/max_f
これにより、この場合の正しい答えが得られますが、サイクルがきれいに分割されていない10
場合は、近似の見積もりが得られます。N
これを次の方法で視覚化できます。
plt.subplot(211)
plt.scatter([max_f,], [np.abs(W[idx]),], s=100,color='r')
plt.plot(freq[:N/2], abs(W[:N/2]))
plt.xlabel(r"$f$")
plt.subplot(212)
plt.plot(1.0/freq[:N/2], abs(W[:N/2]))
plt.scatter([1/max_f,], [np.abs(W[idx]),], s=100,color='r')
plt.xlabel(r"$1/f$")
plt.xlim(0,20)
plt.show()
