Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Convolution with a Gaussian

I need to convolute the next curve with a Gaussian function of specific parameters centered at 3934.8A.

enter image description here

The problem I see is that my curve is a discrete array and the Gaussian would be a well define continuos function. How can I make this work?

like image 217
Pythonice Avatar asked Jun 10 '14 19:06

Pythonice


1 Answers

To do this, you need to create a Gaussian that's discretized at the same spatial scale as your curve, then just convolve.

Specifically, say your original curve has N points that are uniformly spaced along the x-axis (where N will generally be somewhere between 50 and 10,000 or so). Then the point spacing along the x-axis will be (physical range)/(digital range) = (3940-3930)/N, and the code would look like this:

dx = float(3940-3930)/N
gx = np.arange(-3*sigma, 3*sigma, dx)
gaussian = np.exp(-(x/sigma)**2/2)
result = np.convolve(original_curve, gaussian, mode="full")

Here this is a zero-centered gaussian and does not include the offset you refer to (which to me would just add confusion, since the convolution by its nature is a translating operation, so starting with something already translated is confusing).

I highly recommend keeping everything in real, physical units, as I did above. Then it's clear, for example, what the width of the gaussian is, etc.

like image 197
tom10 Avatar answered Oct 01 '22 02:10

tom10