Calculating delay b/w signals using correlation

In summary, Jedishrfu found that the default correlation window size is limiting the accuracy of xcorr when crosscorrelating two signals of the same frequency. He also found that adding noise to the signals makes xcorr unable to determine the true delay between the signals.
  • #1
nauman
91
4
Hi all
I am generating two signals of same frequency, i introduce a fixed delay in one of the signal and then try to find out the simulated delay using MATLAB 'gccphat' and 'finddelay' functions, but not able to measure correct delay. MATLAB Code script is given below:
Delay b/w two signals using correlation:
clc
clear
close all%%%%=======================================================================
%%%%============================General Parameters=========================
%%%%=======================================================================
fo=8500;%frequency in Hz
samp_f=30*51.2e3;%Sampling frequency
samp_t=1/samp_f;
chunk_size=163840;
total_chunks=1;
chunk_time=chunk_size/samp_f;
chunk_t=0:samp_t:(samp_t*(chunk_size-1));%time vector calculated

delay_=0.0064;

H1(1:chunk_size,1)=sin(2*pi*fo*(chunk_t))+0*randn(1,chunk_size);%Sensor H1 signal
H2(1:chunk_size,1)=sin(2*pi*fo*(chunk_t+delay_))+0*randn(1,chunk_size);%Sensor H2 signal
figure,plot(H1(1:2000)),hold on,plot(H2(1:2000),'r')

format long
tau_a=gccphat(H1,H2,samp_f)
tau_b = finddelay(H1, H2) / samp_f

Output:
tau_a = 0
tau_b = 1.171875000000000e-05

Can any one kindly point out where i am doing wrong with such simple task? As per my understanding, both outputs should match with the delay i.e. 0.0064s?
Thanks
 
Physics news on Phys.org
  • #2
You said you created two signals via sine function with one delayed behind the other.

It looks like you also added noise to the signals and plotted them. In your plot can you visually see the delay or does the noise obscure it?
 
  • #3
Hi Jedishrfu
There is no noise added in both signals as i have multiply randn with 0 in both cases. Both signals are pure sinusoidal and delay can be seen visually.
 
  • Like
Likes jedishrfu
  • #4
have you checked for example usage of the finddelay function?

https://www.mathworks.com/help/signal/ref/finddelay.html

Noticein their examples, they prepend zeros. You could try using h1 and make dh1 with prepended zeros to verify that the finddelay function works.

At the end of the article on finddelay, the author mentions that xcorr() is used and that sufficient correlation between the signals must be present so maybe that’s the issue.
 
  • #5
My first recommendation is to try your code with some dirt-simple numbers that are easy to check. You might get some surprises.
1) Your code actually looks like an advance by delay_.
2) Is there a default limit on the correlation window size? Your "delay" (actually advance) is 9,830.4 samples. I didn't check how many samples it is as a true delay from the next cycle.
3) How does your delay compare to your frequency? It looks to me as though your delay is many cycles at that frequency. Is that what you meant to do? That delay at that frequency might be equivalent to a much smaller delay.
 
Last edited:
  • Like
Likes nauman
  • #6
FactChecker said:
2) Is there a default limit on the correlation window size? Your "delay" (actually advance) is 9,830.4 samples. I didn't check how many samples it is as a true delay from the next cycle.
3) How does your delay compare to your frequency? It looks to me as though your delay is many cycles at that frequency. Is that what you meant to do? That delay at that frequency might be equivalent to a much smaller delay.
Hi

Thanks for help. With sampling frequency of approx. 1.53 MHz, one cycle of 8.5 KHz corresponds to 181 samples approx. It means 0.0064 second delay corresponds to approx (9830.4/181=53.4) 54 cycles delay at that particular frequency. This also means xcorr will never able to calculate true delay b/w two signals of same frequency if it is greater than one cycle delay. Am i right in my understanding?

Thanks
 
  • #7
jedishrfu said:
have you checked for example usage of the finddelay function?

https://www.mathworks.com/help/signal/ref/finddelay.html

Noticein their examples, they prepend zeros. You could try using h1 and make dh1 with prepended zeros to verify that the finddelay function works.

At the end of the article on finddelay, the author mentions that xcorr() is used and that sufficient correlation between the signals must be present so maybe that’s the issue.
Hi
I have also tried with appended zeros of same length as that of signal but results does not match with delay_.
Thanks
 
  • #8
nauman said:
Hi

Thanks for help. With sampling frequency of approx. 1.53 MHz, one cycle of 8.5 KHz corresponds to 181 samples approx.
Ok.
nauman said:
It means 0.0064 second delay corresponds to approx (9830.4/181=53.4) 54 cycles delay at that particular frequency.
Right, except that it is not a delay. Since the values of H2 are appropriate for a later time, chunk_t+delay_ , it is actually ahead of H1 in time, not delayed.
nauman said:
This also means xcorr will never able to calculate true delay b/w two signals of same frequency if it is greater than one cycle delay.
I don't think that is right. The algorithm doesn't care that the frequencies are the same. The crosscorrelation should be perfect at 54 cycles advanced, but there may be smaller time shifts that come close enough to fool it. You are asking for crosscorrelations to be calculated for 181*54=9774 both forward and behind. I don't know how many crosscorrelations it does by default, but I would not be surprised if it stops for some reason before that.

