MATLAB Answers

cross correlation between complicated signals and measure the delay between them

13 views (last 30 days)
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 ..


Sign in to comment.

Accepted Answer

Daniel M
Daniel M on 2 Dec 2019
You can use the following code to get the time delay between two signals:
close all
[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
xlabel('Lags [samples]')
[~,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
xlabel('Time [s]')
legend('x1','x2','Shifted x2')
fprintf('\nSignal 2 lags behind Signal 1 by ~%.1f microseconds\n',1e6*abs(delayTime))


Sign in to comment.

More Answers (1)

bzibubab bzibubab
bzibubab bzibubab on 17 Dec 2019
sorry for my late reply, because i needed to check the code you sent me first after i got home from work. and also a big thank you for your answer, i really appreciated it.
Actually i have used a correlation program from matlab before and i got no idea how did the result come from. i Really like your idea to shift the other sample to make a better understanding of how the correlation work and it really enlighten me.
So the point which i want to talk is i just discovered that my impulse has a slightly bit different frequency with another impulse which i want to compare, so it makes the correlation result somewhat different with the result i have been done manually with osciloscope. I checked all of my impulses with fft and their peak frequencies are silghtly different with each other. I really want to know, does a slightly bit different frequency of two of the signals that i want to 'correlate' affected so much to the correlation process ? what should i do to get a better result? or maybe is it possible by changing the frequency of the second signal to be the same as the first one?
im very grateful for anyone who respond my question


Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by