欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

滤波器之SG滤波器:(python实现)

程序员文章站 2022-07-12 09:49:29
...
#%% SG_filter
def sgoal_filter(data,fs,window_size,order):

    if window_size == None:
        window_size = fs // 10       
    if window_size % 2 == 0 or window_size == 0: 
        window_size += 1
    
    arr = []
    step = int((window_size-1)/2)
    for i in range(window_size):
        a = []
        for j in range(order):
            y_val = np.power(-step + i, j)
            a.append(y_val)
        arr.append(a)
    
    arr = np.mat(arr)
    arr = arr * (arr.T * arr).I * arr.T
    
    a = np.array(arr[step])
    a = a.reshape(window_size)
    
    data = np.insert(data, 0, [data[0] for i in range(step)])
    data = np.append(data, [data[-1] for i in range(step)])
    
    qlist = []
    for i in range(step, data.shape[0] - step):
        arra = []
        for j in range(-step, step+1):
            arra.append(data[i +j])   
        b = np.sum(np.array(arra) * a)
        qlist.append(b)
    return qlist
相关标签: 信号处理