[SciPy-User] 2d convolution

josef.pktd at gmail.com josef.pktd at gmail.com
Sat Mar 20 08:49:24 EDT 2010


On Sat, Mar 20, 2010 at 7:51 AM, Nico Schlömer <nico.schloemer at gmail.com> wrote:
> Hi,
>
> I'm trying to compute the the convolution if s 2D array, and I see
> that there are several ways in SciPy to do that. As the original data
> C and the kernel R are about the same size in my case, I'd profit from
> an FFT-based implementation, which I see right now is given by
>
>    scipy.signal.fftconvolve( C, R,
>                                       mode='same' )
>
> and also
>
>    scipy.stsci.convolve.convolve2d( data=C, kernel=R,
>                                                  mode='constant', cval=0.0,
>                                                  fft=1 )
>
> The second method is much faster than the first, and as far as I can
> see it would spit out the the same results.
>
> Now, when the kernel is actually larger than the data, the resulting
> array would have the shape of the kernel. Is there any way to restrict
> the computations to the size of the data? At first, I thought that was
> what "mode='same'"" is for.
> I tried cutting the extra data off of the resulting array, but I'm not
> quite sure which is the part that I would like to get rid of.
>
> Any hints here?

It's always good to take a look at the source

fftconvolve

    elif mode == "same":
        if product(s1,axis=0) > product(s2,axis=0):
            osize = s1
        else:
            osize = s2
        return _centered(ret,osize)
    elif mode == "valid":
        return _centered(ret,abs(s2-s1)+1)

from scipy.signal.signaltools import _centered

and it should be possible to  set osize to s1 = array(in1.shape)   (C
in your notation)

scipy.stsci has been removed recently from scipy and will not be in
the scipy 0.8 version

(I didn't try myself whether it works)

Josef

>
> Cheers,
> Nico
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list