How do I find the time delay between two signals?

31 views (last 30 days)
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 Comment
Randy Souza
Randy Souza on 24 Sep 2013
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.

Sign in to comment.

Accepted Answer

Jan
Jan on 17 Jul 2013
Edited: Jan on 17 Jul 2013
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 Comment
David
David on 8 Nov 2013
Edited: David on 8 Nov 2013
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!