pythonscipysignalssignal-processinggaussian

1D Gaussian Smoothing with Python - Sigma equals filter length?


I would like to smooth time series data. For this I would like to use Python.

Now I have already found the function scipy.ndimage.gaussian_filter1d.

For this, the array and a sigma value must be passed.

Now to my question:

Is the sigma value equal to the filter length? I would like to run a filter of length 365 over the data. Would it then be the correct procedure to set this sigma value to 365 or am I confusing things?


Solution

  • sigma defines how your Gaussian filter are spread around its mean. You can create gaussian filter with a specific size like below.

    import numpy as np
    import matplotlib.pyplot as plt
    
    sigma1 = 3
    sigma2 = 50
    
    def gaussian_filter1d(size,sigma):
        filter_range = np.linspace(-int(size/2),int(size/2),size)
        gaussian_filter = [1 / (sigma * np.sqrt(2*np.pi)) * np.exp(-x**2/(2*sigma**2)) for x in filter_range]
        return gaussian_filter
    
    fig,ax = plt.subplots(1,2)
    ax[0].plot(gaussian_filter1d(size=365,sigma=sigma1))
    ax[0].set_title(f'sigma= {sigma1}')
    ax[1].plot(gaussian_filter1d(size=365,sigma=sigma2))
    ax[1].set_title(f'sigma= {sigma2}')
    plt.show()
    
    

    Here is the effect of sigma on the Gaussian filter.

    enter image description here

    Later, you might convolve your signal with your Gaussian filter.