具有非线性趋势的Detrend Flux时间序列

时间:2015-01-02 17:47:06

标签: python numpy

我需要消除通量时间序列数据(光线曲线),但是当时间序列数据没有简单的线性趋势时,我遇到了问题。

我一直在使用scipy.signal.detrend()来消除线性情况,但这在这里还不够。

我已经使用numpy.polyfit()来尝试多项式去除趋势,但我不确定如何处理它返回的多项式系数。

有人可以告诉我下一个明智的步骤吗?或者,如果某人有更好的方法来消除非线性数据,我也很高兴听到这一点。

1 个答案:

答案 0 :(得分:10)

简而言之,你取polyfit返回的系数并将它们传递给polyval来评估观察到的“x”位置的多项式。

作为一个独立的例子,假设我们有类似于以下内容:

import numpy as np
import matplotlib.pyplot as plt

num = 1000
x = np.linspace(0, 10, num)
y = np.exp(x)

# Add some non-stationary noise that's hard to see without de-trending
noise = 100 * np.exp(0.2 * x) * np.random.normal(0, 1, num)
y += noise

fig, ax = plt.subplots()
ax.plot(x, y, 'ro')
plt.show()

enter image description here

请注意,我这里没有使用多项式函数来创建y。这是故意的。否则,我们会得到一个精确的拟合,并且不需要按照多项式的顺序“玩”。

现在让我们尝试使用二阶多项式函数去除它(请注意行model = np.polyfit(x, y, 2)中的2):

import numpy as np
import matplotlib.pyplot as plt

num = 1000
x = np.linspace(0, 10, num)
y = np.exp(x)

# Add some non-stationary noise that's hard to see without de-trending
noise = 100 * np.exp(0.2 * x) * np.random.normal(0, 1, num)
y += noise

# Detrend with a 2d order polynomial
model = np.polyfit(x, y, 2)
predicted = np.polyval(model, x)

fig, axes = plt.subplots(nrows=2, sharex=True)
axes[0].plot(x, y, 'ro')
axes[0].plot(x, predicted, 'k-')
axes[0].set(title='Original Data and 2nd Order Polynomial Trend')

axes[1].plot(x, y - predicted, 'ro')
axes[1].set(title='Detrended Residual')

plt.show()

enter image description here


请注意,我们并未完全符合数据。这是一个指数函数,我们使用的是多项式。然而,随着我们增加多项式的阶数,我们将更精确地拟合函数(冒着开始拟合噪声的风险):

enter image description here

enter image description here

enter image description here

enter image description here