Python realizes the smooth function of matlab

The input num is an odd number, if the input is an even number, it will be reduced by one, the same as 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

principle

Guess you like

Origin blog.csdn.net/qq_54875519/article/details/126183174