5
$\begingroup$

I have a 2D array of eeg data with shape (64,512) - 64 electrodes, 512 timepoints

I want to calculate the maximum cross correlation (irrespective of lag/time shift) between every single electrode, so I end up with a 64x64 array containing max cross correlation values between all pairs

Is there an efficient way of doing this in python/numpy/scipy without iterating through all pairs of electrodes?

I tried using scipy.signal.correlate2d but I'm not sure its doing what I think its doing as I end up with a 2D array of size 127x1023 rather than 64x64:

from scipy import signal
import numpy as np

data = np.random.randint(1,100,(64,512))

xcorr = signal.correlate2d(data,data)
$\endgroup$
1
  • $\begingroup$ For those looking for 'actual' 2D, see here $\endgroup$ Commented Feb 20, 2023 at 12:09

3 Answers 3

6
$\begingroup$

What you have (conceptually) is not a 2D array but a collection of 1D arrays. correlate2D is designed to perform a 2D correlation calculation, so that's not what you need. Iterating through all pairs is not a big ask really - you can still use numpy to perform the cross correlation, you'll just need to have two loops (nested) to determine which signals to perform the calculation on. With only 64 signals that shouldn't take long.

for first in range(n_signals): for second in range(first + 1, n_signals): corr = numpy.correlate(data[first], data[second], mode='full') max_corr[first, second] = numpy.max(corr)

$\endgroup$
0
$\begingroup$
df.corr() # Compute pairwise correlation of columns, excluding NA/null values.

you can see that in pandas documentation it means you do not need the loop plus you can choose the method of cor

$\endgroup$
1
  • 1
    $\begingroup$ Welcome to SP.SE! Providing code that, in addition to the OP's code, will run immediately is more useful than just a single line. Also, please format code using the code format button (as I've edited it to do). $\endgroup$
    – Peter K.
    Commented Dec 30, 2021 at 10:25
0
$\begingroup$

You can focus on the scipy.signal.correlate() function instead of scipy.signal.correlate2d() scipy.signal.correlate()

$\endgroup$
1
  • 1
    $\begingroup$ Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center. $\endgroup$
    – ZaellixA
    Commented Feb 20, 2023 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.