延迟对应于最大峰值(但不一定是 1(1 将是同一向量之间的 xcorr 的值 - 腿 0 处的自动相关)
并且两个相反的信号的值为 -1)。
xcorr 提供标准化方案。语法
xcorr(x,y,'coeff')
将输出除以 norm(x)*norm(y),因此对于自相关,零滞后的样本为 1。
http://matlab.izmiran.ru/help/toolbox/signal/spectra3.html
以下代码将两个信号的 xcorr 计算为 OP,延迟为 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 的剧情
更新:
对于长度不等且较短的用零填充的信号,xcorr 中的归一化没有意义。
您可以使用 xcorr (s1,s2,'none') 或 xcorr (s1,s2 ),并且 xcor 在内部用零填充较短的信号以保持相等的长度。
你得到峰值的位置,它表示两个信号最相似的时间偏移。
在我们的示例中,使用 xcorr (s1,s2,'none'),结果是:
vm = 55.0000
s1_lag = 5
在 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