Created
February 26, 2020 10:22
-
-
Save endrebak/3750acc7a7d0d137de8aeaef0eb7cee4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@numba.jit | |
def ucorrelate(t, u, maxlag=None): | |
"""Compute correlation of two signals defined at uniformly-spaced points. | |
The correlation is defined only for positive lags (including zero). | |
The input arrays represent signals defined at uniformily-spaced | |
points. This function is equivalent to :func:`numpy.correlate`, but can | |
efficiently compute correlations on a limited number of lags. | |
Note that binning point-processes with uniform bins, provides | |
signals that can be passed as argument to this function. | |
Arguments: | |
tx (array): first signal to be correlated | |
ux (array): second signal to be correlated | |
maxlag (int): number of lags where correlation is computed. | |
If None, computes all the lags where signals overlap | |
`min(tx.size, tu.size) - 1`. | |
Returns: | |
Array contained the correlation at different lags. | |
The size of this array is equal to the input argument `maxlag` | |
(if defined) or to `min(tx.size, tu.size) - 1`, and its `dtype` | |
is the same as argument `t`'s. | |
Example: | |
Correlation of two signals `t` and `u`:: | |
>>> t = np.array([1, 2, 0, 0]) | |
>>> u = np.array([0, 1, 1]) | |
>>> pycorrelate.ucorrelate(t, u) | |
array([2, 3, 0]) | |
The same result can be obtained with numpy swapping `t` and `u` and | |
restricting the results only to positive lags:: | |
>>> np.correlate(u, t, mode='full')[t.size - 1:] | |
array([2, 3, 0]) | |
Also works with other types: | |
>>> t = np.array([1.2, 2.4, 0.5, 0.6]) | |
>>> u = np.array([0, 1.2, 1.3]) | |
>>> pycorrelate.ucorrelate(t, u) | |
array([3.53, 4.56, 1.56]) | |
""" | |
if maxlag is None: | |
maxlag = u.size | |
maxlag = int(min(u.size, maxlag)) | |
C = np.empty(maxlag, dtype=t.dtype) | |
for lag in range(C.size): | |
tmax = min(u.size - lag, t.size) | |
umax = min(u.size, t.size + lag) | |
C[lag] = (t[:tmax] * u[lag:umax]).sum() | |
return C |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment