使用互相关找到两个信号的时移 [英] Find time shift of two signals using cross correlation

查看:93
本文介绍了使用互相关找到两个信号的时移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相互关联的信号,并同时被两个不同的测量设备捕获. 由于这两个测量值不是时间同步的,因此我要计算它们之间的时间延迟很小.另外,我需要知道哪个信号是领先信号.

可以假定以下内容:

  • 没有噪音或只有很少的噪音
  • 算法的速度不是问题,只有准确性和鲁棒性
  • 以高采样率(> 10 kHz)捕获信号几秒钟
  • 预期时间延迟为< 0.5秒

我为此目的使用互相关. 非常感谢您提供有关如何在Python中实现该功能的任何建议.

请告诉我是否应该提供更多信息,以便找到最合适的算法.

解决方案

一种流行的方法:时移是与最大互相关系数相对应的滞后.这是一个示例的工作方式:

import matplotlib.pyplot as plt
from scipy import signal
import numpy as np


def lag_finder(y1, y2, sr):
    n = len(y1)

    corr = signal.correlate(y2, y1, mode='same') / np.sqrt(signal.correlate(y1, y1, mode='same')[int(n/2)] * signal.correlate(y2, y2, mode='same')[int(n/2)])

    delay_arr = np.linspace(-0.5*n/sr, 0.5*n/sr, n)
    delay = delay_arr[np.argmax(corr)]
    print('y2 is ' + str(delay) + ' behind y1')

    plt.figure()
    plt.plot(delay_arr, corr)
    plt.title('Lag: ' + str(np.round(delay, 3)) + ' s')
    plt.xlabel('Lag')
    plt.ylabel('Correlation coeff')
    plt.show()

# Sine sample with some noise and copy to y1 and y2 with a 1-second lag
sr = 1024
y = np.linspace(0, 2*np.pi, sr)
y = np.tile(np.sin(y), 5)
y += np.random.normal(0, 5, y.shape)
y1 = y[sr:4*sr]
y2 = y[:3*sr]

lag_finder(y1, y2, sr)

对于有噪声的信号,通常首先应用带通滤波器.对于谐波噪声,可以通过识别和消除频谱中存在的频率尖峰来消除.

I have two signals which are related to each other and have been captured by two different measurement devices simultaneously. Since the two measurements are not time synchronized there is a small time delay between them which I want to calculate. Additionally, I need to know which signal is the leading one.

The following can be assumed:

  • no or only very less noise present
  • speed of the algorithm is not an issue, only accuracy and robustness
  • signals are captured with an high sampling rate (>10 kHz) for several seconds
  • expected time delay is < 0.5s

I though of using-cross correlation for that purpose. Any suggestions how to implement that in Python are very appreciated.

Please let me know if I should provide more information in order to find the most suitable algorithmn.

解决方案

A popular approach: timeshift is the lag corresponding to the maximum cross-correlation coefficient. Here is how it works with an example:

import matplotlib.pyplot as plt
from scipy import signal
import numpy as np


def lag_finder(y1, y2, sr):
    n = len(y1)

    corr = signal.correlate(y2, y1, mode='same') / np.sqrt(signal.correlate(y1, y1, mode='same')[int(n/2)] * signal.correlate(y2, y2, mode='same')[int(n/2)])

    delay_arr = np.linspace(-0.5*n/sr, 0.5*n/sr, n)
    delay = delay_arr[np.argmax(corr)]
    print('y2 is ' + str(delay) + ' behind y1')

    plt.figure()
    plt.plot(delay_arr, corr)
    plt.title('Lag: ' + str(np.round(delay, 3)) + ' s')
    plt.xlabel('Lag')
    plt.ylabel('Correlation coeff')
    plt.show()

# Sine sample with some noise and copy to y1 and y2 with a 1-second lag
sr = 1024
y = np.linspace(0, 2*np.pi, sr)
y = np.tile(np.sin(y), 5)
y += np.random.normal(0, 5, y.shape)
y1 = y[sr:4*sr]
y2 = y[:3*sr]

lag_finder(y1, y2, sr)

In the case of noisy signals, it is common to apply band-pass filters first. In the case of harmonic noise, they can be removed by identifying and removing frequency spikes present in the frequency spectrum.

这篇关于使用互相关找到两个信号的时移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