I具有长度= 5的参考信号(S1),另一个信号(S2)的长度= 25个样本(包含相同5个样本信号S1的移位版本)。 我想找到两个信号之间的归一化跨相关性,以计算信号S1和S2之间的样品距离(延迟 /滞后)。 带有零的
IPAD S1(因此,它与XCorr'Coeff'选项所需的S2长度相同):
s1(numel(s2)) = 0;
然后做:
[R lags]=xcorr(s2,s1,'coeff');
[vm im]=max(R); %max. correlation and index
s1_lag=lags(im);
找到-24至24个样品的滞后的归一化互相关。
由于S2包含S1的转移版本,我希望获得1个最大相关值1,但最大相关性为0.4776,在19个样本的滞后。我不明白吗?
如果我让S1 = S2和重复XCorr(现在S1和S2相同),我的最大相关值在0样本滞后时获得1.0的最大相关值。
延迟对应于最大峰值值(但不一定是1(1是XCORR的值,在腿0. 两个相反的信号的值为-1)。
XCORR提供归一化方案。语法 Xcorr(x,y,'coeff')
以下代码计算两个信号为OP的XCorr,延迟5:
% s1) of length = 5 and another signal (s2) of length = 25
s1=[1 2 3 4 5]
s2=[0 0 0 0 0 s1 1 1 2 3 1 5 2 3 2 4 1 ]
s1(numel(s2)) = 0;
s1
[R lags]=xcorr(s2,s1,'coeff');
[vm im]=max(R) %max. correlation and index
s1_lag=lags(im)
% review the plot
figure
plot(lags,R), hold on,plot(s1_lag,vm,'ro'),ylabel('Amplitude'),xlabel('lag[n]');
title('cross-correlation');
% result
%vm = 0.5756
%im = 31
%s1_lag = 5
结果是:
Max = 0.5756 (need not to be 1, but it is the peak value)
delay = 5 ( match the actual delay between the two signal which is 5)
xcorr的情节 update:
对于长度不相等的信号并用零填充零件,Xcorr中的归一化没有意义。
您可以使用Xcorr(S1,S2,'None')或Xcorr(S1,S2)和Xcor在内部用零信号填充零值。
您获得峰值的位置,该位置指示两个信号最相似的时间偏移。
在我们的示例中,使用XCorr(S1,S2,'none'),结果是:
VM =55.0000
在MATLAB中,有一个命名的函数:alignSignals
可以与XCorr一起使用,如以下代码:
% method 2: align signals and xcorr for the new aligned signals .
%in that case you get max of xcor = 1, delay =0
[Xa,Ya] = alignsignals(s2,s1)
% after aligning signals, take the part of signal Xa with equal lentht of Ya
[R2 lags2]=xcorr(Xa(1:length(Ya)),Ya,'coeff');
[vm2 im2]=max(R2) %max. correlation and index
s1_lag2=lags2(im2)
figure
plot(lags2,R2), hold on,plot(s1_lag2,vm2,'ro'),ylabel('Amplitude'),xlabel('lag[n]');
title('cross-correlation2');
以下图是结果XCorr:
反式信号
Xa = 0 0 0 0 0 1 2 3 4 5 1 1 2 3 1 5 2 3 2 4 1
Ya = 0 0 0 0 0 1 2 3 4 5
您在预期位置无法获得corr = 1的事实是因为Mathlab中的归一化(也可能在其他软件包中)无法正确执行。实际上,互相关是两个数据阵列之间的“滑点乘积”。获得-1
您可以在等式中找到正确的公式。 (1)以下文章
)