网上的很多都看不懂,而且好像还有错,所以只好自己写了
#递归平均滤波法,N=3 import scipy.signal as signal import numpy as np import pylab as pl import matplotlib.pyplot as plt import matplotlib def SlidingAverage(inputs,per): mean = [] for i in range(len(inputs)): if i ==0: mean.append(inputs[0]) elif i <(per): mean.append(inputs[:i+1].mean()) else: mean.append(inputs[i-per+1:i+1].mean()) return mean a = np.array([1,2,3,4,5,6,7,8]) print(SlidingAverage(a,3)) #生成数据 x = np.arange(0,8.1,0.1) y = np.sin(x*np.pi) plt.plot(x,y,label='sinx') #对y加入噪声 noise = np.random.normal(-0.2,0.2,len(x)) y = y + noise plt.plot(x,y,label='sinx+noise') print(y) #对加入噪声的信号进行滤波 y = SlidingAverage(y,3) print(y) plt.plot(x,y,label='sinx+noise+Slid') plt.legend(['sinx','sinx+noise','sinx+noise+Slid']) plt.show()
sinx表示sinx原函数
sinx+noise表示sinx加了噪音以后的图像
sinx+noise+Slid表示对加了噪声以后的信号进行递归平局滤波处理后的图像
标签:inputs,noise,递归,sinx,奇数,滤波,import From: https://www.cnblogs.com/lpj1393822011/p/17023679.html