pwtools.signal.acorr

pwtools.signal.acorr(v, method=7, norm=True)[source]

(Normalized) autocorrelation function (ACF) for 1d arrays.

Without normalization

c(t) = <v(0) v(t)>

and with

c(t) = <v(0) v(t)> / <v(0)**2>

The x-axis is the offset “t” (or “lag” in Digital Signal Processing lit.). Since the ACF is symmetric around t=0, we return only t=0…len(v)-1 .

Several Python and Fortran implementations. The Python versions are mostly for reference and are slow, except for fft-based, which is by far the fastest.

Parameters:
  • v (1d array)

  • method (int) –

    1: Python loops
    2: Python loops, zero-padded
    3: method 1, numpy vectorized
    4: uses numpy.correlate()
    5: Fortran version of 1
    6: Fortran version of 3
    7: fft, Wiener-Khinchin Theorem

  • norm (bool) – normalize or not

Returns:

c – | c[0] <=> lag = 0 | c[-1] <=> lag = len(v)

Return type:

numpy 1d array

Notes

Generalization of this function to correlation corr(v,w) should be straightforward. Autocorrelation is then corr(v,v).

speed:

methods 1 … are loosely ordered slow … fast

methods:

All methods, besides the FFT, are “exact”, they use variations of loops in the time domain, i.e. norm(acorr(v,1) - acorr(v,6)) = 0.0. The FFT method introduces small numerical noise, norm(acorr(v,1) - acorr(v,4)) = O(1e-16) or so.

signature of the Fortran extension _flib.acorr:

acorr - Function signature:
  c = acorr(v,c,method,[nstep])
Required arguments:
  v : input rank-1 array('d') with bounds (nstep)
  c : input rank-1 array('d') with bounds (nstep)
  method : input int
Optional arguments:
  nstep := len(v) input int
Return objects:
  c : rank-1 array('d') with bounds (nstep)

References