Search code examples
pythonconvolution

Python - Convolution with a Gaussian


I need to convolve this curve with a parametrized Gaussian function centered at 3934.8A:

enter image description here

The problem I see is that my curve is a discrete array and the Gaussian is a continuous function. How can I make this work?


Solution

  • 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.