zero-padded edges in convolution

25 views (last 30 days)
Ke Lu
Ke Lu on 19 Sep 2015
Edited: Thorsten on 21 Sep 2015
The convolution reference material explain the use of the parameter shape, but I don't understand what means "Only those parts of the convolution that are computed without the zero-padded edges". What are the zero padded edges on this context?
For example, if I use u=[1,0,1] and v=[2,7] and convolve them, the default solution is [2,7,2,7], using 'same' gives me [7,2,7] and using 'valid' gives me [7,2].
Using the formula in Convolution and polynomial multiplication , if I am not wrong, gives me the following resuts:
  • w(1)=u(1)*v(1)=2
  • w(2)=u(1)*v(2)+u(2)*v(1)=7+0=7
  • w(3)=u(1)*v(3)+u(2)*v(2)+u(3)*v(1)=none+0+2=2
  • w(4)=u(1)*v(4)+u(2)*v(3)+u(3)*v(2)+u(4)*v(1)=none+none+7+none=7
I put 'none' where I do not have the related matrix element. So, it raises more questions: a) Why should I use 'same'? b) Why should I use 'valid'? c) As the resulting matrix can have an even number of elements and the source matrix can have an even number, how is it possible to get the 'center' part when using 'same'?
Thanks!!!

Answers (1)

Image Analyst
Image Analyst on 19 Sep 2015
If you're filtering an image and you need to do something with that image that depends on it being the same size as some other image, like computing MSE or subtracting them or masking it or whatever, then you should use the 'same' option so the output will have the same size.
If you want to compute the values only for elements or pixels where the window does not hang off the edge of the signal or picture into undefined zones (which we make zeros because it has to have some value if you are going to use it), then you can use 'valid'. Using 'valid' means no part of the sliding window will "hang off" the edge of the image or signal. For example, if you're blurring an image with a huge window, you may not care that you lose a half window width all the way around your image (if you use 'valid') but you'd rather not end up with an image that fades away at the edges because a bunch of zeros got included in the weighted average (which is what happens if you use 'same' or 'full').
If you want the theoretical full convolution, then use full. It will slide the window along until the last tip of the window just barely touches your signal or image. Of course most of the signal is zero in that case with only one element or column or row overlapping.
  3 Comments
Image Analyst
Image Analyst on 21 Sep 2015
'valid' does not allow any part of the sliding window to leave the larger image it's sliding over. So if you look at where the center of that window is when it slides all over the image, it will trace out an area that is half a window width inside the larger image.
You might look at the edge effect explanation in this page:
Thorsten
Thorsten on 21 Sep 2015
Edited: Thorsten on 21 Sep 2015
The case 1 is out of the window, because there is no u(0)*v(2), and case 4 is out of the window, because there is no u(4). You can think of convolution as flipping the filter v left to right and then moving this flipped filter across u. 'Valid' means that you consider only those positions where flipped v completely overlaps with u; 'full' means that you also take positions of partial overlap into account, i.e., case 1 and 4 in your example.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!