# 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)


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)