obspy.signal.cross_correlation.correlate

correlate(a, b, shift, demean=True, normalize='naive', method='auto')[source]

Cross-correlation of two signals up to a specified maximal shift.

This function only allows ‘naive’ normalization with the overall standard deviations. This is a reasonable approximation for signals of similar length and a relatively small shift parameter (e.g. noise cross-correlation). If you are interested in the full cross-correlation function better use correlate_template() which also provides correct normalization.

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 – Method for normalization of cross-correlation. One of 'naive' or None (True and False are supported for backwards compatibility). 'naive' normalizes by the overall standard deviation. None does not normalize.

  • method (str) – Method to use to calculate the correlation. 'direct': The correlation is determined directly from sums, the definition of correlation. 'fft' The Fast Fourier Transform is used to perform the correlation more quickly. 'auto' Automatically chooses direct or Fourier method based on an estimate of which is faster. (Only availlable for SciPy versions >= 0.19. For older Scipy version method defaults to 'fft'.)

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 using the FFT is much faster. Only for small values of shift (approximately less than 100) direct 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

>>> from obspy import read
>>> a = read()[0][450:550]
>>> b = a[:-2]
>>> cc = correlate(a, b, 2)
>>> cc
array([ 0.62390515,  0.99630851,  0.62187106, -0.05864797, -0.41496995])
>>> shift, value = xcorr_max(cc)
>>> shift
-1
>>> round(value, 3)
0.996