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
diff2 was not residual numerical error, as I expected.