简单的基于信号处理的心电信号ECG特征波分割方法(MATLAB)

正常的心电图中,每个心跳周期内包含三个主要的特征波:P波、QRS波和T波,如下图所示。心电特征波能够反映心脏的生理状态信息,通过对其形状、幅值和持续时间的分析,可以用来辅助诊断心血管疾病。对于常见的ECG异常,如心律失常、房颤等,诊断依赖专家和临床医生对ECG进行目视检查。然而,随着心电数据规模不断扩大,分析大量的数据非常耗时,且受到专业知识的限制,及心电专家个人主观判断和经验的影响。

鉴于此,提出一种基于信号处理的心电信号ECG特征波分割方法,运行环境为MATLAB,分割算法的代码如下:

function [ECG_Struct] = ECG_Segmentation(signal,Fs,ECG_distance_threshold_sensivity,ECG_peak_sensivity,Plot_on)
ECG_Struct =struct;

if(nargin<1)
    ECG_Struct=[];
    return;
elseif(nargin<2)
    Fs=1000;
elseif (nargin<3)
    ECG_distance_threshold_sensivity=5;
    ECG_peak_sensivity=35;
    Plot_on=1;
elseif (nargin<4)
    ECG_peak_sensivity=35;
    Plot_on=1;
elseif (nargin<5)
    Plot_on=1;
end
ECG_peak_threshold=round(Fs/100);
ECG_data=signal;
data_len=length(ECG_data);
format long
BL=[1 zeros(1,5) -2 zeros(1,5) 1];      
AL=[32,-64,32];
BH=[-1 zeros(1,15) 32 -32 zeros(1,14) 1];  
AH=[32 -32];
AINT=[8];
BINT=[2 1 0 -1 -2 ]; 
BMOV=ones(1,30)./30; 
AMOV=[1];
min_distance=(Fs/2)-round(Fs/6);
[preB,preA]=butter(4,[2/Fs 60/Fs]);
y=filtfilt(preB,preA,ECG_data);
yL=filter(BL,AL,y);
yH=filter(BH,AH,yL);
yder=filter(BINT,AINT,yH);
ysqu=yder.^2;
yaov=filter(BMOV,AMOV,ysqu);
[pks,locs]=findpeaks(yaov,'MinPeakDistance',Fs);
ECG_range=median(pks)+median(pks)/ECG_distance_threshold_sensivity;
if(max(pks>=ECG_range))
    pks= pks(~(pks>=ECG_range));
    locs=locs(~(pks>=ECG_range));
