[SciPy-User] numpy.convolve needs to be normalized?

josef.pktd at gmail.com josef.pktd at gmail.com
Wed Dec 14 09:47:44 EST 2011


On Tue, Dec 13, 2011 at 10:59 AM, Kevin Gullikson
<kevin.gullikson at gmail.com> wrote:
> Hi all,
>
> I have been playing with the numpy.convolve function. It seems that the
> general behavior is what I expect, but the values are all too high. Does the
> result need to be normalized in some way? Here is some example code that
> should replicate the figure in the wikipedia page for convolution:
>
> import numpy
> import pylab
>
> #Make some square data
> x = numpy.arange(0,10,0.01)
> left1 = 2
> right1=3
> left2=4
> right2=5
> y1=numpy.zeros(x.size)
> y2=numpy.zeros(x.size)
> for i in range(x.size):
>   if x[i] >=left1 and x[i] <= right1:
>     y1[i] = 1
>   if x[i] >=left2 and x[i] <= right2:
>     y2[i] = 1
>
> #Convolve
> conv = numpy.convolve(y1,y2,mode="same")
> pylab.plot(x,conv)
> pylab.show()
>
>
> The peak should be at x=2 (and it is), and should have a height of 1, since
> the maximum area under the two curves is 1. However, the height of the peak
> is about 101

convolve is discrete, just the convolution sum of points.

If the window sums to 1, then you just get a weighted average.

conv = numpy.convolve(y1,y2/y2.sum(),mode="same")

>>> conv.max()
1.0000000000000002

Josef
>
> Kevin Gullikson
>
>
> _______________________________________________
> 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