微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Matlab中使用xcorr和finddelay的不同采样延迟

如何解决Matlab中使用xcorr和finddelay的不同采样延迟

我正在尝试使用 Matlab 找到两个超声脉冲之间的时间延迟,我尝试使用 xcorr 和 finddelay(使用 xcorr),但我得到了两个不同的结果。我想知道为什么会这样,我可以认为这是正确的结果。理想情况下,我想让 xcorr 正常工作,因为绘制互相关会很有用。

示例数据 here

myfile = uigetfile('*.csv','MultiSelect','on');
M = readtable(myfile);
A = table2array(M);
A(1,:) = [];

Time = A(:,1);
Input = A(:,2);
Output = A(:,3);

Fs = 500e6;

d1 = finddelay(Input,Output) / Fs;
[c,lags]=xcorr(Input,Output);
d2 = -(lags(c == max(c))) / Fs; 

编辑:感谢 Mansour Torabi,我从 xcorr提取finddelay 代码以找到归一化互相关:

x = Input;
y = Output;
maxlag = 50000;

% FINDDELAY for column vectors x and y.
% The inputs cxx0 and cyy0 should be:
cxx0 = sum(abs(x).^2);
cyy0 = sum(abs(y).^2);
% Initialize some constants.
ZERO = coder.internal.indexInt(0);
ONE = coder.internal.indexInt(1);
nc = 2*maxlag + 1;
d = ZERO;
max_c = coder.internal.scalarEg(x,y);
scale = sqrt(cxx0*cyy0);
% Quick return for trivial inputs. Empty inputs will have scale == 0.
if maxlag == 0 || scale == 0
    return
end
index_max = ZERO;
index_max_pos = ONE;
index_max_neg = ONE;
[c,lag] = xcorr(x,y,maxlag);
% Process the negative lags in flipped order.
max_c_neg = abs(c(maxlag))/scale;
for k = 2:maxlag
    vneg = abs(c(maxlag - k + 1))/scale;
    if vneg > max_c_neg
        max_c_neg = vneg;
        index_max_neg = k;
    end
end
% Process the positive lags.
max_c_pos = abs(c(maxlag + 1))/scale;
for k = maxlag + 2:nc
    vpos = abs(c(k))/scale;
    if vpos > max_c_pos
        max_c_pos = vpos;
        index_max_pos = k - maxlag;
    end
end
if maxlag == 0
    % Case where MAXLAG is zero.
    index_max = index_max_pos;
elseif max_c_pos > max_c_neg
    % The estimated lag is positive or zero.
    index_max = maxlag + index_max_pos;
    max_c = max_c_pos;
elseif max_c_pos < max_c_neg
    % The estimated lag is negative.
    index_max = maxlag + 1 - index_max_neg;
    max_c = max_c_neg;
elseif max_c_pos == max_c_neg
    max_c = max_c_pos;
    if index_max_pos <= index_max_neg
        % The estimated lag is positive or zero.
        index_max = maxlag + index_max_pos;
    else
        % The estimated lag is negative.
        index_max = maxlag + 1 - index_max_neg;
    end
end
%delay in samples
d = (maxlag + 1) - index_max;
%delay in time
dfcc = d/Fs;

%convert from samples to time and take abs value (scale 0-1)
xcorrlag = abs(lag/Fs);
c = abs(c/max(abs(c)));

然后可以使用以下方法绘制:

plot(xcorrlag,c,[dfcc dfcc],[-1 1],'r:')  
text(dfcc+0.00001,0.5,['ToF: ' num2str(dfcc) ' s'])

突出显示标记最大相关性。

解决方法

在 MATLAB 中 finddelay 函数是一个 m 文件,因此您可以看到其中的代码。 如果是这样,您会发现 finddely 函数执行的是标准化互相关,而不是互相关。这就是 xcorrfinddelay 的结果略有不同的原因。

我应该注意,对于延迟检测,推荐使用归一化互相关 (NCC)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。