find shift using cross correlation

Dear all;
i have a confusion when i am using xcorr function in matlab , suppose i have attached two signals , is this function give the amount of shift in between signals ? is there any conversion factor that shall i use ?

回答(1 个)

I suggest you start with a simple example, like
signal1 = zeros(1, 10);
signal1(5) = 1;
signal2 = zeros(1, 10);
signal2(8) = 1;
Then look at the length of the output and where the max/peak/spike is located. You'll figure it out.
By the way, I don't think it's well known that the max of the correlation is not always when the two signals are optimally overlapped - it just tells you when the area under the curve of the multiplication of the two signals is greatest. I believe this is why normalized cross correlation was invented. This is done by the function normxcorr2 in the Image Processing Toolbox. (Demo available upon request).

6 个评论

Dear Image analyst;
Thank you very much for help . in fact , i understand th concept but i was worried if i am understand in built function well or not.
the above two lines are boundary between two areas in am image ( after extraction). these image are taken from a video and i am trying to estimate how much the two objects are moving with time (in other words , estimate distance shift (in pixels)) then later on convert this to an actual distance in meter. the signals are normalized as shown below: s1= EE1(:,1); s2= EE2(:,1);
s1_sum=0;
s2_sum=0;
for i=1:L1(1,1)
m1= s1(i)*s1(i);
s1_sum= s1_sum+m1;
m2= s2(i)*s2(i);
s2_sum= s2_sum+m2;
end
%// Normalize signals
norm= sqrt(s1_sum*s2_sum);
s1 = (s1 - mean(s1)) /norm;
s2 = (s2 - mean(s2)) /norm;
But the issue that I am not getting satisfactory results when i compare it with actual ones
Attach s1 and s2 in a mat file so we can run your code.
Dear Image analyst; Thank you very much for your help.
Attached are mat files for original signals . the code shown below
s1= EE1(:,1);
s2= EE2(:,1);
L1=size(s1);
s1_sum=0;
s2_sum=0;
for i=1:L1(1,1)
m1= s1(i)*s1(i);
s1_sum= s1_sum+m1;
m2= s2(i)*s2(i);
s2_sum= s2_sum+m2;
end
%// Normalize signals
norm= sqrt(s1_sum*s2_sum);
s1 = (s1 - mean(s1)) /norm;
s2 = (s2 - mean(s2)) /norm;
%// Compute time lag between signals [c,lag] = xcorr(s2(:,1), s1(:,1), 'none' ); %// Cross correlation
% % lag = mod(find(c == max(c)), length(s2)) %// Find the position of the peak % [x,I] = max((c)); Distshift = lag(I)
figure (3)
subplot(311); plot(s1(:,1)); title('s1');
subplot(312); plot(s2(:,1)); title('s2');
subplot(313); plot(lag,c);
title('Cross-correlation between s1 and s2')
FOV=0.10; % Field of View (Meter) FW=1632; % Frame Width (Pixel) Actual_Distance= Distshift*FOV/FW; % Calcuate the actual distance in meter
any feedback about the code?
Hi there! I tried to solve your problem but I wasn´t able. Any solution so far?
Were you able to fix it?. I'm looking forward to hearing from you. Thanks.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by