Finding the how much two curve got shifted ?

11 views (last 30 days)
I have two different image from those I have drawn two different intensity profile along the column profile.
I have attached those both curves. Can anyone suggest me how much the test imaage intensity curve shifted from the reference image intensity through matlab code or matlab built in function? I have tried finddelay function but it gives me answer 0 which is obviously not true in this case.

Accepted Answer

William Rose
William Rose on 12 Sep 2023
Edited: William Rose on 13 Sep 2023
[edit: I tried xcorr() and did notlike the results. xcorr() does not have a normalization option that I like. Therefore I wrote a for loop, to compute the Pearson correlation between the two waveforms, with different lags.]
I see that you attached an image. Please attach the actual data.
It does not look like a simple sideways shift. It looks more complicated.
Find the lag where the cross correlation is maximal. It seems obvious to use xcorr(), but none of the xcorr() normalization options gives good results in this case.
t=1:34;
x=45*sin(2*pi*t/20)./(2*pi*t/20)+160; % ref., green
y=40*sin(2*pi*t/30)./(2*pi*t/30)+170; % test blue
plot(t,x,'-g',t,y,'-b'); legend('x=Test','y=Ref.'); grid on
Curves above are similar to your curves.
Limit the lags (m) to +-20, because the signal is only 34 points long.
N=length(x);
maxLag=20;
for m=-maxLag:maxLag % m>0: shift y to the right
xSeg=x(max(1+m,1):min(N+m,N));
ySeg=y(max(1-m,1):min(N-m,N));
rho=corrcoef(xSeg,ySeg); % rho = 2x2 matrix
xyCorr(m+maxLag+1)=rho(1,2); % save the off diagonal element
end
plot(-maxLag:maxLag,xyCorr,'-r.'); grid on
Find the lag corresponding to max correlation.
[maxCorr,idx]=max(xyCorr);
bestLag=idx-maxLag-1;
Show that best lag point on the plot:
hold on; plot(bestLag,maxCorr,'bd','MarkerFaceColor','b')
Plot x and y, with y shifted by bestLag:
xPlot=x(max(1+bestLag,1):min(N+bestLag,N));
yPlot=y(max(1-bestLag,1):min(N-bestLag,N));
nP=length(xPlot);
figure;
plot(1:nP,xPlot,'-g.',1:nP,yPlot,'-b.')
legend('x=Test','y(shifted)=Ref(shifted)')
  6 Comments
Amit
Amit on 25 Sep 2023
@William Rose Thanks again for your kind help.
Can I ask one more question?
rho = 2x2 matrix; but why you save the off only the diagonal element?
Thank you in Advance !
William Rose
William Rose on 26 Sep 2023
I should have just used corr(x',y'). I tried corr(x,y) and it gave a matrix of NaNs because I had fogotten that you must use column vectors for corr(). So I used corrcoef(x,y), which returns a 2x2 matrix, and I took the off diagonal element. That wasn't very smart of me. Either off-diagonal is fine, since they are equal.
x=rand(1,10); y=rand(1,10);
corrcoef(x,y)
ans = 2×2
1.0000 0.2021 0.2021 1.0000
% For the result above, rho(1,1)=corr(x,x)=1;
% and rho(1,2)=corr(x,y);
% and rho(2,1)=corr(y,x)=rho(1,2);
% and rho(2,2)=corr(y,y)=1
corr(x,y) % This would work if x,y were column vectors, but they're not.
ans = 10×10
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
corr(x',y') % This is what I should do since x,y are row vectors.
ans = 0.2021

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!