Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中平滑曲线_Python_Numpy_Signal Processing_Gaussian_Smoothing - Fatal编程技术网

如何在python中平滑曲线

如何在python中平滑曲线,python,numpy,signal-processing,gaussian,smoothing,Python,Numpy,Signal Processing,Gaussian,Smoothing,我有一条熵曲线(1d numpy数组),但这条曲线有很多噪音。 我想用平滑法删除噪声 这是我的曲线图: 我曾尝试使用Kaiser-Bessel滤波器制作卷积产品来解决此问题: gaussian_curve = window_kaiser(windowLength, beta=20) # kaiser filter gaussian_curve = gaussian_curve / sum(gaussian_curve) for i in range(0, windows_number):

我有一条熵曲线(1d numpy数组),但这条曲线有很多噪音。 我想用平滑法删除噪声

这是我的曲线图:

我曾尝试使用Kaiser-Bessel滤波器制作卷积产品来解决此问题:

gaussian_curve = window_kaiser(windowLength, beta=20)  # kaiser filter
gaussian_curve = gaussian_curve / sum(gaussian_curve)

for i in range(0, windows_number):
     start = (i * step) + 1
     end = (i * step) + windowLength
     convolution[i] = (np.convolve(entropy[start:end + 1], gaussian_curve, mode='valid'))
     entropy[i] = convolution[i][0]
但此代码返回以下错误:

File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 822, in convolve
    raise ValueError('v cannot be empty')
ValueError: v cannot be empty
具有“valid”模式的运算符返回重叠中的中心元素,但在本例中返回空元素

有没有一种简单的方法来应用平滑

谢谢

好的,我解决了。 我采用了另一种方法:

守则:

def savitzky_golay(y, window_size, order, deriv=0, rate=1):

    import numpy as np
    from math import factorial

    try:
        window_size = np.abs(np.int(window_size))
        order = np.abs(np.int(order))
    except ValueError, msg:
        raise ValueError("window_size and order have to be of type int")
    if window_size % 2 != 1 or window_size < 1:
        raise TypeError("window_size size must be a positive odd number")
    if window_size < order + 2:
        raise TypeError("window_size is too small for the polynomials order")
    order_range = range(order+1)
    half_window = (window_size -1) // 2
    # precompute coefficients
    b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
    m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
    # pad the signal at the extremes with
    # values taken from the signal itself
    firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
    lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
    y = np.concatenate((firstvals, y, lastvals))
    return np.convolve( m[::-1], y, mode='valid')
结果是:

FYI:scipy 0.14.0(即将发布)在
scipy.signal.savgol_过滤器中实现了Savitzky-Golay过滤器
。可能与
entropy = np.array(entropy)
entropy = savitzky_golay(entropy, 51, 3) # window size 51, polynomial order 3