2
$\begingroup$

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.

Convolve2D: http://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.convolve2d.html

Python code (using NumPy)

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

Mathematica Code

d = ListConvolve[kernel,var]

Thanks in advance.

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – MarcoB Jun 29 '16 at 23:47
  • 3
    $\begingroup$ 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. $\endgroup$ – J. M. is away Jun 30 '16 at 0:35
  • 2
    $\begingroup$ Also note that ListConvolve does in fact work with 2D arrays as well. $\endgroup$ – Jens Jun 30 '16 at 0:36
2
$\begingroup$

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[10], BoxMatrix[20] - DiamondMatrix[20], {1, -1}, {0, 0}]
]

ListConvolve 2D convolution example

$\mspace{172mu}\Big\Updownarrow$

$\mspace{30mu}$ListConvolve input

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 ;)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.