【问题标题】:Matlab: xcorr 1d cross-correlation normalisation issueMatlab:xcorr 1d 互相关归一化问题
【发布时间】:2017-01-01 21:05:09
【问题描述】:

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

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

我用零填充 s1(因此它与 xcorr 'coeff' 选项所需的长度相同):

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 的最大相关值,但在 19 个样本的滞后时最大相关值为 0.4776。我不明白这个?

如果我让 s1 = s2 并重复 xcorr(现在 s1 和 s2 是相同的),我在 0 样本滞后时得到最大相关值 1.0,正如预期的那样。

【问题讨论】:

  • 如果 s2 包含相同的段,请查看我的更新以获取您的点 max correlation=1

标签: matlab normalization cross-correlation


【解决方案1】:

延迟对应于最大峰值(但不一定是 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

【讨论】:

  • 谢谢,我理解,但是可以对标准化进行修改,因此如果 s2 包含相同的 s1 段,则最大相关 =1。即在您的示例中。
  • 我认为这就是 normxcorr2 用于 2d 模板匹配的方式
  • 如果 s2 包含相同的段,请查看我的更新以获取您的点最大相关性=1
  • 我没有函数 {alignsignals}。我认为这是在较新的 matlab 版本中,但我认为可以编写自己的脚本来仅使用 xcorr 和填充来执行此操作。我很惊讶 matlab 中没有 1d 等效的 normxcorr2。
  • 函数 {alignsignals} 在 MATLAB 版本:R13 中可用
猜你喜欢
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
相关资源
最近更新 更多