function PeakIndex=get_ecgindex(sig,fs)
ecgwave=sig(:,1);
bpwave=sig(:,2);
addbpwave=sig(:,3);
ecgwave=ecgwave';
bpwave=bpwave';
addbpwave=addbpwave';
lenecg=length(ecgwave);%心电信号的长度
PeakIndex=[];
BPIndex=[];

%%
%%%%%%%ecg prcosessing
ecgwave=ecgwave-mean(ecgwave);%去均值
[af,aa,aphi]=fap_estimate(ecgwave,50,fs); 
wave50=aa*cos(2*pi*af*(0:length(ecgwave)-1)/fs+aphi);
ecgwave=ecgwave-wave50;%去50Hz干扰 
[ecgwave,~]=smoth_filter(ecgwave,4,fs);%去大趋势
ecgwave=BPF(ecgwave,1,60,fs,fs+1);%去噪声

%%
%%%%%初步进行心电检测
te=3000;%将心电信号按此长度分段
du=floor(lenecg/te);%分段的段数
sub=500;%左/右加上的参照点数
Loop=3;
dilation=0.1;
thrhd=0.6;
for m=1:du
    if m==1
        wave1=ecgwave((m-1)*te+1:m*te+sub);
        wave2=ecgwave((m-1)*te+1:m*te);
      if max(wave2)-min(wave2)>0.1
        Peakindex=peakdetection_lal(wave1,fs,dilation,thrhd,Loop);
        Peakindex=delete_err(Peakindex,0,te);
        Peakindex=delete_cuojian(wave2,Peakindex);
        Peakindex=Peakindex+te*(m-1);
      else
        Peakindex=[];
      end
        PeakIndex=[PeakIndex;Peakindex];
    else if m>=2&&m<=du-1
            wave1=ecgwave((m-1)*te+1-sub:m*te+sub);
            wave2=ecgwave((m-1)*te+1:m*te);
          if max(wave2)-min(wave2)>0.1
            Peakindex=peakdetection_lal(wave1,fs,dilation,thrhd,Loop);
            Peakindex=delete_err(Peakindex,sub,te);
            Peakindex=delete_cuojian(wave2,Peakindex);
            Peakindex=Peakindex+te*(m-1);
            if isempty(Peakindex)
                Peakindex=[];
            end
          else
            Peakindex=[];
          end
            PeakIndex=[PeakIndex;Peakindex];
        else 
            wave1=ecgwave((m-1)*te+1-sub:end);
            wave2=ecgwave((m-1)*te+1:end);
          if max(wave2)-min(wave2)>0.1
            Peakindex=peakdetection_lal(wave1,fs,dilation,thrhd,Loop);
            term2=lenecg-te*(du-1);
            Peakindex=delete_err(Peakindex,sub,term2);
            Peakindex=delete_cuojian(wave2,Peakindex);
            Peakindex=Peakindex+te*(m-1);
          else
            Peakindex=[];
          end
            PeakIndex=[PeakIndex;Peakindex];
        end
    end
end
%%
meanbp=mean(abs(bpwave));
meanaddbp=mean(abs(addbpwave));
if (max(bpwave)-min(bpwave))<=10||meanbp<=2%%判断第一路血压是否存在
    counter1=0;
else
    counter1=1;
end
if (max(addbpwave)-min(addbpwave))<=10||meanaddbp<=2%%判断第一路血压是否存在
    counter2=0;
else
    counter2=1;
end
if counter1==0&&counter2==0
else
   if counter1==0&&counter2==1
      bpwave=addbpwave;
   end
%%%进行学习阶段
    [differ,thrm]=ca_differ(ecgwave,bpwave,PeakIndex,fs);
    newte=1000;
    newdu=floor(lenecg/newte);
    NPeakIndex=[];
    for m=1:newdu
        if m==1
         wave1=ecgwave((m-1)*newte+1:m*newte+sub);
         input1=bpwave((m-1)*newte+1:m*newte+sub);
         k=find(PeakIndex>newte*(m-1)&PeakIndex<=newte*m+sub);
         peakindex=PeakIndex(k(1:length(k)));
         [Peakindex,BPindex]=ECGPeak(wave1,input1,peakindex,differ,thrm,fs);
         Peakindex=delete_err(Peakindex,0,newte);
         BPindex=delete_err(BPindex,0,newte);
         Peakindex=Peakindex+newte*(m-1);  
         BPindex=BPindex+newte*(m-1);
         NPeakIndex=[NPeakIndex;Peakindex];
         BPIndex=[BPIndex;BPindex];
        else if m>=2&&m<=newdu-1
         wave1=ecgwave((m-1)*newte+1-sub:m*newte+sub);
         input1=bpwave((m-1)*newte-sub+1:m*newte+sub);
         k=find(PeakIndex>newte*(m-1)-sub&PeakIndex<=newte*m+sub);
         peakindex=PeakIndex(k(1:length(k)));
         peakindex=peakindex-(m-1)*newte+sub;
         [Peakindex,BPindex]=ECGPeak(wave1,input1,peakindex,differ,thrm,fs);
         Peakindex=delete_err(Peakindex,sub,newte);
         BPindex=delete_err(BPindex,sub,newte);
         Peakindex=Peakindex+newte*(m-1);  
         BPindex=BPindex+newte*(m-1);
         NPeakIndex=[NPeakIndex;Peakindex];
         BPIndex=[BPIndex;BPindex];
            else 
            wave1=ecgwave((m-1)*newte+1-sub:end);
            input1=bpwave((m-1)*newte-sub+1:end);
            k=find(PeakIndex>newte*(m-1)-sub&PeakIndex<=max(PeakIndex));
            peakindex=PeakIndex(k(1:length(k)));
            peakindex=peakindex-(m-1)*newte+sub;
            [Peakindex,BPindex]=ECGPeak(wave1,input1,peakindex,differ,thrm,fs);
            Peakindex=delete_err(Peakindex,sub,max(PeakIndex));
            BPindex=delete_err(BPindex,sub,max(PeakIndex));
            Peakindex=Peakindex+newte*(m-1);  
            BPindex=BPindex+newte*(m-1);
            NPeakIndex=[NPeakIndex;Peakindex];
            BPIndex=[BPIndex;BPindex];
            end
        end
    end
    PeakIndex=NPeakIndex;
end
    
        
    
        