Search code examples
pythonexcelcurve-fittingsmoothingcdf

Non-linear curve (multi-peak) fitting


Need help smoothening the red graph below to something that looks like blue graph.

enter image description here

I have tried moving average, problem with that is, it didn't remove the noise, further shifts the position of the curve. I would like to maintain the same position and area under the curve (here it is 100%). Is fitting multiple small curves the solution here? or is a way to fit a curve to this in python or excel?


Solution

  • You have several options available. For instance, you could either use a Gaussian or a Savitzky-Golay filter.

    Considering a noisy signal (1D numpy array), like this one: enter image description here

    the following snippet allows you to denoise it. Change the parameters according to your particular use case:

    from scipy.ndimage import gaussian_filter1d
    from scipy.signal import savgol_filter
    
    np.random.seed(42)
    
    x = np.linspace(0.0, 100.0, 300)
    y = f(x) # your signal
    
    y_gaussian = gaussian_filter1d(y, 3)
    y_savgol = savgol_filter(y, 8, 2)
    
    plt.plot(x, y, label="Noisy Signal")
    plt.plot(x, y_gaussian, label="Gaussian Filter")
    plt.plot(x, y_savgol, label="Savgol Filter")
    plt.legend()
    plt.show()
    

    enter image description here

    and zooming-in to see more details: enter image description here