I'm wondering what would be the easiest way to generate a 1D gaussian kernel in python given the filter length. I think that the idea is to evaluate the normal distribution for the values of the vector [-filter-length,...,filter_length], is it correct?
So far, I've done this, but I don't know why it is not correct:
result = np.zeros( filter_length )
mid = filter_length/2
result=[(1/(sigma*np.sqrt(2*np.pi)))*(1/(numpy.exp((i**2)/(2*sigma**2)))) for i in range(-mid,mid+1)]
return result
where sigma
is the standard deviation, which is a parameter. filter-length
is also a parameter.
It's incorrect because I get, for example, for length=3 and sigma=math.sqrt(1.0/2/math.log(2))
[0.23485931967491286, 0.46971863934982572, 0.23485931967491286]
And it should be:
[0.25, 0.5, 0.25]
So, is there any problem of rounding? I don't know what is going on...
Edit I think that I should truncate somehow
Problem Solved The problem was that I wasn't normalizing. I had to divide the vector by the sum of all its components.