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

Warren Weckesser warren.weckesser at enthought.com
Wed Dec 14 09:46:25 EST 2011


On Tue, Dec 13, 2011 at 9: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<http://en.wikipedia.org/wiki/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
>


The peak of 101 is correct.  numpy.convolve is a *discrete*
convolution--scroll down a bit in the wikipedia article to see the
definition.  Your y1 and y2 each contain 101 consecutive 1s, so the peak
value in the convolution should be 101, as you observed.

Warren
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20111214/8d6a49cc/attachment.html>


More information about the SciPy-User mailing list