python实现小波降噪_15转载python实现小波分解【实测成功】

仅作为操作记录,大佬请跳过。

感谢大佬博主,传送门

代码可直接运行

import numpy as np

import matplotlib.pyplot as plt

import pywt

import pywt.data

ecg = pywt.data.ecg()

data1 = np.concatenate((np.arange(1, 400),

np.arange(398, 600),

np.arange(601, 1024)))

x = np.linspace(0.082, 2.128, num=1024)[::-1]

data2 = np.sin(40 * np.log(x)) * np.sign((np.log(x)))

mode = pywt.Modes.smooth

def plot_signal_decomp(data, w, title):

"""Decompose and plot a signal S.

S = An + Dn + Dn-1 + ... + D1

"""

w = pywt.Wavelet(w)#选取小波函数

a = data

ca = []#近似分量

cd = []#细节分量

for i in range(5):

(a, d) = pywt.dwt(a, w, mode)#进行5阶离散小波变换

ca.append(a)

cd.append(d)

rec_a = []

rec_d = []

for i, coeff in enumerate(ca):

coeff_list = [coeff, None] + [None] * i

rec_a.append(pywt.waverec(coeff_list, w))#重构

for i, coeff in enumerate(cd):

coeff_list = [None, coeff] + [None] * i

if i ==3:

print(len(coeff))

print(len(coeff_list))

rec_d.append(pywt.waverec(coeff_list, w))

fig = plt.figure()

ax_main = fig.add_subplot(len(rec_a) + 1, 1, 1)

ax_main.set_title(title)

ax_main.plot(data)

ax_main.set_xlim(0, len(data) - 1)

for i, y in enumerate(rec_a):

ax = fig.add_subplot(len(rec_a) + 1, 2, 3 + i * 2)

ax.plot(y, 'r')

ax.set_xlim(0, len(y) - 1)

ax.set_ylabel("A%d" % (i + 1))

for i, y in enumerate(rec_d):

ax = fig.add_subplot(len(rec_d) + 1, 2, 4 + i * 2)

ax.plot(y, 'g')

ax.set_xlim(0, len(y) - 1)

ax.set_ylabel("D%d" % (i + 1))

#plot_signal_decomp(data1, 'coif5', "DWT: Signal irregularity")

#plot_signal_decomp(data2, 'sym5',

# "DWT: Frequency and phase change - Symmlets5")

plot_signal_decomp(ecg, 'sym5', "DWT: Ecg sample - Symmlets5")

plt.show()

展示:

标签:15,python,coeff,len,实测,pywt,rec,np,ax

来源: https://blog.csdn.net/weixin_41529093/article/details/111039950

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小波降噪是一种基于小波包分解的信号去噪方法,其原理是将信号分解为多个子带,对每个子带进行阈值处理,再将处理后的子带重构得到去噪后的信号。下面是python实现小波降噪的示例代码: ```python import pywt import numpy as np def wpd(x, wavelet='db4', level=4, threshold_type='soft', sigma=None): wp = pywt.WaveletPacket(data=x, wavelet=wavelet, mode='symmetric', maxlevel=level) nodes = wp.get_level(level, 'natural') for node in nodes: if node.is_leaf: # 对每个叶节点进行阈值处理 coeffs = node.data if sigma is None: # 如果未指定sigma,则使用默认阈值估计方法 threshold = pywt.threshold(coeffs, threshold_type) else: # 如果指定了sigma,则使用基于sigma的阈值估计方法 threshold = sigma * np.median(np.abs(coeffs)) / 0.6745 threshold = pywt.threshold(coeffs, threshold_type, threshold) node.data = threshold return wp.reconstruct(update=True) # 示例 import matplotlib.pyplot as plt # 生成带噪信号 t = np.linspace(0, 1, 1000) x = np.sin(2*np.pi*10*t) + np.sin(2*np.pi*20*t) + np.random.randn(len(t)) # 小波降噪 y = wpd(x, level=4, threshold_type='soft') # 绘图 plt.subplot(211) plt.plot(t, x) plt.title('Original signal') plt.subplot(212) plt.plot(t, y) plt.title('Denoised signal') plt.show() ``` 在上面的示例中,`wpd`函数实现小波降噪,其输入参数包括原始信号`x`、小波基、小波包分解的数、阈值处理方法和阈值估计参数。其中,阈值处理方法可以是硬阈值或软阈值,阈值估计参数可以是指定的sigma,也可以使用默认的阈值估计方法。 示例中使用`matplotlib`库绘制了原始信号和去噪后的信号的图像,可以看到去噪后的信号已经明显减少了噪声。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值