Cross Correlation between two ramp signals
7 views (last 30 days)
Show older comments
Hello all,
Perhaps I have misunderstood how cross correlation works, nonetheless here is my problem:
I have two ramp signals, A and B, each have a slope of 1, A begins at time 0, and B begins 5 seconds later. I am using xcorr to correlate the two signals in order to find the lag. However, xcorr returns a graph which would indicate a lag of 0 time steps.
Does anyone know why this is?
0 Comments
Answers (1)
Harsh Kumar
on 9 Jul 2023
Hi James ,
I understand that you are trying to calculate the Lag between two signals using 'xcorr' function.
You are getting '0' in result as you might be computing the lag at the maximam correlation point which is true indeed because the two signal are aligned perfectly at that point as can be seen from the attached graph as well.
Refer the code snippet and graph of 'correlation' vs 'Lag' to check that shows that lag is 0 when cross-correlation is maximam.
Also refer to the given documentaion for details :Documentation
% Define the time vector
t = 0:0.1:10;
% Define the ramp signals
A = t;
B = zeros(size(t));
B(t >= 5) = t(t >= 5) - 5;
% Perform cross-correlation
[R, lag] = xcorr(A, B);
% Plot the cross-correlation result
figure
stem(lag, R)
title('Cross-correlation between A and B')
xlabel('Lag')
ylabel('Correlation')
% Find the lag corresponding to the maximum correlation
[maxCorr, maxIdx] = max(R);
lagAtMaxCorr = lag(maxIdx);
disp(['The lag with maximum correlation is ', num2str(lagAtMaxCorr), ' time units.']);
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!