检测两个信号之间的多个时移

时间:2019-04-21 21:43:06

标签: python statistics time-series data-science cross-correlation

我有两个时间序列的温度数据。两者都具有(如果它们在相同的时区中,则非常相似的基本功能(年度和每日季节)

信号A以UTC0每小时频率进行测量。度量例如下午1点为5°C表示中午至下午1点之间的平均温度为5°C。

信号B的时区未知,并且以10分钟为间隔进行测量。测量例如下午1点5°C表示平均温度在12.50和1pm之间为5°C。此外,信号B可能会有多个时区变化。

我想写一种能够检测信号B时区的算法。

我认为多个时间间隔的互相关可能是解决此问题的方法,所以我尝试了以下方法:

  1. I使用每小时的平均值将信号B汇总到一个小时的频率。

  2. 我将模型拟合到Signal A以获取Signal A的残差。

  3. 我从信号B中减去信号A的模型,以获得信号B的残差。

  4. 我对两个残差的多个滞后进行了互相关,并进行了连续间隔(我在这里尝试了从几周到三个月的多个间隔)

不幸的是,我得到的结果相当疯狂,而且似乎无处不在。

'''
For this example I am making the assumption that the signal consists of two sine waves.
'''

# Creating the two signals
x = np.arange(0, 4*math.pi, 0.01)

signalA = np.sin(x) + np.sin(x*10) + np.random.randn(len(x))*0.1
signalB = np.sin(x) + np.sin(x*10) + np.random.randn(len(x))*0.1
data = pd.DataFrame()
data['A'] = signalA
data['B'] = signalB
data.loc[500:, 'B'] = data.loc[500:, 'B'].shift(10)
data.loc[800:, 'B'] = data.loc[800:, 'B'].shift(-15)

# Pre-whiten
data['Aw'] = data.A - (np.sin(x) + np.sin(x*10))
data['Bw'] = data.B - (np.sin(x) + np.sin(x*10))

# Find max cross correlation
def crosscorr(d1, d2, lag):
    return d1.corr(d2.shift(lag))

def max_lag(d1, d2):
    d = {}
    for i in range(-30, 31):
        d[crosscorr(d1, d2, i)] = i
    return d[max(d)]

d_index = []
d_lag = []

for i in range(100, len(x), 10):
    d_index.append(i)
    d_lag.append(max_lag(data[i-100:i].Aw, data[i-100:i].Bw))

# Plot result
plt.scatter(d_index, d_lag)

我想将ccf的argmax从0:500设为0,将500:800设为10,将800:end设为-5,但是我得到的结果到处都是。

0 个答案:

没有答案