Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

timedelay - Different sample delays using xcorr and finddelay in Matlab

I am trying to find the time delay between two ultrasound pulses using Matlab, I have tried using xcorr and finddelay (which uses xcorr) but I get two different results. I would like to know why this is, and which I can consider to be the correct result. Ideally I would like to get xcorr working correctly as it would be useful to plot the cross-correlation.

Example data 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; 

Edit: Thanks to Mansour Torabi I have extracted the xcorr code from finddelay to find the normalised cross correlation:

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)));

This can then be plotted using:

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

The maximum correlation is highlighted and labelled.

question from:https://stackoverflow.com/questions/65836210/different-sample-delays-using-xcorr-and-finddelay-in-matlab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In MATLAB finddelay function is a m-file, so you can see the codes inside it. If so, you will find that the finddelyfunction do the Normalized Cross-Correlation instead of Cross-Correlation. This is the reason that results of xcorr and finddelay are slightly different.

And I should note that for delay detection, Normalized Cross-Correlation (NCC) is recommended.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...