Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@endolith
Created December 16, 2012 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save endolith/4303970 to your computer and use it in GitHub Desktop.
Save endolith/4303970 to your computer and use it in GitHub Desktop.
SciPy convolve2d vs Matlab conv2 'same' mode
In [198]: f = [[2,3,4],[1,6,7]]
In [199]: g = [[9,1,0],[2,5,8],[1,3,3]]
In [200]: convolve2d(f,g)
Out[200]:
array([[ 18, 29, 39, 4, 0],
[ 13, 71, 108, 51, 32],
[ 4, 26, 71, 104, 68],
[ 1, 9, 28, 39, 21]])
In [201]: convolve2d(f,g,'same')
Out[201]:
array([[ 71, 108, 51],
[ 26, 71, 104]])
In [202]: convolve2d(g,f,'same')
Out[202]:
array([[ 29, 39, 4],
[ 71, 108, 51],
[ 26, 71, 104]])
Compare with the example in http://books.google.com/books?id=fTvQkLBLyK8C&lpg=PA43&ots=0HC1LsH0cE&dq=conv2%20same%20example&pg=PA43#v=onepage&q=conv2%20same%20example&f=false
71 108 51
26 71 104
and
71 108 51
26 71 104
9 28 39
http://www.mathworks.com/help/matlab/ref/conv2.html "If there are an odd number of rows or columns, the "center" leaves one more at the beginning than the end."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment