# Cross correlate a 2D array

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)

• For those looking for 'actual' 2D, see here Feb 20 at 12:09

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) 

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

• 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, 2021 at 10:25

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