仅作为记录,大佬请跳过。
直接上代码
import numpy as np
import pywt
import matplotlib.pyplot as plt
data=np.loadtxt('L2.txt',dtype='int')
coeffs = pywt.wavedec(data, 'sym8', level=8) ;coeffs=np.array(coeffs)
ca1=coeffs[0]
cd1=coeffs[8]
cd2=coeffs[7]
cd3=coeffs[6]
cd4=coeffs[5]
cd5=coeffs[4]
cd6=coeffs[3]
cd7=coeffs[2]
cd8=coeffs[1]
Td1=3.7188
Td2=3.5317
Td3=0.1206
Td4=0.2542
Td5=0.6816
Td6=0.0099
Td7=1.9913
Td8=1.0525
sa1=np.zeros(len(ca1))
sd4=pywt.threshold(cd4,Td4,'soft')
sd5=pywt.threshold(cd5,Td5,'soft')
sd6=pywt.threshold(cd6,Td6,'soft')
sd7=pywt.threshold(cd7,Td7,'soft')
sd8=pywt.threshold(cd8,Td8,'soft')
sd1=np.zeros(len(cd1));sd2=np.zeros(len(cd2));sd3=np.zeros(len(cd3))
c2=[sa1,sd8,sd7,sd6,sd5,sd4,sd3,sd2,sd1]
s0=pywt.waverec(c2, 'sym8')
plt.figure();plt.subplot(211);plt.plot(data);plt.subplot(212);plt.plot(s0);plt.show()
print('sec3——pulse')
参考
传送门1
传送门2
相应的matlab实现
a = load(file); %打开文件夹
% plot(a);title('原信号');grid on;
[c,l]=wavedec(a,8,'sym8');
ca1=appcoef(c,l,'sym8',8);
cd1=detcoef(c,l,1); %%获取高频细节
%% (获取高频细节-折叠)
cd2=detcoef(c,l,2);
cd3=detcoef(c,l,3);
cd4=detcoef(c,l,4);
cd5=detcoef(c,l,5);
cd6=detcoef(c,l,6);
cd7=detcoef(c,l,7);
cd8=detcoef(c,l,8);
%% (求heursure阈值-折叠)
Td1=thselect(cd1,'heursure'); %%heursure阈值
Td2=thselect(cd2,'heursure');
Td3=thselect(cd3,'heursure');
Td4=thselect(cd4,'heursure');
Td5=thselect(cd5,'heursure');
Td6=thselect(cd6,'heursure');
Td7=thselect(cd7,'heursure');
Td8=thselect(cd8,'heursure');
%% (1~6层heursure阈值处理-7-8层置0)
sa1=zeros(1,length(ca1)); %%近似信号a8置零去除基线漂移
% sd1=wthresh(cd1,'s',Td1); %%1~6层heursure阈值处理
% sd2=wthresh(cd2,'s',Td2);
% sd3=wthresh(cd3,'s',Td3);
sd4=wthresh(cd4,'s',Td4);
sd5=wthresh(cd5,'s',Td5);
sd6=wthresh(cd6,'s',Td6);
sd7=wthresh(cd7,'s',Td7);
sd8=wthresh(cd8,'s',Td8);
%%7-8层置0;(0.75Hz以下)
sd1=zeros(1,length(cd1));sd2=zeros(1,length(cd2));sd3=zeros(1,length(cd3));
% sd4=zeros(1,length(cd4));
% sd5=zeros(1,length(cd5));sd6=zeros(1,length(cd6));
% sd7=zeros(1,length(cd7));
% sd8=zeros(1,length(cd8));
%%
c2=[sa1,sd8',sd7',sd6',sd5',sd4',sd3,sd2,sd1]';
s0=waverec(c2,l,'sym8');