Why don't you try some much fewer frames of time-shift and see if your code behaves as expected? Try a simple test case.
 
Last edited:
  • #9
FactChecker said:
Why don't you try some much fewer frames of time-shift and see if your code behaves as expected? Try a simple test case.
I am simulating signals on two hydrophones H1 and H2 of linear array which are at a distance 'd' from each other. The geometry is shown below:

two_sensors.jpg

Assuming Plane Wave assumption, there will be some 'delay_' b/w 8.5KHz source signal arriving at H1 and H2 assuming zero noise and pure sinusoidal signals. MATLAB script given below:

two sensor array:
%%%%============================================================
%%%%====================Two Sensor Array Parameters========================
%%%%=======================================================================
D=15;%separation b/w sensors in m
c=1500;%in m/s
Target_theta=40;%Source angle in deg
delay_=D*sind(Target_theta)/c

The delay_ value calculated is around 0.0064s. The literature says we can estimate source angle by calculating the delay b/w two sensors (i.e. H1 & H2) using cross correlation techniques.
 
  • #10
nauman said:
I am simulating signals on two hydrophones H1 and H2 of linear array which are at a distance 'd' from each other. The geometry is shown below:

View attachment 322134
Assuming Plane Wave assumption, there will be some 'delay_' b/w 8.5KHz source signal arriving at H1 and H2 assuming zero noise and pure sinusoidal signals. MATLAB script given below:

two sensor array:
%%%%============================================================
%%%%====================Two Sensor Array Parameters========================
%%%%=======================================================================
D=15;%separation b/w sensors in m
c=1500;%in m/s
Target_theta=40;%Source angle in deg
delay_=D*sind(Target_theta)/c

The delay_ value calculated is around 0.0064s. The literature says we can estimate source angle by calculating the delay b/w two sensors (i.e. H1 & H2) using cross correlation techniques.
Find, but it is premature to apply your code to the actual problem when it is not working as expected.
At this time, I wouldn't worry about your ultimate application. See if it works on some simple cases.
You also might want to run the xcorr() program that finddelay uses and see what it gives you. That might make it obvious what is going on.
 
Last edited:
  • #11
nauman said:
Hi
I have also tried with appended zeros of same length as that of signal but results does not match with delay_.
Thanks
Notice that the simple test cases where zeros are prepended are somewhat "rigged". The leading zeros hurt any crosscorrelation for any delay that is smaller than the true delay. Small delays are trying to correlate a lot of zeros with the real signal. So those test cases are easier to get right than normal applications will be.
 

FAQ: Calculating delay b/w signals using correlation

What is the basic concept of using correlation to calculate the delay between two signals?

Correlation measures the similarity between two signals as a function of the time-lag applied to one of them. By calculating the cross-correlation between two signals, the point at which the correlation is maximized indicates the delay or time shift between the two signals. This method is widely used because it can identify both small and large delays accurately.

How do you perform cross-correlation between two signals?

Cross-correlation can be performed by taking one signal and sliding it over the other signal, computing the dot product at each position. Mathematically, this is often done using the Fourier Transform, where the cross-correlation can be computed efficiently in the frequency domain. In practice, many software tools and libraries, such as MATLAB, Python's NumPy, and SciPy, provide built-in functions to perform cross-correlation.

What are the limitations of using correlation to calculate delay between signals?

While correlation is a powerful tool, it has limitations. It assumes that the signals are stationary and that the delay is constant over the period of observation. Additionally, noise and other distortions can affect the accuracy of the delay estimation. In cases of very noisy signals, preprocessing steps such as filtering may be necessary to improve the correlation results.

Can correlation be used to calculate delays in real-time systems?

Yes, correlation can be used in real-time systems, but it requires efficient computation methods to handle the data quickly. Fast algorithms and hardware implementations, such as using Digital Signal Processors (DSPs) or Field-Programmable Gate Arrays (FPGAs), can be employed to achieve real-time performance. However, the computational complexity and resource requirements need to be carefully managed.

How does windowing affect the calculation of delay using correlation?

Windowing involves segmenting the signals into smaller parts or "windows" before performing the correlation. This can help in analyzing non-stationary signals where the delay might vary over time. However, the choice of window size and shape can significantly affect the results. Too small a window may not capture the delay accurately, while too large a window may include irrelevant parts of the signal, leading to poor correlation results.

Similar threads

Replies
14
Views
2K
Replies
1
Views
1K
Replies
11
Views
12K
Replies
1
Views
957
Replies
43
Views
5K
Replies
2
Views
1K
Back
Top