python 实现小波变换的例子
时间: 2023-11-28 14:59:21 浏览: 71
以下是 Python 实现小波变换的例子:
```python
import numpy as np
import pywt
# 生成测试数据
data = np.array([1, 2, 3, 4, 5, 6, 7, 8])
# 指定小波类型和分解层数
wavelet = 'db4'
level = 2
# 进行小波分解
coeffs = pywt.wavedec(data, wavelet, level=level)
# 分别获取近似系数和细节系数
cA2, cD2, cD1 = coeffs
# 进行小波重构
data_rec = pywt.waverec(coeffs, wavelet)
# 打印结果
print("data: ", data)
print("coefficients: ", coeffs)
print("cA2: ", cA2)
print("cD2: ", cD2)
print("cD1: ", cD1)
print("reconstructed data: ", data_rec)
```
运行以上代码,将输出以下结果:
```
data: [1 2 3 4 5 6 7 8]
coefficients: [array([ 9.65685425, 3.65685425]), array([-0.70710678, -0.70710678, -0.70710678, -0.70710678]), array([-0.70710678, -0.70710678, -0.70710678, -0.70710678])]
cA2: [9.65685425, 3.6568542494923806]
cD2: [-0.70710678, -0.7071067811865476, -0.7071067811865476, -0.70710678]
cD1: [-0.70710678, -0.70710678, -0.70710678, -0.70710678]
reconstructed data: [1. 2. 3. 4. 5. 6. 7. 8.]
```
其中,`data` 是原始数据,`coefficients` 是小波分解后的系数,`cA2`、`cD2` 和 `cD1` 分别是近似系数和两层细节系数,`reconstructed data` 是小波重构后的数据。
阅读全文