Reading this and having tried np.correlate and cv2.matchTemplate I still have a question that I don't seem to be able to solve.
I've got two numpy arrays each with the shape (6000,50)
. 6000 sequences with each 50 values. Now I would like to do a cross-correlation of two 1-dimensional sequences of this array to detect the timeshift. I tried openCV briefly, but for me this returns a single number (I expect the highest correlation), so now I use numpy.correlate like this:
np.correlate(x[2500], y[2500], mode='same')
(In the cross-correlation plot I'm not looking for the highest peak, but I'm looking for the first peak using this. See plot for an example)
As you might expect, I would like to do this for all 6000 sequences, but hoping to avoid iteration. I was hoping this would work:
np.correlate(x, y, mode='same')
But this gives me the following error: ValueError: object too deep for desired array
.
Is there any change that this is possible with NumPy or OpenCV. Or will i have to do it like this :(
for i in range(x.shape[0]):
np.correlate(x[i], y[i], mode='same')
scipy.ndimage.correlate1d
seemed like what you are after, but it only broadcasts on the first array, the second has to be strictly 1D, so no luck there. And the functions inscipy.signal
do multi-dimensional correlation, not 1D like you are after. So there doesn't seem to be anything in the stack that solves your problem.Just for the fun of it, you could always do this with FFTs and the cross-correlation theorem:
With your use case this isn't a great idea:
But the approach does have its merits, mostly for larger sequences: