📄 060921bijiaodb5ceng.m
字号:
%-----------------比较小波函数db(1-8)的5层分解去噪效果程序-------------------
clc; clear all;
%----------从MIT-BIH库中读取心电异常第100组数据------------------------------
% This programm reads ECG data which are saved in format 212.
% (e.g., 100.dat from MIT-BIH-DB, cu01.dat from CU-DB,...)
% The data are displayed in a figure together with the
% annotations.(数据和说明在一起)
% The annotations are saved in the vector ANNOT, the corresponding
% times (in seconds) are saved in the vector ATRTIME.
% The annotations are saved as numbers, the meaning of the numbers can
% be found in the codetable "ecgcodes.h" available at www.physionet.org.
%
% ANNOT only contains the most important information, which is displayed
% with the program rdann (available on www.physionet.org) in the 3rd row.
% The 4th to 6th row are not saved in ANNOT.
%------ SPECIFY DATA ------------------------------------------------------
PATH= 'D:\matlab\matlab7.2\work'; % path, where data are saved
HEADERFILE= '100.hea'; % header-file in text format
%头文件.
ATRFILE= '100.atr'; % attributes-file in binary format
%说明文件.
DATAFILE='100.dat'; % data-file
%数据文件.
SAMPLES2READ=30000; % number of samples to be read
%样本数.
% in case of more than one signal:
% 2*SAMPLES2READ samples are read
%------ LOAD HEADER DATA --------------------------------------------------
fprintf(1,'\\n$> WORKING ON %s ...\n', HEADERFILE);
%将格式化的文件写入字符串.
%signalh= fullfile(PATH, HEADERFILE);
%signalh= fullfile(HEADERFILE);
fid1=fopen('100.hea','r');
%'100.hea为打开的文件,'r'为打开的方式(为只读)
z= fgetl(fid1);
%从文件中读行,并丢掉换行符.
A= sscanf(z, '%*s %d %d %d',[1,3]);
%从格式化的字符串中读取(数据).(%*s 忽略,%d10进制)
nosig= A(1); % number of signals(信号的数)
sfreq=A(2); % sample rate of data(数的样品率).
clear A;
for k=1:nosig
%for循环(1=<k=<nosig).
z= fgetl(fid1);
%从文件中读行,并丢掉换行符.
A= sscanf(z, '%*s %d %d %d %d %d',[1,5]);
%从格式化的字符串中读取.
dformat(k)= A(1); % format; here only 212 is allowed
%[212 212]
gain(k)= A(2); % number of integers per mV
%[200 200]
bitres(k)= A(3); % bitresolution
%[11 11]
zerovalue(k)= A(4); % integer value of ECG zero point
%[1024 1024]
firstvalue(k)= A(5); % first integer value of signal (to test for errors)
%[995 1011]
end;
%结束循环.
fclose(fid1);
%关掉文件fid1.
clear A;
%清除A.
%------ LOAD BINARY DATA --------------------------------------------------
if dformat~= [212,212], error('this script does not apply binary formats different to 212.'); end;
%如果dformat~=[212 212]提示错误.
signald= fullfile(PATH, DATAFILE); % data in format 212
%从部分文件中建立文件全名(或把DATAFILE文件放在PATH目录下).
fid2=fopen('100.dat','r');
%打开数据文件100.dat,'r'为只读.
A= fread(fid2, [3, SAMPLES2READ], 'uint8')'; % matrix with 3 rows, each 8 bits long, = 2*12bit
%读二进制文件fid2,生成3行,列为小于等于3000的矩阵.'uint8'无符号8字节型.(实际执行程序时生成为3000*3矩阵.)
fclose(fid2);
%关闭文件fid2.
M2H= bitshift(A(:,2), -4);
%把矩阵A第二列按位运算右移4位,即保留其高4位.
M1H= bitand(A(:,2), 15);
%把矩阵A第二列按位运算与15作与运算,即保留其低4位.
PRL=bitshift(bitand(A(:,2),8),9); % sign-bit
%先做与运算保留了第4为,然后移9位,移到了第13位.
PRR=bitshift(bitand(A(:,2),128),5); % sign-bit
%先做与运算保留了第8位,然后左移5位,移到了第13位.
M( : , 1)= bitshift(M1H,8)+ A(:,1)-PRL;
%把矩阵A第二列的低4位,左移8位,然后和其第1列求和,减去第13位(原第4位).
M( : , 2)= bitshift(M2H,8)+ A(:,3)-PRR;
%把矩阵A第二列的高4位,左移8位,然后和其第3列求和,减去第13位(原第8位).
if M(1,:) ~= firstvalue, error('inconsistency in the first bit values'); end;
switch nosig
case 2
M( : , 1)= (M( : , 1)- zerovalue(1))/gain(1);
M( : , 2)= (M( : , 2)- zerovalue(2))/gain(2);
TIME=(0:(SAMPLES2READ-1))/sfreq;
%时间为采样点数除以样品率。
case 1
M( : , 1)= (M( : , 1)- zerovalue(1));
M( : , 2)= (M( : , 2)- zerovalue(1));
M=M';
M(1)=[];
sM=size(M);
sM=sM(2)+1;
M(sM)=0;
M=M';
M=M/gain(1);
TIME=(0:2*(SAMPLES2READ)-1)/sfreq;
otherwise % this case did not appear up to now!
% here M has to be sorted!!!
disp('Sorting algorithm for more than 2 signals not programmed yet!');
end;
clear A M1H M2H PRR PRL;
%提取其中一个导联的数据进行去噪
%--------------------------小波分析心电信号---------------------------------
E=M(:,2);
E=E';
n=size(E);
s=E(1:2000);
%使用db小波对s进行3层分解
for jj=1:8
%jj表示小波的类型
switch jj
case 1
db='db1';
case 2
db='db2';
case 3
db='db3';
case 4
db='db4';
case 5
db='db5';
case 6
db='db6';
case 7
db='db7';
case 8
db='db8';
otherwise
end;
[C L]=wavedec(E,5,db);
% 从c中提取尺度3下的近似小波系数
cA3=detcoef(C,L,5);
%从信号c中提取尺度3,2,1下的细节小波系数
[cD1,cD2,cD3,cD4,cD5]=detcoef(C,L,[1,2,3,4,5]);
%重构尺度5下的近似小波系数
A5=wrcoef('a',C,L,db,5);
% 重构尺度3,2,1下的细节小波系数.
D1=wrcoef('d',C,L,db,1);
D2=wrcoef('d',C,L,db,2);
D3=wrcoef('d',C,L,db,3);
D4=wrcoef('d',C,L,db,4);
D5=wrcoef('d',C,L,db,5);
%-------------------使用阈值法去噪-----------------------------
%利用'ddencmp'得到除噪的默认参数
%thr,sorh,keepapp分别表示是阈值,软阈值(或硬阈值)和允许用户保存的低频系数.
%'den','wv',s分别为降噪('cmp'表示压缩),小波('wp'小波包),和降噪信号.
[thr,sorh,keepapp]=ddencmp('den','wv',E);
%去噪
clean=wdencmp('gbl',C,L,db,5,thr,sorh,keepapp);
%---------去噪效果衡量(SNR越大效果越好,MSE越小越好)--------------------------------
%选取信号的长度。
N=n(2);
x=E;
y=clean;
F=0;
M=0;
for ii=1:N
m(ii)=(x(ii)-y(ii))*(x(ii)-y(ii));
t(ii)=y(ii)*y(ii);
f(ii)=t(ii)/m(ii);
F=F+f(ii);
M=M+m(ii);
end;
SNR(jj)=10*log10(F);
MSE(jj)=M/N;
end;
%对比原始信号和除噪后的信号
subplot(2,1,1);
plot(s(1:1000));
title('原始信号');
subplot(2,1,2);
plot(clean(1:1000));
title('除噪后的信号');
SNR,MSE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -