python实现matlab的smooth函数

输入num为奇数,若输入的为偶数,则会减一,与matlab相同。

def smooth(x,num):
    if num // 2 == 0: # 偶数转奇数
        num -= 1
    length = len(x)
    y = np.zeros(length)
    N = (num - 1) / 2
    for i in range(0, length):
        cont_0 = i
        cont_end = length - i - 1
        if cont_0 in range(0,int(N)) or cont_end in range(0,int(N)):
            cont = min(cont_0,cont_end)
            y[i] = np.mean(x[i - cont : i + cont + 1])
        else:
            y[i] = np.mean(x[i - int(N) : i + int(N) + 1])
    return y

原理

python实现matlab的smooth函数_第1张图片

你可能感兴趣的:(python,python)