用python编写去噪函数采用小波去噪
时间: 2024-02-28 14:29:37 浏览: 65
以下是一个简单的Python小波去噪函数:
```python
import pywt
def denoise(data):
# 选择小波函数
wavelet = 'db4'
# 分解层数
level = 1
# 小波分解
coeff = pywt.wavedec(data, wavelet, mode='per', level=level)
# 高频系数置零
coeff[1:] = (pywt.threshold(i, value=0.5, mode='soft') for i in coeff[1:])
# 信号重构
denoised_data = pywt.waverec(coeff, wavelet, mode='per')
return denoised_data
```
使用方法:
```python
# 生成测试数据
import numpy as np
data = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 1000)) + 0.5 * np.sin(2 * np.pi * 20 * np.linspace(0, 1, 1000))
# 去噪
denoised_data = denoise(data)
# 可视化
import matplotlib.pyplot as plt
plt.plot(data, label='Noisy signal')
plt.plot(denoised_data, label='Denoised signal')
plt.legend()
plt.show()
```
阅读全文