フィルターのクリア

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 Center および File ExchangeCorrelation and Convolution についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by