首页 > 解决方案 > 检测两个信号之间的多个时移

问题描述

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

信号 A 以 UTC0 为单位,以小时为单位进行测量。例如,下午 1 点测量为 5°C 意味着中午和下午 1 点之间的平均温度为 5°C。

信号 B 的时区未知,每隔 10 分钟测量一次。例如,下午 1 点测量 5°C 意味着 12.50 和下午 1 点之间的平均温度为 5°C。此外,信号 B 可能有多个时区变化。

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

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

  1. 我使用每小时的平均值将信号 B 聚合为每小时频率。

  2. 我将模型拟合到信号 A 以获得信号 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,但我得到的结果到处都是。

标签: pythonstatisticstime-seriesdata-sciencecross-correlation

解决方案


推荐阅读