end
Threshold=max(pks)*ECG_peak_sensivity/100;
clear pks locs
[pks,locs]=findpeaks(yaov,'MinPeakHeight',Threshold,'MinPeakDistance',min_distance);
new_locs=zeros([size(locs)]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
grp_delay = 23;  %  group delay of Pan-Tompkins filters.
if(locs(1,1)>round(Fs/grp_delay))
    new_locs=locs-round(Fs/grp_delay);
else
    % new_locs=[locs(1,1) locs(2:end)-round(Fs/23)];
    new_locs=locs(2:end)-round(Fs/grp_delay);
    pks=pks(2:end);
end
%%%%%%%%%R peak correction%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=2:length(new_locs)
    [d,r]=max(abs(ECG_data(new_locs(i)-ECG_peak_threshold : new_locs(i)+ECG_peak_threshold)));
    new_locs(i)=new_locs(i)-ECG_peak_threshold+r-1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
P_waves=[];
P_wave_locs=[];
T_waves=[];
T_wave_locs=[];
Q_waves=[];
Q_wave_locs=[];
S_waves=[];
S_wave_locs=[];
QT_intervals_locs=[];
PR_intervals_locs=[];
QRS_complex_locs=[];

for i=1:length(new_locs)-1
    isoelectric_line(i)=mean(ECG_data(new_locs(i):new_locs(i+1)));
end

for i=1:length(new_locs)-2
    duration_coef=round(abs(new_locs(i+1)-new_locs(i)));
    if(duration_coef<=Fs)
        Q_T_duration=duration_coef/2;
        P_R_duration=duration_coef/4;
        QRS_mid_duration=0.06*duration_coef;
    else
        Q_T_duration=Fs/2;
        P_R_duration=Fs/4;
        QRS_mid_duration=0.06*Fs;
    end
    x=ECG_data(round(new_locs(i+1)-P_R_duration:new_locs(i+1)-QRS_mid_duration));
    y=ECG_data(round(new_locs(i+1)+QRS_mid_duration:new_locs(i+1)+Q_T_duration));
    z=ECG_data(round(new_locs(i+1)-QRS_mid_duration:new_locs(i+1)));
    h=ECG_data(round(new_locs(i+1):new_locs(i+1)+QRS_mid_duration));
    P_waves=[P_waves max(x)];
    k=find(ECG_data==max(x));
    P_wave_locs=[P_wave_locs k(1,1)];
    T_waves=[T_waves max(y)];
    k=find(ECG_data==max(y));
    T_wave_locs=[T_wave_locs k(1,1)];
    Q_waves=[Q_waves min(z)];
    k=find(ECG_data==min(z));
    Q_wave_locs=[Q_wave_locs k(1,1)];
    S_waves=[S_waves min(h)];
    k=find(ECG_data==min(h));
    S_wave_locs=[S_wave_locs k(1,1)];
end

Q_isolation=[];
for(i=1:length(Q_wave_locs))
    cnt=0;
    for j=1:duration_coef
        
        if(Q_wave_locs(i)-j < 1)
            break;
        end

        if(ECG_data(Q_wave_locs(i))<=isoelectric_line(i))
            if(ECG_data(Q_wave_locs(i)-j)>=isoelectric_line(i))
                Q_isolation=[Q_isolation Q_wave_locs(i)-j];
                cnt=1;
                break;
            end
        else
            if(ECG_data(Q_wave_locs(i)-j)<=isoelectric_line(i))
                Q_isolation=[Q_isolation Q_wave_locs(i)-j];
                cnt=1;
                break;
            end
        end
    end
    if(cnt==0)
    Q_isolation=[Q_isolation Q_wave_locs(i)-j];
    end
end

S_isolation=[];
for(i=1:length(S_wave_locs))
    cnt=0;
    for j=1:duration_coef
        
        if(S_wave_locs(i)+j > data_len)
            break;
        end
        
        if(ECG_data(S_wave_locs(i))<=isoelectric_line(i))
            if(ECG_data(S_wave_locs(i)+j)>=isoelectric_line(i))
                S_isolation=[S_isolation S_wave_locs(i)+j];
                cnt=1;
                break;
            end
        else
            if(ECG_data(S_wave_locs(i)+j)<=isoelectric_line(i))
                S_isolation=[S_isolation S_wave_locs(i)+j];
                cnt=1;
                break;
            end
        end
    end
    if(cnt==0)
    S_isolation=[S_isolation Q_wave_locs(i)-j];    
    end
end

P_isolation_1=[];
for(i=1:length(P_wave_locs))
    cnt=0;
    for j=1:duration_coef
        
        if(P_wave_locs(i)-j < 1)
            break;
        end
     
        if(ECG_data(P_wave_locs(i))<=isoelectric_line(i))
            if(ECG_data(P_wave_locs(i)-j)>=isoelectric_line(i))
                P_isolation_1=[P_isolation_1 P_wave_locs(i)-j];
                cnt=1;
                break;
            end
        else
            if(ECG_data(P_wave_locs(i)-j)<=isoelectric_line(i))
                P_isolation_1=[P_isolation_1 P_wave_locs(i)-j];
                cnt=1;
                break;
            end
        end
    end
    if(cnt==0)
    P_isolation_1=[P_isolation_1 P_wave_locs(i)-j];
    end
end


P_isolation_2=[];
for(i=1:length(P_wave_locs))
    cnt=0;
    for j=1:duration_coef
        
        if(P_wave_locs(i)+j > data_len)
            break;
        end
        
        if(ECG_data(P_wave_locs(i))<=isoelectric_line(i))
            if(ECG_data(P_wave_locs(i)+j)>=isoelectric_line(i))
                P_isolation_2=[P_isolation_2 P_wave_locs(i)+j];
                cnt=1;
                break;
            end
        else
            if(ECG_data(P_wave_locs(i)+j)<=isoelectric_line(i))
                P_isolation_2=[P_isolation_2 P_wave_locs(i)+j];
                cnt=1;
                break;
            end
        end
    end
    if(cnt==0)
    P_isolation_2=[P_isolation_2 P_wave_locs(i)+j];    
    end
end

T_isolation_1=[];
for(i=1:length(T_wave_locs))
    cnt=0;
    for j=1:duration_coef
        
        if(T_wave_locs(i)-j < 1)
            break;
        end
        
        if(ECG_data(T_wave_locs(i))<=isoelectric_line(i))
            if(ECG_data(T_wave_locs(i)-j)>=isoelectric_line(i))
                T_isolation_1=[T_isolation_1 T_wave_locs(i)-j];
                cnt=1;
                break;
            end
        else
            if(ECG_data(T_wave_locs(i)-j)<=isoelectric_line(i))
                T_isolation_1=[T_isolation_1 T_wave_locs(i)-j];
                cnt=1;
                break;
            end
        end
    end
    if(cnt==0)
    T_isolation_1=[T_isolation_1 T_wave_locs(i)-j];    
    end
end

T_isolation_2=[];
for(i=1:length(T_wave_locs))
    cnt=0;
    for j=1:duration_coef
        
        if(T_wave_locs(i)+j >data_len)
            break;
        end
        
        if(ECG_data(T_wave_locs(i))<=isoelectric_line(i))
            
            if(ECG_data(T_wave_locs(i)+j)>=isoelectric_line(i))
                T_isolation_2=[T_isolation_2 T_wave_locs(i)+j];
                cnt=1;
                break;
            end
        else
            if(ECG_data(T_wave_locs(i)+j)<=isoelectric_line(i))
                T_isolation_2=[T_isolation_2 T_wave_locs(i)+j];
                cnt=1;
                break;
            end
        end
    end
    if(cnt==0)
    T_isolation_2=[T_isolation_2 T_wave_locs(i)+j];    
    end
end
clear x y k z h
if (Plot_on >=1)
    fig=figure('WindowState','maximized');
    t=1/Fs:1/Fs:length(ECG_data)/Fs;
    plot(t,ECG_data);
    hold on
    txt = 'P';
    plot(P_wave_locs/Fs,P_waves,'s');
    text(P_wave_locs/Fs,P_waves,txt,'FontSize',14)
    txt = 'T';
    plot(T_wave_locs/Fs,T_waves,'o')
    text(T_wave_locs/Fs,T_waves,txt,'FontSize',14)
    txt = 'Q';
    plot(Q_wave_locs/Fs,Q_waves,'*')
    text(Q_wave_locs/Fs,Q_waves,txt,'FontSize',14)
    txt = 'S';
    plot(S_wave_locs/Fs,S_waves,'+')
    text(S_wave_locs/Fs,S_waves,txt,'FontSize',14)
    txt = 'R';
    plot(new_locs/Fs,ECG_data(new_locs),'x');
    text(new_locs/Fs,ECG_data(new_locs),txt,'FontSize',14)
    
    %%%%%%%%%%%%P wave%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:length(P_isolation_1)
        plot((P_isolation_1(i):P_isolation_2(i))/Fs,ECG_data(P_isolation_1(i):P_isolation_2(i)),'g');
    end
    %%%%%%%%%%%%QRS COMPLEX%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:length(Q_isolation)
        plot((Q_isolation(i):S_isolation(i))/Fs,ECG_data(Q_isolation(i):S_isolation(i)),'r');
    end
    QRS_Complex= [Q_isolation;S_isolation];
    %%%%%%%%%%%%T wave%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:length(T_isolation_1)
        plot((T_isolation_1(i):T_isolation_2(i))/Fs,ECG_data(T_isolation_1(i):T_isolation_2(i)),'y');
    end
    line_threshold=mean(ECG_data(new_locs));
    %%%%%%%%%%%%QT interval%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:length(Q_isolation)
        dump=pks(i)+ones(1,length(Q_isolation(i):T_isolation_2(i))-2)*line_threshold;
        plot((Q_isolation(i):T_isolation_2(i))/Fs,[line_threshold dump line_threshold],'-r','LineWidth',1.2);
    end
    line_threshold=mean(ECG_data(new_locs));
    %%%%%%%%%%%%PR interval%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:length(Q_isolation)
        %dump=[dump Q_isolation(i):T_isolation(i)];
        dump=pks(i)+ones(1,length(P_isolation_1(i):Q_isolation(i))-2)*line_threshold;
        plot((P_isolation_1(i):Q_isolation(i))/Fs,[line_threshold dump line_threshold],'-g','LineWidth',1.2);
    end
    line_threshold=mean(ECG_data(new_locs))+mean(ECG_data(new_locs))*0.10;
    %%%%%%%%%%%%PR Segment%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:length(Q_isolation)
        %dump=[dump Q_isolation(i):T_isolation(i)];
        dump=pks(i)+ones(1,length(P_isolation_2(i):Q_isolation(i))-2)*line_threshold;
        if ~isempty(dump)
            plot((P_isolation_2(i):Q_isolation(i))/Fs,[line_threshold dump line_threshold],'-b','LineWidth',1.2);
        end
    end
    line_threshold=mean(ECG_data(new_locs))+mean(ECG_data(new_locs))*0.10;
    %%%%%%%%%%%%ST segment interval%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for i=1:length(Q_isolation)
        dump=pks(i)+ones(1,length(S_isolation(i):T_isolation_1(i))-2)*line_threshold;
        if ~isempty(dump)
            plot((S_isolation(i):T_isolation_1(i))/Fs,[line_threshold dump line_threshold],'-k','LineWidth',1.2);
        end
    end
xlabel('Time in Seconds','fontsize',24)
ylabel('12bit Raw ECG','fontsize',24);
title('ECG Segmentation','fontsize',24);
set(gca,'Fontsize',16)
%saveas(fig,'son.png');
%xlim([5,10]);
%saveas(fig,'son2.png');
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%NEW METHOD OF FICIDUAL POINTS     
%(Her EKG i莽in denenmedi ama 莽o冒unda 莽al媒镁媒yor)

R_wave_locs=new_locs(2:length(P_waves)+1)';
R_waves=ECG_data(new_locs(2:length(P_waves)+1))';



if(T_isolation_2(end)>length(ECG_data))
T_isolation_2(end)= length(ECG_data);           %脟ok nadir durumlarda T_isolation d媒镁ar媒da kal媒yor. Hatay媒 engellemek i莽in yap媒ld媒.
end

dump=[R_waves,S_waves,Q_waves,P_waves,T_waves,ECG_data(T_isolation_1)',ECG_data(T_isolation_2)',ECG_data(P_isolation_1)',ECG_data(P_isolation_2)'];
max_point=max(dump);
min_point=min(dump);

r=max_point-min_point;

R_waves_normalized=((R_waves-min_point)/r)';
Q_waves_normalized=((Q_waves-min_point)/r)';
S_waves_normalized=((S_waves-min_point)/r)';
P_waves_normalized=((P_waves-min_point)/r)';
T_waves_normalized=((T_waves-min_point)/r)';
V5_points=((((ECG_data(P_isolation_1)'+ECG_data(P_isolation_2)')/2)-min_point)/r)';
V6_points=((((ECG_data(T_isolation_1)'+ECG_data(T_isolation_2)')/2)-min_point)/r)';

clear dump

A1=[];
for i=1:length(P_isolation_1)
x2=P_isolation_1(i)/Fs;y2=((ECG_data(P_isolation_1(i))-min_point)/r)';
x1=P_wave_locs(i)/Fs;y1=P_waves_normalized(i);
x3=Q_wave_locs(i)/Fs;y3=Q_waves_normalized(i);
%x2=P_isolation_1(i);y2=ECG_data(P_isolation_1(i));
%x1=P_wave_locs(i);y1=P_waves(i);
%x3=Q_wave_locs(i);y3=Q_waves(i);
A1(i)=Angle_of_2lines(x1,x2,x3,y1,y2,y3);
end

A2=[];
for i=1:length(P_isolation_1)
x2=S_wave_locs(i)/Fs;y2=S_waves_normalized(i);
x1=T_wave_locs(i)/Fs;y1=T_waves_normalized(i);
x3=T_isolation_2(i)/Fs;y3=((ECG_data(T_isolation_2(i))-min_point)/r)';
%x2=S_wave_locs(i);y2=S_waves(i);
%x1=T_wave_locs(i);y1=T_waves(i);
%x3=T_isolation_2(i);y3=ECG_data(T_isolation_2(i));
A2(i)=Angle_of_2lines(x1,x2,x3,y1,y2,y3);
end

A3=[];
for i=1:length(P_isolation_1)
x2=P_wave_locs(i)/Fs;y2=P_waves_normalized(i);
y1=Q_waves_normalized(i);x1=Q_wave_locs(i)/Fs;
x3=R_wave_locs(i)/Fs;y3=R_waves_normalized(i);
%x2=P_wave_locs(i);y2=P_waves(i);
%y1=Q_waves(i);x1=Q_wave_locs(i);
%x3=R_wave_locs(i);y3=R_waves(i);
A3(i)=Angle_of_2lines(x1,x2,x3,y1,y2,y3);
end

A4=[];
for i=1:length(P_isolation_1)
x2=R_wave_locs(i)/Fs;y2=R_waves_normalized(i);
y1=S_waves_normalized(i);x1=S_wave_locs(i)/Fs;
x3=T_wave_locs(i)/Fs;y3=T_waves_normalized(i);
%x2=R_wave_locs(i);y2=R_waves(i);
%y1=S_waves(i);x1=S_wave_locs(i);
%x3=T_wave_locs(i);y3=T_waves(i);
A4(i)=Angle_of_2lines(x1,x2,x3,y1,y2,y3);
end

A5=[];
for i=1:length(P_isolation_1)
x2=S_wave_locs(i)/Fs;y2=S_waves_normalized(i);
y1=R_waves_normalized(i);x1=R_wave_locs(i)/Fs;
x3=Q_wave_locs(i)/Fs;y3=Q_waves_normalized(i);
%x2=S_wave_locs(i);y2=S_waves(i);
%y1=R_waves(i);x1=R_wave_locs(i);
%x3=Q_wave_locs(i);y3=Q_waves(i);
A5(i)=Angle_of_2lines(x1,x2,x3,y1,y2,y3);
end


%T1->
T1=(P_isolation_2-P_isolation_1);
T1_scaled=ECG_Temp_Normalize(T1,R_wave_locs);
T1_scaled=T1_scaled/Fs;

%T2->
T2=(T_isolation_2-T_isolation_1);
T2_scaled=ECG_Temp_Normalize(T2,R_wave_locs);
T2_scaled=T2_scaled/Fs;

%T3->
T3=R_wave_locs-Q_wave_locs;
T3_scaled=ECG_Temp_Normalize(T3,R_wave_locs);
T3_scaled=T3_scaled/Fs;

%T4->
T4=S_wave_locs-R_wave_locs;
T4_scaled=ECG_Temp_Normalize(T4,R_wave_locs);
T4_scaled=T4_scaled/Fs;

%T5->
T5=Q_wave_locs-P_wave_locs;
T5_scaled=ECG_Temp_Normalize(T5,R_wave_locs);
T5_scaled=T5_scaled/Fs;

%T6->
T6=T_wave_locs-S_wave_locs;
T6_scaled=ECG_Temp_Normalize(T6,R_wave_locs);
T6_scaled=T6_scaled/Fs;

%T7->
T7=Q_wave_locs-P_isolation_1;
T7_scaled=ECG_Temp_Normalize(T7,R_wave_locs);
T7_scaled=T7_scaled/Fs;

%T8->
T8=T_isolation_2-S_wave_locs;
T8_scaled=ECG_Temp_Normalize(T8,R_wave_locs);
T8_scaled=T8_scaled/Fs;

%T9->
T9=R_wave_locs-P_isolation_2;
T9_scaled=ECG_Temp_Normalize(T9,R_wave_locs);
T9_scaled=T9_scaled/Fs;

%T10->
T10=T_isolation_1-R_wave_locs;
T10_scaled=ECG_Temp_Normalize(T10,R_wave_locs);
T10_scaled=T10_scaled/Fs;

%T11->
T11=R_wave_locs-P_wave_locs;
T11_scaled=ECG_Temp_Normalize(T11,R_wave_locs);
T11_scaled=T11_scaled/Fs;

%T12->
T12=T_wave_locs-R_wave_locs;
T12_scaled=ECG_Temp_Normalize(T12,R_wave_locs);
T12_scaled=T12_scaled/Fs;

%T13->
T13=R_wave_locs-P_isolation_1;
T13_scaled=ECG_Temp_Normalize(T13,R_wave_locs);
T13_scaled=T13_scaled/Fs;

%T14->
T14=T_isolation_2-R_wave_locs;
T14_scaled=ECG_Temp_Normalize(T14,R_wave_locs);
T14_scaled=T14_scaled/Fs;

%T15->
T15=T_wave_locs-P_wave_locs;
T15_scaled=ECG_Temp_Normalize(T15,R_wave_locs);
T15_scaled=T15_scaled/Fs;

%V1->
V1=R_waves-Q_waves;
V1_normalized=R_waves_normalized-Q_waves_normalized;
%V2->
V2=R_waves-S_waves;
V2_normalized=R_waves_normalized-S_waves_normalized;
%V3->
V3=P_waves-Q_waves;
V3_normalized=P_waves_normalized-Q_waves_normalized;
%V4->
V4=T_waves-S_waves;
V4_normalized=T_waves_normalized-S_waves_normalized;
%V5->
V5=P_waves-(ECG_data(P_isolation_1)'+ECG_data(P_isolation_2)')/2;
V5_normalized=P_waves_normalized-V5_points;

%V6->
V6=T_waves-(ECG_data(T_isolation_1)'+ECG_data(T_isolation_2)')/2;
V6_normalized=T_waves_normalized-V6_points;
%V7->
V7=Q_waves-S_waves;
V7_normalized=Q_waves_normalized-S_waves_normalized;


ECG_Struct.Ficidual_Points=[P_waves_normalized,Q_waves_normalized,R_waves_normalized,S_waves_normalized,T_waves_normalized,((ECG_data(P_isolation_1)'-min_point)/r)',((ECG_data(P_isolation_2)'-min_point)/r)',((ECG_data(T_isolation_1)'-min_point)/r)',((ECG_data(T_isolation_2)'-min_point)/r)'];
ECG_Struct.Temporal=[T1_scaled',T2_scaled',T3_scaled',T4_scaled',T5_scaled',T6_scaled',T7_scaled',T8_scaled',T9_scaled',T10_scaled',T11_scaled',T12_scaled',T13_scaled',T14_scaled',T15_scaled'];
ECG_Struct.Amplitudes=[V1_normalized,V2_normalized,V3_normalized,V4_normalized,V5_normalized,V6_normalized,V7_normalized];
ECG_Struct.Angles=[A1',A2',A3',A4',A5'];

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


ECG_Struct.ECG_data=ECG_data;
ECG_Struct.P_points=P_waves;
ECG_Struct.P_locs=P_wave_locs;
ECG_Struct.T_points=T_waves;
ECG_Struct.T_locs=T_wave_locs;
ECG_Struct.Q_points=Q_waves;
ECG_Struct.Q_locs=Q_wave_locs;
ECG_Struct.S_points=S_waves;
ECG_Struct.S_locs=S_wave_locs;
ECG_Struct.R_points=ECG_data(new_locs)';
ECG_Struct.R_locs=new_locs';
ECG_Struct.P_wave_interval=[P_isolation_1;P_isolation_2];
ECG_Struct.QRS_Complex_interval=[Q_isolation;S_isolation];
ECG_Struct.T_wave_interval=[T_isolation_1;T_isolation_2];
ECG_Struct.QT_interval=[Q_isolation;T_isolation_2];
ECG_Struct.PR_interval=[P_isolation_1;P_isolation_2];
ECG_Struct.PR_segment=[P_isolation_2;Q_isolation];
ECG_Struct.ST_segment=[S_isolation;T_isolation_1]; 

% a=length(P_isolation_1);
% b=length(P_isolation_2);
% if(a>b)
% ECG_Struct.P_wave_interval=[P_isolation_1(1:b);P_isolation_2(1:b)];
% else
% ECG_Struct.P_wave_interval=[P_isolation_1(1:a);P_isolation_2(1:a)];    
% end

% a=length(Q_isolation);
% b=length(S_isolation);
% if(a>b)
% ECG_Struct.QRS_Complex_interval=[Q_isolation(1:b);S_isolation(1:b)];
% else
% ECG_Struct.QRS_Complex_interval=[Q_isolation(1:a);S_isolation(1:a)];    
% end

% a=length(T_isolation_1);
% b=length(T_isolation_2);
% if(a>b)
% ECG_Struct.T_wave_interval=[T_isolation_1(1:b);T_isolation_2(1:b)];
% else
% ECG_Struct.T_wave_interval=[T_isolation_1(1:a);T_isolation_2(1:a)];
% end

% a=length(Q_isolation);
% b=length(T_isolation_2);
% if(a>b)
% ECG_Struct.QT_interval=[Q_isolation(1:b);T_isolation_2(1:b)];
% else
% ECG_Struct.QT_interval=[Q_isolation(1:a);T_isolation_2(1:a)];    
% end

% a=length(P_isolation_1);
% b=length(P_isolation_2);
% if(a>b)
% ECG_Struct.PR_interval=[P_isolation_1(1:b);P_isolation_2(1:b)];
% else
% ECG_Struct.PR_interval=[P_isolation_1(1:a);P_isolation_2(1:a)];
% end

% a=length(P_isolation_2);
% b=length(Q_isolation);
% if(a>b)
% ECG_Struct.PR_segment=[P_isolation_2(1:b);Q_isolation(1:b)];
% else
% ECG_Struct.PR_segment=[P_isolation_2(1:a);Q_isolation(1:a)];
% end

% a=length(S_isolation);
% b=length(T_isolation_1);
% if(a>b)
% ECG_Struct.ST_segment=[S_isolation(1:b);T_isolation_1(1:b)];
% else
% ECG_Struct.ST_segment=[S_isolation(1:a);T_isolation_1(1:a)];    
% end


%%%%%%%%%%%%%%%%%QRS Complex interval Align%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(ECG_Struct.R_locs(1,1)<ECG_Struct.QRS_Complex_interval(1,1))
    threshold1=0;       %sa冒a kayd媒rma oran媒
    threshold2=0;       %sola kayd媒rma oran媒
    for i=1:length(ECG_Struct.QRS_Complex_interval)-1
        threshold1=threshold1+floor(ECG_Struct.R_locs(i+1)-ECG_Struct.QRS_Complex_interval(1,i));
        threshold2=threshold2+floor(ECG_Struct.QRS_Complex_interval(2,i)-ECG_Struct.R_locs(i+1));
    end
    threshold1=floor(threshold1/length(ECG_Struct.QRS_Complex_interval));
    threshold2=floor(threshold2/length(ECG_Struct.QRS_Complex_interval));
    for i=1:length(ECG_Struct.QRS_Complex_interval)
        ECG_Struct.QRS_Complex_interval_align(1,i)=ECG_Struct.R_locs(i+1)-threshold1;
        ECG_Struct.QRS_Complex_interval_align(2,i)=ECG_Struct.R_locs(i+1)+threshold2;
    end
else
    for i=1:length(ECG_Struct.QT_interval)
        threshold1=threshold1+floor(ECG_Struct.R_locs(i)-ECG_Struct.QRS_Complex_interval(1,i));
        threshold2=threshold2+floor(ECG_Struct.QRS_Complex_interval(2,i)-ECG_Struct.R_locs(i));
    end
    threshold1=floor(threshold1/length(ECG_Struct.QRS_Complex_interval));
    threshold2=floor(threshold2/length(ECG_Struct.QRS_Complex_interval));
    for i=1:length(ECG_Struct.QRS_Complex_interval)
        ECG_Struct.QRS_Complex_interval_align(1,i)=ECG_Struct.R_locs(i)-threshold1;
        ECG_Struct.QRS_Complex_interval_align(2,i)=ECG_Struct.R_locs(i)+threshold2;
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%P_wave_interval align%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
threshold1=0;
threshold2=0;
for i=1:length(ECG_Struct.P_wave_interval)
    threshold1=threshold1+floor(ECG_Struct.P_locs(i)-ECG_Struct.P_wave_interval(1,i));
    threshold2=threshold2+floor(ECG_Struct.P_wave_interval(2,i)-ECG_Struct.P_locs(i));
end
threshold1=floor(threshold1/length(ECG_Struct.P_wave_interval));
threshold2=floor(threshold2/length(ECG_Struct.P_wave_interval));
for i=1:length(ECG_Struct.P_wave_interval)
    ECG_Struct.P_wave_interval_align(1,i)=ECG_Struct.P_locs(i)-threshold1;
    ECG_Struct.P_wave_interval_align(2,i)=ECG_Struct.P_locs(i)+threshold2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%T wave interval align%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
threshold1=0;
threshold2=0;
for i=1:length(ECG_Struct.T_wave_interval)
    threshold1=threshold1+floor(ECG_Struct.T_locs(i)-ECG_Struct.T_wave_interval(1,i));
    threshold2=threshold2+floor(ECG_Struct.T_wave_interval(2,i)-ECG_Struct.T_locs(i));
end
threshold1=floor(threshold1/length(ECG_Struct.T_wave_interval));
threshold2=floor(threshold2/length(ECG_Struct.T_wave_interval));
for i=1:length(ECG_Struct.T_wave_interval)
    ECG_Struct.T_wave_interval_align(1,i)=ECG_Struct.T_locs(i)-threshold1;
    ECG_Struct.T_wave_interval_align(2,i)=ECG_Struct.T_locs(i)+threshold2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%QT_interval align%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if(ECG_Struct.R_locs(1,1)<ECG_Struct.QT_interval(1,1))
    threshold1=0;
    threshold2=0;
    for i=1:length(ECG_Struct.QT_interval)-1
        threshold1=threshold1+floor(ECG_Struct.R_locs(i+1)-ECG_Struct.QT_interval(1,i));
        threshold2=threshold2+floor(ECG_Struct.QT_interval(2,i)-ECG_Struct.R_locs(i+1));
    end
    threshold1=floor(threshold1/length(ECG_Struct.QT_interval));
    threshold2=floor(threshold2/length(ECG_Struct.QT_interval));
    for i=1:length(ECG_Struct.QT_interval)
        ECG_Struct.QT_interval_align(1,i)=ECG_Struct.R_locs(i+1)-threshold1;
        ECG_Struct.QT_interval_align(2,i)=ECG_Struct.R_locs(i+1)+threshold2;
    end
else
    for i=1:length(ECG_Struct.QT_interval)
        threshold1=threshold1+floor(ECG_Struct.R_locs(i)-ECG_Struct.QT_interval(1,i));
        threshold2=threshold2+floor(ECG_Struct.QT_interval(2,i)-ECG_Struct.R_locs(i));
    end
    threshold1=floor(threshold1/length(ECG_Struct.QT_interval));
    threshold2=floor(threshold2/length(ECG_Struct.QT_interval));
    for i=1:length(ECG_Struct.QT_interval)
        ECG_Struct.QT_interval_align(1,i)=ECG_Struct.R_locs(i)-threshold1;
        ECG_Struct.QT_interval_align(2,i)=ECG_Struct.R_locs(i)+threshold2;
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%PR_interval align%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
threshold1=0;
threshold2=0;
for i=1:length(ECG_Struct.PR_interval)
    threshold1=threshold1+floor(ECG_Struct.P_locs(i)-ECG_Struct.PR_interval(1,i));
    threshold2=threshold2+floor(ECG_Struct.PR_interval(2,i)-ECG_Struct.P_locs(i));
end
threshold1=floor(threshold1/length(ECG_Struct.PR_interval));
threshold2=floor(threshold2/length(ECG_Struct.PR_interval));
for i=1:length(ECG_Struct.PR_interval)
    ECG_Struct.PR_interval_align(1,i)=ECG_Struct.P_locs(i)-threshold1;
    ECG_Struct.PR_interval_align(2,i)=ECG_Struct.P_locs(i)+threshold2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%% QRS Complex Fixed Interval       %%%%%%%%%%%%%%%%%%%
threshold1=floor(Fs/8);     %left side of R peaks (125, when Fs=1000)
threshold2=floor(Fs/6.5);   %right side of R peaks (153, when Fs=1000)
if(ECG_Struct.R_locs(1,1)<ECG_Struct.QRS_Complex_interval(1,1))
    for i=1:length(ECG_Struct.QRS_Complex_interval)
        ECG_Struct.QRS_Complex_interval_fixed(1,i)=ECG_Struct.R_locs(i+1)-threshold1;
        ECG_Struct.QRS_Complex_interval_fixed(2,i)=ECG_Struct.R_locs(i+1)+threshold2;
    end
else
    for i=1:length(ECG_Struct.QRS_Complex_interval)
        ECG_Struct.QRS_Complex_interval_fixed(1,i)=ECG_Struct.R_locs(i)-threshold1;
        ECG_Struct.QRS_Complex_interval_fixed(2,i)=ECG_Struct.R_locs(i)+threshold2;
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%          P wave interval fixed     %%%%%%%%%%%%%%%%
threshold1=floor(Fs/16);    %left side of P peaks (62, when Fs=1000)
threshold2=floor(Fs/28);    %right side of P peaks (35 when Fs=1000)
for i=1:length(ECG_Struct.P_wave_interval)
    ECG_Struct.P_wave_interval_fixed(1,i)=ECG_Struct.P_locs(i)-threshold1;
    ECG_Struct.P_wave_interval_fixed(2,i)=ECG_Struct.P_locs(i)+threshold2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%        T wave interval fixed %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
threshold1=floor(Fs/8.3);   %left side of T peaks (120, when Fs=1000)
threshold2=floor(Fs/11);    %left side of T peaks (90, when Fs=1000)
for i=1:length(ECG_Struct.T_wave_interval)
    ECG_Struct.T_wave_interval_fixed(1,i)=ECG_Struct.T_locs(i)-threshold1;
    ECG_Struct.T_wave_interval_fixed(2,i)=ECG_Struct.T_locs(i)+threshold2;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

threshold1=floor(Fs/16);
threshold2=floor(Fs/11);
for i=1:length(ECG_Struct.P_locs)
    ECG_Struct.P_QRS_T_Complex_interval_fixed(1,i)=ECG_Struct.P_locs(i)-threshold1;
    ECG_Struct.P_QRS_T_Complex_interval_fixed(2,i)=ECG_Struct.T_locs(i)+threshold2;
end
%%%%%%%%%%%%P-QRS-T interval fixed %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
threshold1=floor(Fs/25)+floor(Fs/8);
threshold2=floor(Fs/6)+floor(Fs/6.5);

if(ECG_Struct.R_locs(1,1)<ECG_Struct.QRS_Complex_interval(1,1))
    for i=1:length(ECG_Struct.QRS_Complex_interval)
        ECG_Struct.P_QRS_T_interval_fixed(1,i)=ECG_Struct.R_locs(i+1)-threshold1;
        ECG_Struct.P_QRS_T_interval_fixed(2,i)=ECG_Struct.R_locs(i+1)+threshold2;
    end
else
    for i=1:length(ECG_Struct.QRS_Complex_interval)
        ECG_Struct.P_QRS_T_interval_fixed(1,i)=ECG_Struct.R_locs(i)-threshold1;
        ECG_Struct.P_QRS_T_interval_fixed(2,i)=ECG_Struct.R_locs(i)+threshold2;
    end
end
%完整代码:mbd.pub/o/bread/mbd-ZJuTl5tt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end

分割结果示例如下:

工学博士,担任《Mechanical System and Signal Processing》《中国电机工程学报》《控制与决策》等期刊审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。

相关推荐
Elastic 中国社区官方博客30 分钟前
使用 Elastic AI Assistant for Search 和 Azure OpenAI 实现从 0 到 60 的转变
大数据·人工智能·elasticsearch·microsoft·搜索引擎·ai·azure
江_小_白2 小时前
自动驾驶之激光雷达
人工智能·机器学习·自动驾驶
Swift社区2 小时前
在 Swift 中实现字符串分割问题:以字典中的单词构造句子
开发语言·ios·swift
没头脑的ht2 小时前
Swift内存访问冲突
开发语言·ios·swift
没头脑的ht2 小时前
Swift闭包的本质
开发语言·ios·swift
wjs20242 小时前
Swift 数组
开发语言
yusaisai大鱼3 小时前
TensorFlow如何调用GPU?
人工智能·tensorflow
stm 学习ing3 小时前
FPGA 第十讲 避免latch的产生
c语言·开发语言·单片机·嵌入式硬件·fpga开发·fpga
LNTON羚通3 小时前
摄像机视频分析软件下载LiteAIServer视频智能分析平台玩手机打电话检测算法技术的实现
算法·目标检测·音视频·监控·视频监控
湫ccc4 小时前
《Python基础》之字符串格式化输出
开发语言·python