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)
| 414 | |
| 415 | |
| 416 | def 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 | ####################################################################### |
nothing calls this directly
no outgoing calls
no test coverage detected