Matlab:xcorr 1d互相关归一化问题 [英] Matlab: xcorr 1d cross-correlation normalisation issue

查看:300
本文介绍了Matlab:xcorr 1d互相关归一化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长度= 5的参考信号(s1)和另一个长度= 25个样本的信号(s2)(包含相同的5个样本信号s1的移位版本).

I have a reference signal (s1) of length = 5 and another signal (s2) of length = 25 samples (containing a shifted version of the same 5 sample signal s1).

我想找到两个信号之间的归一化互相关,以计算信号s1和s2之间的采样距离(延迟/滞后).

I want to find the normalised cross correlation between the two signals to calculate the sample distance (delay / lag) between signals s1 and s2.

我将s1填充为零(因此与xcorr'coeff'选项所需的长度一样,它与s2相同):

I pad s1 with zeros (so it is the same length as s2 as required for xcorr 'coeff' option):

s1(numel(s2)) = 0;

然后执行:

[R lags]=xcorr(s2,s1,'coeff');

[vm im]=max(R); %max. correlation and index
s1_lag=lags(im);

查找-24到24个样本的滞后的归一化互相关.

to find the normalised cross-correlation for lags of -24 to 24 samples.

由于s2包含s1的移位版本,因此我希望获得最大相关值1,但是在19个样本的滞后时最大相关值为0.4776.我不明白吗?

Since s2 contains a shifted version of s1 I would expect to obtain a maximum correlation value of 1 but maximum correlation is 0.4776 at a lag of 19 samples. I don't understand this?

如果让s1 = s2并重复xcorr(现在s1和s2相同),则在0个样本滞后时,我得到的最大相关值为1.0.

If I let s1 = s2 and repeat xcorr (now s1 and s2 are identical) I get a maximum correlation value of 1.0 at 0 sample lag as expected.

推荐答案

延迟对应于最大峰值(但不一定为1(1将是同一矢量之间的xcorr值-腿0处的自动相关) 并且两个相反的信号的值为-1).

The delay corresponds to the max peak value (but not necessarily 1 ( 1 will be the value of xcorr between the same vector - auto correlation at leg 0) and two opposite signals will have value of -1).

xcorr提供规范化方案.语法 xcorr(x,y,'coeff')

xcorr provides normalization scheme. The syntax xcorr(x,y,'coeff')

将输出除以norm(x)* norm(y),以便进行自相关时,零滞后的样本为1.

divides the output by norm(x)*norm(y) so that, for auto-correlations, the sample at zero lag is 1.

http://matlab.izmiran.ru/help/toolbox/signal /spectra3.html

以下代码将两个信号作为OP计算xcorr,延迟为5:

The following code compute xcorr for the two signal as OP, with delay 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中的规范化没有意义.

For signals of unequal length and padding the shorter with zeros, normalization in xcorr has no meaning.

您可以使用xcorr(s1,s2,'none')或xcorr(s1,s2),并且xcor在内部将较短的信号填充为零以得到相等的长度.

You can use xcorr (s1,s2,'none') or xcorr (s1,s2 ) and xcor internally pads the shorter signal with zeros for equal length.

您将获得峰值位置,该位置指示两个信号最相似的时间偏移. 在我们的示例中,使用xcorr(s1,s2,'none'),结果为: vm = 55.0000
s1_lag = 5

You get the the position of peak value which indicates the time offset at which the two signals are the most similar. in our example, using xcorr (s1,s2,'none'), the result is: vm = 55.0000
s1_lag = 5

在Matlab中,有一个名为: alignsignals 的函数可以与xcorr一起使用,如以下代码所示:

In Matlab there is a function named : alignsignals can be used with xcorr as in the following code:

    % 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:

The following plot is the resultant 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

这篇关于Matlab:xcorr 1d互相关归一化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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