0

I am wondering is there a program defined for 2D circular convolution? If not, I am looking for how to perform it. I did search for it. Could not find anything. So I am wondering does anybody know it. Here is a sample 2D data I have taken

a= np.array([[ -3-3j, 0-10j,  +3-3j, +3-3j],
                   [-10+0j, 0+ 0j, +10+0j, 0-10.8j],
                   [ -3+3j, 0+10j,  +3+3j, 0-10.7j],
                   [-10+0j, 0+ 0j, +2+0.2j, -1+0j]]); 
b = np.array([[ -3-3.2j, 0-10.8j,  +3-3j, +3.2+4j],
                   [-10+0j, 0+ 0j, +2+0.2j, -1+0j],
                   [ -3+3j, 0+10j,  +3+4j, 0+ 0j],
                   [ -3-3j, 0-10j,  +3-3j, +3-3j]]);

I tried c = scipy.signal.convolve2d(a, b, boundary='wrap'). It's output is

[[ 166.96 -60.j    -66.4   -8.1j  -393.4  -37.6j  -158.9  -30.1j
   166.96 -60.j    -66.4   -8.1j  -393.4  -37.6j ]
 [ -25.84 +35.2j    46.1  +77.1j   -23.9  -76.86j  -62.66 -35.5j
   -25.84 +35.2j    46.1  +77.1j   -23.9  -76.86j]
 [ 106.64+102.2j   106.66 +70.9j    96.    -2.64j   61.56+166.3j
   106.64+102.2j   106.66 +70.9j    96.    -2.64j]
 [ 180.   -46.j    -15.06-113.16j -271.16 -22.9j   -89.44+138.j
   180.   -46.j    -15.06-113.16j -271.16 -22.9j ]
 [ 166.96 -60.j    -66.4   -8.1j  -393.4  -37.6j  -158.9  -30.1j
   166.96 -60.j    -66.4   -8.1j  -393.4  -37.6j ]
 [ -25.84 +35.2j    46.1  +77.1j   -23.9  -76.86j  -62.66 -35.5j
   -25.84 +35.2j    46.1  +77.1j   -23.9  -76.86j]
 [ 106.64+102.2j   106.66 +70.9j    96.    -2.64j   61.56+166.3j
   106.64+102.2j   106.66 +70.9j    96.    -2.64j]]

It is giving correct result. But this correct result is only a subset of the above output. i.e. I have extract the first few rows and columns of size as much as the input size. So this way I got the correct output.

[[ 166.96 -60.j    -66.4   -8.1j  -393.4  -37.6j  -158.9  -30.1j ]
 [ -25.84 +35.2j    46.1  +77.1j   -23.9  -76.86j  -62.66 -35.5j ]
 [ 106.64+102.2j   106.66 +70.9j    96.    -2.64j   61.56+166.3j ]
 [ 180.   -46.j    -15.06-113.16j -271.16 -22.9j   -89.44+138.j  ]]

I tried spicy.fft.fftshift with using mode='same' in spicy.fft.convolve2D but it is not giving correct output.

I am looking for either a built in function or normal code

  • Circular convolution in 2D is equivalent to conventional 2D convolution with a periodically extended input. As far as I understand, that is the `boundary='wrap'` parameter of [`scipy.signal.convolve2d`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html). – Cpt.Hook Dec 09 '22 at 08:12
  • I did use `signal.convolve2d(a, b, boundary='wrap')`. It is not giving correct result. I used all the _modes_ `same`, `full`,`valid`. Nothing is giving correct result. –  Dec 09 '22 at 08:19
  • `scipy.signal.convolve2d(a, b, boundary='wrap')` has given correct output , but its size is big. _I have to extract a first few rows and columns of size as much as input array_. This adds to the delay of the program. Is there is way we can directly get the same size output, without extracting first few rows and columns of size as much as input array elements. –  Dec 10 '22 at 05:29
  • Did you try `mode='same'` as written in the documentation? – Cpt.Hook Dec 10 '22 at 07:32
  • yes. I did. As written in the document `mod='same'` gives the center part of the result produced by `c=scipy.signal.convolve2d(a, b, boundary='wrap')` which is giving delay for large input (512*512 arrays). But the required correct output is in the first rows and columns of the result. I have to use `output=c[0:4,0:4]; print(output);` to get the correct output. –  Dec 10 '22 at 13:07

0 Answers0