Python 对一维信号做小波分解

import pywt
import scipy.io as scio
import numpy as np
from scipy.fft import fft
import matplotlib.pyplot as plt

def waveletdec(s,wname='db6',level=4, mode='symmetric'):
        N = len(s)
        w = pywt.Wavelet(wname)
        a = s
        ca = []
        cd = []
        for i in range(level):
            (a, d) = pywt.dwt(a, w, mode)  # 将a作为输入进行dwt分解
            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)[0:N])  # 进行重构
        for i, coeff in enumerate(cd):
            coeff_list = [None, coeff] + [None] * i
            rec_d.append(pywt.waverec(coeff_list, w)[0:N])  # 进行重构
        return ca,cd,rec_a,rec_d

def zuotu(data,name_,level=4):

    plt.rcParams['font.sans-serif'] = ['simhei']  # 添加中文字体为黑体
    plt.rcParams['axes.unicode_minus'] = False
    plt.figure()
    if level == 4:
        plt.subplot(411)
        plt.plot(data[0])
        plt.title('第1层')
        plt.subplot(412)
        plt.plot(data[1])
        plt.title('第2层')
        plt.subplot(413)
        plt.plot(data[2])
        plt.title('第3层')
        plt.subplot(414)
        plt.plot(data[3])
        plt.title('第4层')
        plt.savefig('./results/'+name_+'.jpg')
        plt.close()

dataFile = 'doubl.mat'
data = scio.loadmat(dataFile)

data_ = np.array(data['doubl'])
print(data_)
#
for i in range(0,data_.shape[0]):
    l1 = data_[i,:]
    ca,cd,rec_a,rec_d = waveletdec(l1,wname='db6',level=4, mode='symmetric')

    zuotu(cd,name_ = '第'+str(i+1)+'次'+'Detail Coefficients')
    zuotu(ca,name_ = '第'+str(i+1)+'次'+'Approximation Coefficients')
    zuotu(rec_d,name_ = '第'+str(i+1)+'次'+'Reconstructed Detail Coefficients')
    zuotu(rec_a,name_ = '第'+str(i+1)+'次'+'Reconstructed Approximation Coefficients')

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值