function [classifyResult,QRS,ecg,AFEv,RR,qrs_amp_raw,QRSa,ATEv,OrgIdx,OriginCount,IrrEv,PACEv] = challengeECGp(ecg,choice)
classifyResult = 0; % default output normal rhythm
fs=300;
% [QRS,sign,en_thres] = qrs_detect2(ecg',0.25,0.6,fs);
% [~,QRS] = pt(ecg',fs);
% QRS=findBestQRSnewPD(ecg);
switch choice
    case 0
        minDiff=400;
        minPeakHeight=abs(mean(ecg));
        [~,QRS] = findpeaks(ecg,'MinPeakHeight',minPeakHeight,'MinPeakDistance',minDiff);
    case 1
        minDiff=300;
        minPeakHeight=abs(mean(ecg));
        [~,QRS] = findpeaks(ecg,'MinPeakHeight',minPeakHeight,'MinPeakDistance',minDiff);
    case 2
        minDiff=200;
        minPeakHeight=abs(mean(ecg));
        [~,QRS] = findpeaks(ecg,'MinPeakHeight',minPeakHeight,'MinPeakDistance',minDiff);
    case 3
        minDiff=100;
        minPeakHeight=abs(mean(ecg));
        [~,QRS] = findpeaks(ecg,'MinPeakHeight',minPeakHeight,'MinPeakDistance',minDiff);
    case 4
        minDiff=150;
        minPeakHeight=abs(mean(ecg));
        [~,QRS] = findpeaks(ecg,'MinPeakHeight',minPeakHeight,'MinPeakDistance',minDiff);
end
QRS=QRS(:)';
thresh=7;

x=ecg;
for kk=1:length(QRS)
    aroundQRS=x(max(QRS(kk)-thresh,1):min(QRS(kk)+thresh,length(x)));
    if x(QRS(kk))>0
        [~,v1i]=max(aroundQRS);
        QRS(kk)=QRS(kk)-thresh+v1i-1;
    else
        [~,v1i]=min(aroundQRS);
        QRS(kk)=QRS(kk)-thresh+v1i-1;
    end
    if QRS(kk)<1
        QRS(kk)=1;
    end
    if QRS(kk)>length(x)
        QRS(kk)=length(x);
    end
end




% QRS11=findBestQRSnew(ecg);
% QRS11
QRS(QRS>length(ecg))=[];
if isempty(QRS)==0
    qrs_amp_raw=ecg(QRS);
else
    qrs_amp_raw=0;
end
QRSa=QRS;
AFEv=0;OriginCount=0;IrrEv=0;PACEv=0;
ATEv=0;OrgIdx=0;

if length(QRS)<6
    RR=diff(QRS')/fs;
    AFEv =0;ATEv=0;OrgIdx=0;
    IrrEv=0;PACEv=0;OriginCount=0;
else
    RR=diff(QRS')/fs;
    [AFEv,ATEv,OrgIdx,OriginCount,IrrEv,PACEv] = comput_AFEv(RR);
    if AFEv>1
        classifyResult = 1;
    end
end