What is the discrete form of the convolution theorem in using np.fft?

83 Views Asked by At

First, this is not a duplicate! I found similar questions on this site but they do not answer this question.

I tried to use the convolution theorem in Python. I have two N*N arrays where I can change the value of N.

For example,

import numpy as np
import scipy.signal as sp
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
b = np.array([[1,2,3],[4,5,6],[7,8,9]])
np.fft.fft2(a*b)
sp.convolve2d(np.fft.fft2(a), np.fft.fft2(b), mode='same', boundary='wrap') / 9

The /9 arises becasue Numpy fft uses backward normalization and I need to divide the number of elements here. However, the above two do not agree. I don't know where I went wrong.

I need to note that the above code somehow works when N = 2, i.e. when the size of the array is 2x2.

Also, FT( convolve(a,b) ) = FT(a) times FT(b) holds, but the alternate form I have here does not hold FT(a times b) = convolve( FT(a) , FT(b) )

I want to know where I went wrong and a correct example for FT(a times b) = convolved (FT(a), FT(b))

1

There are 1 best solutions below

1
vakvakvak On

FFT( convolution(a,b)) = FFT(a) * FFT(b)

np.fft.fft2(a)*np.fft.fft2(b)
array([[2025.   +0.j        ,   13.5 -23.3826859j ,   13.5 +23.3826859j ],
       [ 121.5-210.44417312j,    0.   +0.j        ,    0.   +0.j        ],
       [ 121.5+210.44417312j,    0.   +0.j        ,    0.   +0.j        ]])

np.fft.fft2(sp.convolve2d(a,b, mode='same', boundary='wrap') )
array([[2025.   +0.j        ,   13.5 +23.3826859j ,   13.5 -23.3826859j ],
       [ 121.5+210.44417312j,    0.   +0.j        ,    0.   +0.j        ],
       [ 121.5-210.44417312j,    0.   +0.j        ,    0.   +0.j        ]])