- correlate(a, b, shift, demean=True, normalize='naive', method='auto')
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.
shift (int) – Number of samples to shift for cross correlation. The cross-correlation will consist of
2*shiftsamples. The sample with zero shift will be in the middle.
demean (bool) – Demean data beforehand.
normalize – Method for normalization of cross-correlation. One of
Falseare supported for backwards compatibility).
'naive'normalizes by the overall standard deviation.
Nonedoes 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
To calculate shift and value of the maximum of the returned cross-correlation function use
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.
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:
len(a)-len(b)the cross-correlation function will consist of only
2*shiftsamples because a shift of 0 corresponds to the middle between two samples.
>>> from obspy import read >>> a = read()[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