最近学习小波变换,发现在PyWavelets 对离散信号有三种不同的方法,包括dwt、wavedec、WaveletPacket,他们简单使用、变换后的结果,输出的数据类型、格式等进行简单演示
话不多,上代码
import numpy as np
from pywt import wavedec, WaveletPacket, dwt
x = np.arange(1, 10, 1, dtype=int)
a = x
print(a)
ca = [] # 近似分量
cd = [] # 细节分量
for i in range(3):
(a, d) = dwt(a, 'db1', mode='symmetric') # 进行5阶离散小波变换
ca.append(a)
cd.append(d)
print('-'*20, 'dwt', '-'*20)
print('ca:', ca)
print('cd', cd)
print('-' * 20, 'wavedec', '-' * 20)
coeffs = wavedec(x, 'db1', mode='symmetric', level=3)
print(coeffs)
print('-' * 20, 'WaveletPacket', '-' * 20)
wp = WaveletPacket(data=x, wavelet='db1', mode='symmetric', maxlevel=3)
print(wp)
for node in wp.get_level(3, 'natural'):
print('{}-{}'.format(node.path, node.data))
从结果看,不同的变换方式在最后一层的结果一致,其他树叶由于层次不同,不可比,数据格式不同
-------------------- dwt --------------------
ca: [array([ 2.12132034, 4.94974747, 7.77817459, 10.60660172, 12.72792206]), array([ 5., 13., 18.]), array([12.72792206, 25.45584412])]
cd [array([-0.70710678, -0.70710678, -0.70710678, -0.70710678, 0. ]), array([-2., -2., 0.]), array([-5.65685425, 0. ])]
-------------------- wavedec --------------------
[array([12.72792206, 25.45584412]), array([-5.65685425, 0. ]), array([-2., -2., 0.]), array([-0.70710678, -0.70710678, -0.70710678, -0.70710678, 0. ])]
-------------------- WaveletPacket --------------------
: [1 2 3 4 5 6 7 8 9]
aaa-[12.72792206 25.45584412]
aad-[-5.65685425 0. ]
ada-[-2.82842712 0. ]
add-[-6.66133815e-16 0.00000000e+00]
daa-[-1.41421356 0. ]
dad-[4.4408921e-16 0.0000000e+00]
dda-[-7.85046229e-17 0.00000000e+00]
ddd-[-7.85046229e-17 0.00000000e+00]