%求differ
function [differ,thrm]=ca_differ(wave,input,PeakIndex,fs)
lenw=length(wave);%心电信号的长度
te=2000;%将心电信号按此长度分段
du=floor(lenw/te);%分段的段数
re=lenw-te*du;
speak=zeros(1,du);,
for m=1:du-1
    wave1=wave((m-1)*te+1:m*te);
    if max(wave1)-min(wave1)>=0.1
        k=find(PeakIndex>te*(m-1)&PeakIndex<=te*m);
        Peakindex=PeakIndex(k(1:length(k)));
        if length(Peakindex)>1
        	RRpeak=diff(Peakindex);
        	speak(m)=std(RRpeak);
	else
		speak(m)=10000;
	end
    else
        speak(m)=10000;
    end
end
endwave=wave((du-1)*te+1:end);
if max(endwave)-min(endwave)>=0.1
    k=find(PeakIndex>te*(du-1)&PeakIndex<=max(PeakIndex));
    Peakindex=PeakIndex(k(1:length(k)));
    if length(Peakindex)>1
	RRpeak=diff(Peakindex);
        speak(du)=std(RRpeak);
    else
	speak(du)=10000;
    end
else
    speak(du)=10000;
end
[so_speak,b]=sort(speak);
lensp=length(so_speak);
i=1;
while(i<=lensp)
    tempbp=input((b(i)-1)*te+1:b(i)*te);
    if so_speak(i)==0||(max(tempbp)-min(tempbp))<=20||max(tempbp)>200
        if i==1
            so_speak=so_speak(i+1:end);
            b=b(i+1:end);
        elseif i==lensp
            so_speak=so_speak(1:i-1);
            b=b(1:i-1);
        else
            so_speak=[so_speak(1:i-1),so_speak(i+1:end)];
            b=[b(1:i-1),b(i+1:end)];
        end
        lensp=length(so_speak);
        i=i-1;
    end
    i=i+1;
end 
i=1;
lensp=length(so_speak);
while(i<=lensp)
    tempbp=input((b(i)-1)*te+1:b(i)*te);
    [output,~]=smoth_filter(tempbp,2,fs);
    meanbp=mean(abs(output));
    diffbp=diff(tempbp);
    if meanbp<=2||max(diffbp)>=15;
        so_speak=so_speak(i+1:end);
        b=b(i+1:end);
        i=i-1;
        lensp=length(so_speak);
    else
        BPindex=BP_test(tempbp,fs);
        if length(BPindex)>=20||length(BPindex)<=3
            so_speak=so_speak(i+1:end);
            b=b(i+1:end);
            i=i-1;
            lensp=length(so_speak);
        else
           m=b(i); 
           break
        end
    end
    i=i+1;
end        
if m==du
    wave1=wave((m-1)*te+1:m*te+re);
    input1=input((m-1)*te+1:m*te+re);  
else
    wave1=wave((m-1)*te+1:m*te);
    input1=input((m-1)*te+1:m*te); 
end
indiffer=dediffer(wave1,input1);
BPIndex=BP_test(input1,fs);
[indiffer,thrm]=ca_thrm(wave1,BPIndex,indiffer);
differ=indiffer; 
end



