I want to do this with larger arrays, but so that I can understand it, i'll use a smaller example.
Let's say I have the following array:
A = [[0, 0, 100],
[0, 0, 0],
[0, 0, 0]]
If I want to calculate the correlation of this array with another, by multiplying the corresponding entries. For example, A*A would be equal to A (1*1 = 1, zeros everywhere else). I read that fast fourier transforms can be used to speed this up with large arrays. From what I read, I got the impression if I wanted to multiply two arrays A and B, as in A*B, that I could do it quicker with the following (using numpy in python):
a = np.conj(np.fft.fftn(A))
b = np.fft.fftn(B)
c = np.fft.ifft(a*b)
So in effect, take the fft of A, take the fft of B, multiply the two results, and then get the inverse of that result. However, I tried it with the case of A given above, multiplying itself. I had hoped the inverse multiplication would give me
[[0, 0, 10000],
[0, 0, 0 ],
[0, 0, 0 ]]
However I got something a bit different, closer to
[[10000, 0, 0],
[10000, 0, 0],
[10000, 0, 0]]
does anyone know what's going on? Sorry, I'm guessing there's something I'm misunderstanding about the fft.
You should use
scipy.signal.fftconvolveinstead.It is already implemented and has been extensively tested, particularly regarding the handling the boundaries. The only additional step necessary to go from the convolution to the correlation operator in 2D is to rotate the filter array by 180° (see this answer).