Finding the how much two curve got shifted ?
9 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
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)
% 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.
corr(x',y') % This is what I should do since x,y are row vectors.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!