cross correlation between complicated signals and measure the delay between them
조회 수: 12 (최근 30일)
이전 댓글 표시
excuse me, im very new in matlab and have just been strugling for half a year, and my problem is i cant find the delay i need from 2 different signals with correlation method from matlab. These signals are from 2 different transducers, so it would be a little bit difference in shape. so my idea is to give it a hilbert envelope then correlate them with 'corcoeff; but it still didnt give the result i hope for which in case ive been done it manually wtih osciloscope. i attached some files here (in zip) that contains with some of my impulses from signal 1 and signal 2 with .wav format, and the result it should give is about 10.19 micro second for delay between impulse1a and impulse2a and 10.19 us for delay between impulse1b and impulse2b. i really appreacite for helping me to solve this mystery that haunt me for a long time, or maybe theres another method for finding the delay between them in my only case?. Thank you in advance ..
댓글 수: 0
채택된 답변
Daniel M
2019년 12월 2일
You can use the following code to get the time delay between two signals:
clearvars
close all
clc
[y1,fs1] = audioread('impulse1a.wav');
[y2,fs2] = audioread('impulse1b.wav');
[y3,fs3] = audioread('impulse2a.wav');
[y4,fs4] = audioread('impulse2b.wav');
% Get delays for all combinations
delay12 = getDelay(y1,y2,fs1);
delay13 = getDelay(y1,y3,fs1);
delay14 = getDelay(y1,y4,fs1);
delay23 = getDelay(y2,y3,fs1);
delay24 = getDelay(y2,y4,fs1);
delay34 = getDelay(y3,y4,fs1);
%%% Subfunction
function [delayTime,delaySamples] = getDelay(x1,x2,fs)
% Functions must have same sampling frequency
t = (0:length(x1)-1)*fs; % time vector
% make sure they are the same length
len = min(length(x1),length(x2));
x1 = x1(1:len);
x2 = x2(1:len);
[r,lags] = xcorr(x1,x2); % get the cross-correlation and view the lags
figure
stem(lags,r)
xlabel('Lags [samples]')
ylabel('Cross-correlation')
[~,maxlags] = max(abs(r)); % position of maximum lag
delaySamples = lags(maxlags); % number of samples difference
delayTime = delaySamples/fs; % convert to time
y3 = circshift(x2,lags(maxlags)); % shift the sample
% View the raw data and the shifted sample
% Notice how x3 lies on top of x1 nicely
figure
plot(t,x1,t,x2,t,y3)
xlabel('Time [s]')
legend('x1','x2','Shifted x2')
fprintf('\nSignal 2 lags behind Signal 1 by ~%.1f microseconds\n',1e6*abs(delayTime))
end
댓글 수: 0
추가 답변 (1개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!