1

Let's say I have a 100x100x100x100 numpy array. I'm trying to apply np.correlate of this array with itself but to only one of its axes, say axes 1. In an ideal world, this would be something like

print(a.shape) # (100,100,100,100)
correlated = np.correlate(a, a, "full", axis=1)
print(correlated.shape) # (100,199,100,100)

So basically the same behavior of np.fft, but with correlate. I can iterate over all the other dimensions and then calculate the correlation at each step, but these arrays can get pretty big and this would become very slow.

Is there a Numpy way of doing this that is faster? I tried using apply_over_axes but apparently it's not possible. And also, apply_over_axes, vectorize, etc are just python loops under the hood, so I guess that wouldn't much of an advantage anyway.

TomCho
  • 3,204
  • 6
  • 32
  • 83
  • Welcome to the [club](http://stackoverflow.com/questions/41887553/compact-way-of-simulating-axes-parameter-in-scipy-signal-fftconvolve)! – Paul Panzer Jan 31 '17 at 04:57
  • 1
    @PaulPanzer I was hoping this had a straightforward answer, but I'm disappointed to see that this is apparently not the case. Oh well... – TomCho Jan 31 '17 at 05:19
  • If you really need it urgently you could do what zaq is suggesting (his option (2)) – Paul Panzer Jan 31 '17 at 05:35
  • @PaulPanzer I don't know enough C to reprogram the correlate function actually. I guess I'll just have to do it on python for now. Or maybe even program it in fortran. – TomCho Jan 31 '17 at 07:50
  • You wouldn't have to touch any C, fftconvolve is written in python. It's calling fftn for the heavy lifting and fftn actually does let you pick axes if you feel so inclined. So you'd have to modify this code to accept an axes parameter and pass that through to fftn, plus some mildly annnoying but doable book keeping. That said, I so far couldn't be bothered to do it myself... – Paul Panzer Jan 31 '17 at 08:06
  • Possible duplicate of [Autocorrelation of a multidimensional array in numpy](https://stackoverflow.com/questions/4503325/autocorrelation-of-a-multidimensional-array-in-numpy) – Peter Oct 27 '17 at 23:57

0 Answers0