确定两个向量之间的滞后

问题描述 投票:0回答:1

我想找到两个向量之间的最小滞后量,我的意思是基于另一个向量在向量中重复某事的最小距离,例如

x=[0 0 1 2 2 2 0 0 0 0]
y=[1 2 2 2 0 0 1 2 2 2]

我希望从x到y获得4,从y到x获得2。

我发现了一个finddelay(x,y)函数,它只适用于x到y(对于x,它给出了-4)。

是否有任何函数只能根据向量的正确方向给出滞后?如果你介意帮助我得到这个结果,我将非常感激

matlab function signal-processing delay lag
1个回答
2
投票

我认为这可能是finddelay的潜在错误。请注意文档中的摘录(强调我的):

XY不需要是彼此的精确延迟副本,因为finddelay(X,Y)通过互相关返回延迟的估计。然而,只有在XY的延迟版本之间存在足够的相关性时,这种估计的延迟才有用。此外,如果可能有多个延迟,如在周期信号的情况下,则返回具有最小绝对值的延迟。在具有相同绝对值的正延迟和负延迟都是可能的情况下,返回正延迟。

这似乎暗示finddelay(y, x)应该返回2,当它实际返回-4

编辑:

这似乎是与floating-point errors引入的xcorr相关的问题,因为我描述了in my answer to this related question。如果在命令窗口中键入type finddelay,则可以看到finddelay在内部使用xcorr。即使xcorr的输入是整数值,结果(您预期也会是整数值)最终会出现浮点错误,导致它们略大于或小于整数值。然后,这可以改变最大值所在的指数。解决方案是在知道输入都是整数值时对xcorr的输出进行舍入。

为整数值更好地实现finddelay可能是这样的,它实际上将返回具有最小绝对值的延迟:

function delay = finddelay_int(x, y)
  [d, lags] = xcorr(x, y);
  d = round(d);
  lags = -lags(d == max(d));
  [~, index] = min(abs(lags));
  delay = lags(index);
end

但是,在您的问题中,您要求返回正延迟,这绝对不一定是绝对值的最小值。这是finddelay的一个不同实现,它适用于整数值,并优先考虑正延迟:

function delay = finddelay_pos(x, y)
  [d, lags] = xcorr(x, y);
  d = round(d);
  lags = -lags(d == max(d));
  index = (lags <= 0);
  if all(index)
    delay = lags(1);
  else
    delay = lags(find(index, 1)-1);
  end
end

以下是您的测试用例的各种结果:

>> x = [0 0 1 2 2 2 0 0 0 0];
>> y = [1 2 2 2 0 0 1 2 2 2];
>> [finddelay(x, y) finddelay(y, x)]  % The default behavior, which fails to find 
                                      %   the delays with smallest absolute value
ans =

     4    -4

>> [finddelay_int(x, y) finddelay_int(y, x)]  % Correctly finds the delays with the
                                              %   smallest absolute value
ans =

    -2     2

>> [finddelay_pos(x, y) finddelay_pos(y, x)]  % Finds the smallest positive delays

ans =

     4     2
© www.soinside.com 2019 - 2024. All rights reserved.