# Python Convolve2D functionality in Mathematica

I am new to mathematica. I am looking for an alternative for scipy.signal.convolve2d. I did find functions like ListConvolve, but it returns a 1D array whereas I expect a 2-dimensional array containing a subset of the discrete linear convolution. Also help me as to how can i specify mode as "same" like in numpy.

Python code (using NumPy)

d = convolve2d(var, kernel, mode="same")


Mathematica Code

d = ListConvolve[kernel,var]


• Although I'm sure that some users here are conversant in NumPy, many here (including myself, for instance) may not know what the convolve2D function does, and what its output should be. You may want to expand your description, and perhaps include an example with expected output, a link to a documentation page, or similar. – MarcoB Jun 29 '16 at 23:47
• You'll want to look at the third overhang argument and the fourth padding argument of ListConvolve[]; in particular, ListConvolve[] does not pad by default, so you explicitly need to give a fourth argument of 0 if you want it to act like the corresponding function on NumPy. – J. M. will be back soon Jun 30 '16 at 0:35
• Also note that ListConvolve does in fact work with 2D arrays as well. – Jens Jun 30 '16 at 0:36

Like J.M. said, the use of ListConvolve can be a bit tricky, because by default it cuts overhangs and has no padding. From your description i guess you are more interested in something like this:

MatrixPlot[
ListConvolve[GaussianMatrix, BoxMatrix - DiamondMatrix, {1, -1}, {0, 0}]
] $\mspace{172mu}\Big\Updownarrow$

This uses the overhang parameters {1,-1} to have maximum overhang on both ends. The next parameter {0,0} adds zero padding in both dimensions to make sure we don't get cyclical convolution (which is the default).

For more details see the documentation of ListConvolve but this example should get you started ;)