필터 지우기
필터 지우기

Finding the how much two curve got shifted ?

조회 수: 7 (최근 30일)
Amit
Amit 2023년 9월 12일
댓글: William Rose 2023년 9월 26일
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.

채택된 답변

William Rose
William Rose 2023년 9월 12일
편집: William Rose 2023년 9월 13일
[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
Amit
Amit 2023년 9월 25일
@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 2023년 9월 26일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by