我正在尝试使用水位时间序列的numpy Fourier变换方法进行谐波拟合。我有一个月的数据(两个春季/春季周期),我想推断未来的水位。数据本身(数据集中的前400个点)拟合得很好,但是在那一点之后,可以看到外推中出现奇怪的跳跃,并且相位和两足动物不再对应。似乎它只是复制拟合并将其粘贴在后面。有谁知道解决方案吗? 我使用的代码基于:https://gist.github.com/tartakynov/83f3cd8f44208a1856ce
def fourierExtrapolation(x, n_predict):
n = x.size
n_harm = 100 # number of harmonics in model
t = np.arange(0, n)
p = np.polyfit(t, x, 1) # find linear trend in x
x_notrend = x - p[0] * t # detrended x
x_freqdom = fft.fft(x_notrend) # detrended x in frequency domain
f = fft.fftfreq(n) # frequencies
indexes = list(range(n))
# sort indexes by frequency, lower -> higher
indexes.sort(key = lambda i: np.absolute(f[i]))
t = np.arange(0, n + n_predict)
restored_sig = np.zeros(t.size)
for i in indexes[:1 + n_harm * 2]:
ampli = np.absolute(x_freqdom[i]) / n # amplitude
phase = np.angle(x_freqdom[i]) # phase
restored_sig += ampli * np.cos(2 * np.pi * f[i] * t + phase)
return restored_sig + p[0] * t
y=df["Water Level EGM08 [m]"][:400]
y2=df["Water Level EGM08 [m]"]
n_predict = 800
extrapolation = fourierExtrapolation(y, n_predict)
pl.figure(figsize=(20,10))
pl.plot(np.arange(0,y2.size),y2,'--b',label='measurement')
pl.plot(np.arange(0, extrapolation.size), extrapolation, 'r', label = 'extrapolation')
pl.legend()
pl.show()