필터 지우기
필터 지우기

How could I find the lag between two signals by using xcorr?

조회 수: 1 (최근 30일)
Angel Romero
Angel Romero 2017년 6월 23일
답변: Image Analyst 2017년 6월 24일
I want to find the lag between two different signals by using xcorr. I have this code and it's working:
clear
clc
f=1e3;
x= [-8:1/f:20];%Número de muestras que tomara xcorr (toma el mayor número de muestras)
r=2;%numero de ondulaciones
%Traslación
tshift=x+0;
for i=1:length(x)
y=exp((-tshift(i)^2)/2);
w=cos(pi.* tshift (i).*(r/log(2))^(1/2));
n=w/pi^(1/4) ;
fshift(i)=(y.*n);
end
y1 = sinc(x-1);%segunda señal
figure (4)
plot(x,fshift,'r',x,y1,'b')
%d1 = -finddelay(y1,fshift)/f %Encuentra el retraso en los puntos más altos de las señales
[c,lags]=xcorr(y1,fshift);
d2 = -(lags(c == max(c)))/f
%d2 =-1 whom result is right
end
Unfortunately, I don't know what is happening with the next one. I'd appreciate any help I can get. Thanks again
% clear
clc
fm=100e6;
Delta=5e-5;%rango de la wavelet
x= -Delta:1/fm:Delta;
r=2;%numero de ondulaciones
s=50;%s=escala (recordar que debe de ser multiplo de 10 delta)
es=x*s/Delta-22.47; %-22.47=2.47
for i=1:length(x)
y=exp((-es(i)^2)/2);
w=cos(pi.* es(i).*(r/log(2))^(1/2));
n=w/pi^(1/4);
WavMorlet(i)=(y.*n);
end
figure (1)
plot(x,WavMorlet,'b')
fs=100e6;%freciencia de muestreo
%fs=1e2;%freciencia de muestreo
deltat=1/fs;%periodo de mujestreo
nmuestras=9000;
%nmuestras=1000;
tPt=(-deltat*1000):deltat:deltat*nmuestras; %cambia de 9000 a 9000
%tPt=(-deltat*1000):deltat:deltat*nmuestras; %cambia de 9000 a 9000
f0=1e6; % Frecuencia del transductor
B= 300e3;% ancho de banda
T=[25,50];%temperatura
espesor=34e-3;% distancia fija
% velocidad de propagacion del agua calculada segun Bilianuk
for p=1:length(T)
z(p)=(1.40238742e3)+((5.03821344)*T(p))-((5.80539349e-2)*T(p)^2)+((3.32000870e-4)*T(p)^3)-((1.44537900e-6)*T(p)^4)+((2.99402365e-9)*T(p)^5);%velocidad de referencia
for k=1:length(tPt)
temp=(espesor/z(p));
t2(k)=tPt(k)-temp;
Pt(k,p)=-(exp(-4*(t2(k)^2)*(B^2)))*sin(2*pi*f0*t2(k));%señal eco en el retardo t2
end
end
figure(2)
plot(tPt,Pt(:,1),'r','linewidth',2)
xlim([0 5e-5])
hold on%dibujo básico
title('Temperature 25°C','fontsize',18)
xlabel('Time [s]','fontsize',14)
ylabel('Amplitud [V]','fontsize',14)
figure(3)
plot(x,WavMorlet,'b',tPt,Pt(:,1),'-r')
%d1 = finddelay(WavMorlet,Pt)/7e9;
%[c,lags]=xcorr(WavMorlet,Pt); %do the cross-correlation
%[max_c,I]=max(x); %find the best correlation
%delay = lags(I);
T25=Pt(:,1)';
[c,lags]=xcorr(T25,WavMorlet);
d2 = -(lags(c == max(c)))
%d2= 4002 whom result is utterly wrong. The acurrate answer should be practically 0.
end
I must insist, thank you for any advice.

답변 (1개)

Image Analyst
Image Analyst 2017년 6월 24일
The signal is not the same signal just shifted in time. It has different frequencies. Correlation is the sum of the products as one signal shifts past the other, which does not mean lag if the signals are not the same. In fact the help even tells you so:
"Cross-correlation measures the similarity between x and shifted (lagged) copies of y as a function of the lag. "
Your two signals are not shifted versions of each other, therefore you are not measuring lag.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by