from scipy import signal
from scipy import linalg
import numpy as np
from openpiv.pyprocess import fft_correlate
x = [[1 , 0 , 0 , 0] , [0 , -1 , 0 , 0] , [0 , 0 , 3 , 0] , [0 , 0 , 0 , 1],[0,0,2,0]]
x = np.array(x)
y = [[4 , 5] , [3 , 4]]
y = np.array(y)
# x = np.random.rand(2,3)
# y = np.random.rand(4,6)
print("x:\n", x)
print("y:\n", y)
print ("conv:\n" , signal.convolve2d(x , y , 'full'))
s1 = np.array(x.shape)
s2 = np.array(y.shape)
size = s1 + s2 - 1
fsize = 2 ** np.ceil(np.log2(size)).astype(int)
fslice = tuple([slice(0, int(sz)) for sz in size])
new_x = np.fft.rfft2(x , fsize)
new_y = np.fft.rfft2(y , fsize)
result = np.fft.irfft2(new_x*new_y)[fslice].copy()
print("fft for my method:\n" , np.array(result.real, np.int32))
print("fft:\n" , np.array(signal.fftconvolve(x ,y,'full'), np.int32 ))
print("openpiv:\n", fft_correlate(x,y[::-1,::-1]).astype(np.int32))
print("circular: \n " ,np.array(np.fft.irfft2(np.fft.rfft2(x,size)*np.fft.rfft2(y,size))).astype(np.int32))