Search code examples
pythonnumpyscipygaussiansmoothing

gaussian sum filter for irregular spaced points


I have a set of points (x,y) as two vectors x,y for example:

from pylab import *
x = sorted(random(30))
y = random(30)
plot(x,y, 'o-')

enter image description here

Now I would like to smooth this data with a Gaussian and evaluate it only at certain (regularly spaced) points on the x-axis. lets say for:

x_eval = linspace(0,1,11)

I got the tip that this method is called a "Gaussian sum filter", but so far I have not found any implementation in numpy/scipy for that, although it seems like a standard problem at first glance. As the x values are not equally spaced I can't use the scipy.ndimage.gaussian_filter1d.

Usually this kind of smoothing is done going through furrier space and multiplying with the kernel, but I don't really know if this will be possible with irregular spaced data.

Thanks for any ideas


Solution

  • This will blow up for very large datasets, but the proper calculaiton you are asking for would be done as follows:

    import numpy as np
    import matplotlib.pyplot as plt
    
    np.random.seed(0) # for repeatability
    x = np.random.rand(30)
    x.sort()
    y = np.random.rand(30)
    
    x_eval = np.linspace(0, 1, 11)
    sigma = 0.1
    
    delta_x = x_eval[:, None] - x
    weights = np.exp(-delta_x*delta_x / (2*sigma*sigma)) / (np.sqrt(2*np.pi) * sigma)
    weights /= np.sum(weights, axis=1, keepdims=True)
    y_eval = np.dot(weights, y)
    
    plt.plot(x, y, 'bo-')
    plt.plot(x_eval, y_eval, 'ro-')
    plt.show()
    

    enter image description here