Python实现小波变换去噪

python实现小波变换去噪 

# coding = gbk
# 使用小波分析进行阈值去噪声,使用pywt.threshold
import pywt
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import math

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, 小于等于阈值的值不变

#-*-coding:utf-8-*-
#get Data
ecg = pywt.data.ecg()  #生成心电信号
index = []
data = []
coffs = []

for i in range(len(ecg)-1):
    X = float(i)
    Y = float(ecg[i])
    index.append(X)
    data.append(Y)
#create wavelet object and define parameters
w = pywt.Wavelet('db8') #选用Daubechies8小波
maxlev = pywt.dwt_max_level(len(data),w.dec_len)
print("maximum level is" + str(maxlev))
threshold = 0.1  #Threshold for filtering
#Decompose into wavelet components,to the level selected:
coffs=pywt.wavedec(data,'db8',level=maxlev) #将信号进行小波分解
for i in range(1, len(coffs)):
    coffs[i] = pywt.threshold(coffs[i], threshold*max(coffs[i]))
datarec = pywt.waverec(coffs, 'db8') #将信号进行小波重构
mintime = 0
maxtime = mintime + len(data)
print(mintime, maxtime)
plt.figure()
plt.subplot(3,1,1)
plt.plot(index[mintime:maxtime], data[mintime:maxtime])
plt.xlabel('time (s)')
plt.ylabel('microvolts (uV)')
plt.title("Raw signal")
plt.subplot(3, 1, 2)
plt.plot(index[mintime:maxtime], datarec[mintime:maxtime])
plt.xlabel('time (s)')
plt.ylabel('microvolts (uV)')
plt.title("De-noised signal using wavelet techniques")
plt.subplot(3, 1, 3)
plt.plot(index[mintime:maxtime],data[mintime:maxtime]-datarec[mintime:maxtime])
plt.xlabel('time (s)')
plt.ylabel('error (uV)')
plt.tight_layout()
plt.show()

运行结果如下: 

 

  • 11
    点赞
  • 100
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
小波变换是一种信号处理技术,可以将信号分解成不同频率的子信号,并且可以根据需要选择保留或丢弃某些子信号,从而实现信号去噪的目的。在Python中,可以使用PyWavelets库来实现小波变换去噪。 以下是一个简单的小波变换去噪Python实现示例: ```python import pywt import numpy as np # 定义小波变换去噪函数 def denoise_signal(signal): # 选择小波函数和分解层数 wavelet = 'db4' level = 3 # 执行小波变换 coeffs = pywt.wavedec(signal, wavelet, level=level) # 根据阈值选择保留或丢弃系数 threshold = np.std(coeffs[-level]) coeffs[1:] = (pywt.threshold(c, threshold) for c in coeffs[1:]) # 执行小波重构 denoised_signal = pywt.waverec(coeffs, wavelet) return denoised_signal # 示例使用 # 假设有一个包含噪声的信号 signal = np.random.randn(1000) + 5*np.sin(50*np.pi*np.arange(1000)/1000) # 对信号进行去噪 denoised_signal = denoise_signal(signal) # 打印去噪后的信号 print(denoised_signal) ``` 这个示例中,我们首先导入了`pywt`库,然后定义了一个`denoise_signal`函数来执行小波变换去噪。在函数中,我们选择了小波函数为`db4`,分解层数为3。然后,我们使用`pywt.wavedec`函数对信号进行小波变换,并根据阈值选择保留或丢弃系数。最后,我们使用`pywt.waverec`函数执行小波重构,得到去噪后的信号。 请注意,这只是一个简单的示例,实际应用中可能需要根据具体情况进行参数调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值