def cross_corr(y1, y2):
"""Calculates the cross correlation and lags without normalization.
The definition of the discrete cross correlation is in:
https://www.mathworks.com/help/matlab/ref/xcorr.html
Args:
y1, y2: Should have the same length.
Returns:
max_corr: Maximum correlation without normalization.
lag: The lag in terms of the index.
"""
if len(y1) != len(y2):
raise ValueError('The lengths of the inputs should be the same.')
y1_auto_corr = np.dot(y1, y1) / len(y1)
y2_auto_corr = np.dot(y2, y2) / len(y1)
corr = correlate(y1, y2, mode='same')
# The unbiased sample size is N - lag.
unbiased_sample_size = correlate(
np.ones(len(y1)), np.ones(len(y1)), mode='same')
corr = corr / unbiased_sample_size / np.sqrt(y1_auto_corr * y2_auto_corr)
shift = len(y1) // 2
max_corr = np.max(corr)
argmax_corr = np.argmax(corr)
return max_corr, argmax_corr - shift
先看一些例子。假设我们已经在单元测试类中了。
这是我的密码。
根据this Wikipedia article中的定义,信号之间的延迟由互相关的argmax给出。因此,下面的代码将
b_sig
与a_sig
分阶段移动,以最小化错误。相关问题 更多 >
编程相关推荐