obspy.signal.cross_correlation.correlate

correlate(a, b, shift, demean=True, normalize=True, domain=u'freq')[source]

Cross-correlation of signals a and b with specified maximal shift.

Parameters:
  • a (ndarray, Trace) first signal
  • b (ndarray, Trace) second signal to correlate with first signal
  • shift (int) Number of samples to shift for cross correlation. The cross-correlation will consist of 2*shift+1 or 2*shift samples. The sample with zero shift will be in the middle.
  • demean (bool) Demean data beforehand.
  • normalize (bool) Normalize cross-correlation. A perfect correlation will correspond to the value 1.
  • domain (str) Correlation will be performed in frequency domain with scipy.signal.fftconvolve() for domain='freq' and in time domain with scipy.signal.correlate() for domain='time'.
Returns:

cross-correlation function.

To calculate shift and value of the maximum of the returned cross-correlation function use xcorr_max().

Note

For most input parameters cross-correlation in frequency domain is much faster. Only for small values of shift (⪅100) time domain cross-correlation migth save some time.

Note

If the signals have different length, they will be aligned around their middle. The sample with zero shift in the cross-correlation function corresponds to this correlation:

--aaaa--
bbbbbbbb

For odd len(a)-len(b) the cross-correlation function will consist of only 2*shift samples because a shift of 0 corresponds to the middle between two samples.

Example

>>> a = np.random.randn(10000).astype(np.float32)
>>> cc = correlate(a, a, 1000)
>>> shift, value = xcorr_max(cc)
>>> shift
0
>>> round(value, 5)
1.0
>>> b = np.roll(a, 20)  # shift a by 20 samples
>>> cc = correlate(a, b, 1000)
>>> shift, value = xcorr_max(cc)
>>> shift
-20
>>> round(value, 2)
1.0