# Applying a 2D Convolution Using 2D FFT

So I was following the article Victor Podlozhnyuk (nVidia) - FFT Based 2D Convolution (Page 7).

I have expanded the kernel to the correct way they have done it. However when it comes to the part on clamping to the edge its very confusing. I would love for someone to explain this as i require the image convolved as the same shape before the padding, but in this article it just shows how to convolve with clamp to edge but not how to recover it.

code as of now

def circExt(k, rows, cols):

k_c = k

k_c = np.pad(k_c,((0, int(rows-k.shape)), (0, int(cols-k.shape))))

k_c = np.roll(k_c, int(-radiusV), axis = 0)
k_c = np.roll(k_c, int(-radiusH), axis = 1)

return k_c

def testfft2(image, kernel):
imageC = image.copy()
kSize = (kernel.size // 2) + 1
kernelShape = tuple(ti//2 for ti in kernel.shape)

k_width = kernel.shape
k_height = kernel.shape

centery ,centerx = kernelShape

imageC = np.pad(imageC, ((0, int(centery)), (0, int(centerx))) , mode = "edge")

imageC = np.pad(imageC,((0, int(k_height-centery-1)), (0, int(k_width-centerx-1))), mode = "wrap" )

kernelShift = circExt(kernel, imageC.shape, imageC.shape)

imageC = np.fft.fft2(imageC)
kernel= np.fft.fft2(kernelShift)
output = np.real(np.fft.ifft2(np.multiply(imageC, kernel)))

return output