首先在python中,小波处理的库为Pywt
安装使用pip:命令如下:
pip install PyWavelets
小波分解使用函数pywt.wavedec
完整函数为:
def wavedec(data, wavelet, mode='symmetric', level=None, axis=-1):
"""
Multilevel 1D Discrete Wavelet Transform of data.
Parameters
----------
data: array_like
Input data
wavelet : Wavelet object or name string
Wavelet to use
mode : str, optional
Signal extension mode, see :ref:`Modes `.
level : int, optional
Decomposition level (must be >= 0). If level is None (default) then it
will be calculated using the ``dwt_max_level`` function.
axis: int, optional
Axis over which to compute the DWT. If not given, the
last axis is used.
参数一般只需要添加三个,分别是data,wavelet,level:
wavelet指的是小波族,一共有下面几种,一般用db就好。[‘haar’, ‘db’, ‘sym’, ‘coif’, ‘bior’, ‘rbio’, ‘dmey’]
level指的是分解的阶数,设置为n就返回一个n维的list[cAn, cDn, cDn-1, …, cD2, cD1],n为分解阶次,cAn是逼近系数数组,后面的依次是细节系数数组。
重构使用函数pywt.waverec
完整函数为:
def waverec(coeffs, wavelet, mode='symmetric', axis=-1):
"""
Multilevel 1D Inverse Discrete Wavelet Transform.
Parameters
----------
coeffs : array_like
Coefficients list [cAn, cDn, cDn-1, ..., cD2, cD1]
wavelet : Wavelet object or name string
Wavelet to use
mode : str, optional
Signal extension mode, see :ref:`Modes `.
axis: int, optional
Axis over which to compute the inverse DWT. If not given, the
last axis is used.
参数设置一般只需要两个:
coeffs为小波分解并处理后的list。
wavelet和函数pywt.wavedec保持一致即可
小波去噪使用函数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, 小于等于阈值的值不变
给一个完整的分解去噪重构的例子,大概是这样子的。哈哈
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')