5

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)
Simon
  • 305
  • 2
  • 6
  • 22

3 Answers3

6

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)

lxop
  • 1,369
  • 7
  • 15
0
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

Peter K.
  • 25,714
  • 9
  • 46
  • 91
oriel
  • 1
  • 1
    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). – Peter K. Dec 30 '21 at 10:25
0

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

  • 1
    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. – ZaellixA Feb 20 '23 at 09:21