How do I find the time delay between two signals?

17 次查看(过去 30 天)
This is a program I am using to find the delay between two signals with different sampling frequencies. My time signal is from 1 to 10.
The actual delay in the plot is 1.382 but I am getting 109 when I calculate the cross-correlation and find the max value.
Please help me figure out how to get exact value in this case.
x = sin(2*pi*(1:.025:20));
t1 = linspace(1,10,length(x));
x = rand(1,t1)*x;
y = sin(2*pi*(1:.0123:30));
t2 = linspace(1,10,length(y));
y = rand(1,t2)*y;
y = [zeros(1,100) y(1:end-100)];
figure
plot(t1,x);hold on;plot(t2,y,'-r')
s1 =x;
s2 =y;
c = xcorr(s2,s1); % Cross correlation
lag = mod(find(c == max(c)), length(t1)) % Find the position of the peak
  1 个评论
Randy Souza
Randy Souza 2013-9-24
I have restored the original text of this question.
@Sainath: this question has a clear subject and an accepted answer, so it may be valuable to someone else in the future. If you have a good reason why it should be removed from MATLAB Answers, please flag the question, explain why it should be deleted, and an administrator or high-reputation contributor will consider deleting the question. Please do not simply edit your question away.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2013-7-17
编辑:Jan 2013-7-17
It is impossible for xcorr to find a delay between two signals, if they have a different frequency. This is not a problem of Matlab, but the delay between such signals is not even physically or mathematically defined.
So at first Use an interpolation or up- or downsampling method to get the signals with the same frequency. Then take into account that xcorr replies indices, which are positive integers. The function does not have any information about the frequency of the signals and can in consequence not reply a delay in seconds. But a trivial division allows you to convert the index output to the wanted time.
  1 个评论
David
David 2013-11-8
编辑:David 2013-11-8
The two signals do not have different frequencies. If you read carefully and look at his code, you'll see that the two signals, which both have a frequency of 1 Hz, are sampled at different frequencies. And so the delay can be measured by using, like you mention, the xcorr function. No down or up converting necessary.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by