1.pywt.threshold
def threshold(data, value, mode='soft', substitute=0):
Parameters
----------
data : array_like
Numeric data.
value : scalar
Thresholding value.
mode : {
'soft', 'hard', 'garrote', 'greater', 'less'}
Decides the type of thresholding to be applied on input data. Default
is 'soft'.
substitute : float, optional
Substitute value (default: 0).
参数分别是数据,阈值,模式,和替换值
value是用来和data里的数据做比较的。
mode有四种,{‘soft’, ‘hard’, ‘garrote’, ‘greater’, 'less}指的是不同的替换方式。后面有一段代码能很好的理解。
substitute是用来做替换的值。
#使用小波分析进行阈值去噪声,使用pywt.threshold
data = np.linspace(1, 10, 10)
print(data)
# [ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
# pywt.threshold(data, value, mode, substitute) mode 模式有4种,soft, hard, greater, less; substitute是替换值
data_soft = pywt.threshold(data=data, value=6, mode='soft', substitute=12)
print(data_soft)
# [12. 12. 12. 12. 12. 0. 1. 2. 3. 4.] 将小于6 的值设置为12, 大于等于6 的值全部减去6
data_hard = pywt.threshold(data=data, value=6, mode='hard', substitute=12)
print(data_hard)
# [12. 12. 12. 12. 12. 6. 7. 8. 9. 10.] 将小于6 的值设置为12, 其余的值不变
data_greater = pywt.threshold(data, 6, 'greater', 12)
print(data_greater)
# [12. 12. 12. 12. 12. 6. 7. 8. 9. 10.] 将小于6 的值设置为12,大于等于阈值的值不变化
data_less = pywt.threshold(data, 6, 'less', 12)
print(data_less)
# [ 1. 2. 3. 4. 5. 6. 12. 12. 12. 12.] 将大于6 的值设置为12, 小于等于阈值的值不变
2.去噪实例
给一个完整的分解去噪重构的例子,大概是这样子的
dataset = Read_list('D:\\Program Files\\python_project\\Zdata\\DEAP\\Video\\s01\\2')
data = dataset[0]
x = range(0, len(data))
w = pywt.Wavelet('db8') # 选用Daubechies8小波
maxlev = pywt.dwt_max_level(len(data), w.dec_len)
print("maximum level is " + str(maxlev))
threshold = 0.5 # Threshold for filtering
# Decompose into wavelet components, to the level selected:
coeffs = pywt.wavedec(data, 'db8', level=maxlev) # 将信号进行小波分解
for i in range(1, len(coeffs)):
coeffs[i] = pywt.threshold(coeffs[i], threshold*max(coeffs[i])) # 将噪声滤波
datarec = pywt.waverec(coeffs, 'db8')