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')