How do I apply a 2D circular convolution without zero-padding in MATLAB?

37 views (last 30 days)
I would like to apply the CONV2 function to two matrices A and B. However, I do not want to zero-pad one of the two matrices.
I would like to use the matrix expansion methods available with the PADARRAY function from the Image Processing Toolbox. I would like to pad one matrix with circular repetition of elements within the dimension, repeating border elements, or mirror reflections of itself.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Oct 2013
The ability to apply CONV2 by methods other than zero-padding is not available in MATLAB.
However, you can make use of CONV2 with PADARRAY to manually apply 2D convolution with circular repetition, repeating border elements, or mirror reflections of itself.
For example, if you want to convolve the following two matrices:
A = [1:2; 2:3;];
B = [4:6; 5:7; 6:8;];
A =
1 2
2 3
B =
4 5 6
5 6 7
6 7 8
Firstly, expand one of the two matrices with either circular repetition of elements within the dimension, repeating border elements, or mirror reflections of itself using the PADARRAY function.
xLayer = 2;
B_x = padarray(B,[xLayer xLayer],'circular'); % B is expanded with an extra 2 layers of mirror values
B_x =
6 5 5 6 7 7 6
5 4 4 5 6 6 5
5 4 4 5 6 6 5
6 5 5 6 7 7 6
7 6 6 7 8 8 7
7 6 6 7 8 8 7
6 5 5 6 7 7 6
Next, convolve the matrices A and B_x together with CONV2:
C_x = conv2(A,B_x) % CONV2 on replicated mirror values
C_x =
6 17 15 16 19 21 20 12
17 42 37 40 48 53 50 28
15 37 32 35 43 48 45 25
16 40 35 38 46 51 48 27
19 48 43 46 54 59 56 32
21 53 48 51 59 64 61 35
20 50 45 48 56 61 58 33
12 28 25 27 32 35 33 18
Lastly, choose the correct answers:
C_correct = C_x(xLayer+1:end-xLayer, xLayer+1:end-xLayer)
C_correct =
32 35 43 48
35 38 46 51
43 46 54 59
48 51 59 64

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!