MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / autocorrelate1D

Function autocorrelate1D

numpy_ml/preprocessing/dsp.py:416–450  ·  view source on GitHub ↗

Autocorrelate a 1D signal `x` with itself. Notes ----- The `k` th term in the 1 dimensional autocorrelation is .. math:: a_k = \sum_n x_{n + k} x_n NB. This is a naive :math:`O(N^2)` implementation. For a faster :math:`O(N \log N)` approach using the FFT, se

(x)

Source from the content-addressed store, hash-verified

414
415
416def autocorrelate1D(x):
417 """
418 Autocorrelate a 1D signal `x` with itself.
419
420 Notes
421 -----
422 The `k` th term in the 1 dimensional autocorrelation is
423
424 .. math::
425
426 a_k = \sum_n x_{n + k} x_n
427
428 NB. This is a naive :math:`O(N^2)` implementation. For a faster :math:`O(N
429 \log N)` approach using the FFT, see [1].
430
431 References
432 ----------
433 .. [1] https://en.wikipedia.org/wiki/Autocorrelation#Efficient%computation
434
435 Parameters
436 ----------
437 x : :py:class:`ndarray <numpy.ndarray>` of shape `(N,)`
438 A 1D signal consisting of N samples
439
440 Returns
441 -------
442 auto : :py:class:`ndarray <numpy.ndarray>` of shape `(N,)`
443 The autocorrelation of `x` with itself
444 """
445 N = len(x)
446 auto = np.zeros(N)
447 for k in range(N):
448 for n in range(N - k):
449 auto[k] += x[n + k] * x[n]
450 return auto
451
452
453#######################################################################

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected