Find time shift of two signals using cross correlation

Rickson picture Rickson · Jan 5, 2017 · Viewed 13.7k times · Source

I have two signals which are related to each other and have been captured by two different measurement devices simultaneously. Since the two measurements are not time synchronized there is a small time delay between them which I want to calculate. Additionally, I need to know which signal is the leading one.

The following can be assumed:

  • no or only very less noise present
  • speed of the algorithm is not an issue, only accuracy and robustness
  • signals are captured with an high sampling rate (>10 kHz) for several seconds
  • expected time delay is < 0.5s

I though of using-cross correlation for that purpose. Any suggestions how to implement that in Python are very appreciated.

Please let me know if I should provide more information in order to find the most suitable algorithmn.

Answer

Reveille picture Reveille · Jun 3, 2019

A popular approach: timeshift is the lag corresponding to the maximum cross-correlation coefficient. Here is how it works with an example:

import matplotlib.pyplot as plt
from scipy import signal
import numpy as np


def lag_finder(y1, y2, sr):
    n = len(y1)

    corr = signal.correlate(y2, y1, mode='same') / np.sqrt(signal.correlate(y1, y1, mode='same')[int(n/2)] * signal.correlate(y2, y2, mode='same')[int(n/2)])

    delay_arr = np.linspace(-0.5*n/sr, 0.5*n/sr, n)
    delay = delay_arr[np.argmax(corr)]
    print('y2 is ' + str(delay) + ' behind y1')

    plt.figure()
    plt.plot(delay_arr, corr)
    plt.title('Lag: ' + str(np.round(delay, 3)) + ' s')
    plt.xlabel('Lag')
    plt.ylabel('Correlation coeff')
    plt.show()

# Sine sample with some noise and copy to y1 and y2 with a 1-second lag
sr = 1024
y = np.linspace(0, 2*np.pi, sr)
y = np.tile(np.sin(y), 5)
y += np.random.normal(0, 5, y.shape)
y1 = y[sr:4*sr]
y2 = y[:3*sr]

lag_finder(y1, y2, sr)

enter image description here

In the case of noisy signals, it is common to apply band-pass filters first. In the case of harmonic noise, they can be removed by identifying and removing frequency spikes present in the frequency spectrum.