I was writing this Jupyter Notebook. Then, I unexpectedly faced two visually similar output, but their numerical results were very different.

This is the code I used for convolution in the time domain:

filtered_k1 = sp.signal.convolve2d(img, k1, mode='same')
filtered_k2 = sp.signal.convolve2d(img, k2, mode='same')

def FT(x):
    return np.fft.fft2(x)

def iFT(x):
    return np.fft.ifft2(x)

def pad_kernel(k, x):
    x_h, x_w = x.shape
    k_h, k_w = k.shape
    padding = [[0, x_h - k_h], [0, x_w - k_w]]
    return np.pad(k, padding, mode='constant', constant_values=0)

k1_big = pad_kernel(k1, img)
k2_big = pad_kernel(k2, img)

K1_BIG = FT(k1_big)
K2_BIG = FT(k2_big)

filtered_K1 = np.real(iFT(IMG * K1_BIG))
filtered_K2 = np.real(iFT(IMG * K2_BIG))

diff1 = filtered_K1 - filtered_k1
diff2 = filtered_K2 - filtered_k2

The RSS on diff1 and diff2 was not residual numerical error, as I expected.

  • 1
    $\begingroup$ The convolution using the fft results in a circular convolution; is that what is causing your difference in the results? $\endgroup$ – Dan Boschen Jan 4 '19 at 14:14
  • $\begingroup$ I am not sure. If this is the case, I would like to know how to set up sp.signal.convolve2d to yield the same output $\endgroup$ – Eduardo Reis Jan 4 '19 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.