首页 > 其他分享 >限幅平均滤波法

限幅平均滤波法

时间:2023-01-04 12:13:55浏览次数:43  
标签:noise sinx 滤波 plt 限幅 np 平均 mean

#限幅平均滤波法
import scipy.signal as signal
import numpy as np
import pylab as pl
import matplotlib.pyplot as plt
import matplotlib

 
'''
限幅平均滤波法
先限幅,再进行递推平均滤波

Amplitude:    限制最大振幅
'''


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

def AmplitudeLimitingAverage(inputs,per,Amplitude):
    
    #先进行限幅滤波
    lasttmp = inputs[0]
    flag = 0
    mean = []
    for index,tmp in enumerate(inputs):
        if np.abs(tmp - lasttmp) > Amplitude:
            mean.append(lasttmp)
            flag = 1
        lasttmp = tmp
        if flag ==0:
            mean.append(lasttmp)
        flag = 0
    
    #再进行递推平均滤波
    mean = np.array(mean)
    mean = SlidingAverage(mean,per)
    return mean

a = np.array([1,2,3,4,5,6,8,9,3,5,6,8])
a = a.astype(np.float64)

print(AmplitudeLimitingAverage(a,3,1))

#生成数据
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 = AmplitudeLimitingAverage(y,3,0.2)
print(y)
plt.plot(x,y,label='sinx+noise+Amp')
plt.legend(['sinx','sinx+noise','sinx+noise+Amp'])
plt.show()

 

标签:noise,sinx,滤波,plt,限幅,np,平均,mean
From: https://www.cnblogs.com/lpj1393822011/p/17024448.html

相关文章