function classifyResult = challenge(recordName)
[~,ecg,fs,~]=rdmat(recordName);
[classifyResult,QRS,ecg,AFEv,RR,QRSamp,QRSa,ATEv,OrgIdx,OriginCount,IrrEv,PACEv] = challengeAA(ecg,fs);

[classifyResult1,QRS1,ecg1,AFEv1,RR1,QRSamp1,QRSa1,ATEv1,OrgIdx1,OriginCount1,IrrEv1,PACEv1] = challengeECG(ecg(1:round(end/3)));
[classifyResult2,QRS2,ecg2,AFEv2,RR2,QRSamp2,QRSa2,ATEv2,OrgIdx2,OriginCount2,IrrEv2,PACEv2] = challengeECG(ecg(round(end/3):round(2*end/3)));
[classifyResult3,QRS3,ecg3,AFEv3,RR3,QRSamp3,QRSa3,ATEv3,OrgIdx3,OriginCount3,IrrEv3,PACEv3] = challengeECG(ecg(round(2*end/3):end));
[classifyResult4,QRS4,ecg4,AFEv4,RR4,QRSamp4,QRSa4,ATEv4,OrgIdx4,OriginCount4,IrrEv4,PACEv4] = challengeECG(ecg(round(1*end/4):end));
[classifyResult5,QRS5,ecg5,AFEv5,RR5,QRSamp5,QRSa5,ATEv5,OrgIdx5,OriginCount5,IrrEv5,PACEv5] = challengeECG(ecg(1:round(3*end/4)));

classifyResultp0=0;QRSp0=0;AFEvp0=0;RRp0=0;QRSampp0=0;ATEvp0=0;OrgIdxp0=0;OriginCountp0=0;IrrEvp0=0;PACEvp0=0;
classifyResultp1=0;QRSp1=0;AFEvp1=0;RRp1=0;QRSampp1=0;ATEvp1=0;OrgIdxp1=0;OriginCountp1=0;IrrEvp1=0;PACEvp1=0;
classifyResultp2=0;QRSp2=0;AFEvp2=0;RRp2=0;QRSampp2=0;ATEvp2=0;OrgIdxp2=0;OriginCountp2=0;IrrEvp2=0;PACEvp2=0;
classifyResultp3=0;QRSp3=0;AFEvp3=0;RRp3=0;QRSampp3=0;ATEvp3=0;OrgIdxp3=0;OriginCountp3=0;IrrEvp3=0;PACEvp3=0;
classifyResultp4=0;QRSp4=0;AFEvp4=0;RRp4=0;QRSampp4=0;ATEvp4=0;OrgIdxp4=0;OriginCountp4=0;IrrEvp4=0;PACEvp4=0;

classifyResults=classifyResult;
maxLengthEcg=18286;

lle=length(ecg);
nor=ceil(maxLengthEcg/lle);
cecg = repmat(ecg,[nor 1]);
ecgTrainVec=cecg(1:maxLengthEcg);
cqrts=length(QRS);
averageDiff=200;
if (cqrts)>2
    diff1=abs(diff(QRS));
    averageDiff=median(diff1);
end
if averageDiff>400
    averageDiff=200;
end
if isnan(averageDiff)
    averageDiff=200;
end
[peakMag1,peakLoc1] = findpeaks(ecg,'MinPeakHeight',abs(mean(ecg)),...
    'MinPeakDistance',averageDiff/2);
val3=std(peakMag1);
diffi=diff(peakLoc1);
llp=median(diffi);
val4=std(diffi);
val5=moment(peakMag1,3);
val6=moment(peakMag1,4);
val7=moment(peakMag1,7);

[peakMag2,peakLoc2] = findpeaks(abs(ecg),'MinPeakHeight',abs(mean(abs(ecg))),...
    'MinPeakDistance',averageDiff/3);
peakMag2=ecg(peakLoc2);
val3a=std(peakMag2);

diffia=diff(peakLoc2);
ddiffia=diff(diffia);

llpa=median(diffia);
val4a=std(diffia);

dllpa=median(ddiffia);
dval4a=std(ddiffia);


val5a=moment(peakMag2,3);
val6a=moment(peakMag2,4);
val7a=moment(peakMag2,7);

wdt = pulsewidth(ecgTrainVec);
%     save('wdtval','wdt')
%     ssss
if isnan(val3)==1
    val3=0;llp=0;val4=0;
end
%     [QRS,diffo{i}]=findBestQRS1(ecg,QRS,QRS1,QRS2,QRS3,QRS4,QRS5);
[QRS]=findBestQRSnewPD(ecg);

[lnn,lff]=dfa(ecg);
pp=polyfit(lnn,lff,2);
ffaa=[min(lff),max(lff),mean(lff),median(lff),std(lff),moment(lff,3),pp(1),pp(2),pp(3)];


[PPlocs,QQlocs,RRlocs,SSlocs,TTlocs,UUlocs,PPvals,QQvals,RRvals,SSvals,TTvals,UUvals,PRinterval,QRSinterval,QTinterval,meanBeats,TTPlocs,TQinterval]=findPQRSTintervalsNewN(ecg,QRS);
PRends=PRinterval+PPlocs;
%     QRSends=QRSinterval+QQvals;
TQends=TTPlocs+TQinterval;
wensTQ=[];
wensTQ2=[];
wensPR=[];
for kk=1:length(PRends)
    currentBeatPR=ecg(PPlocs(kk):PRends(kk));
    wensPR=[wensPR,wentropy(currentBeatPR,'shannon')];
end
wensQRS=[];
for kk=1:length(TQends)
    currentBeatTQ=ecg(TTPlocs(kk):TQends(kk));
    wensTQ=[wensTQ,wentropy(currentBeatTQ,'shannon')];
    wensTQ2=[wensTQ2,wentropy(currentBeatTQ,'log energy')];
end
veccc=[];
for k=1:length(PPlocs)-1
    xq=PPlocs(k):PPlocs(k+1)-1;
    xz=[PPlocs(k),PPlocs(k+1)-1];
    v=[meanBeats(k),meanBeats(k+1)];
    vq = interp1(xz,v,xq,'linear');
    veccc=[veccc,vq];
end
a1sm=mean(veccc(:));
a1sp=abs(mean(veccc(:)));
wensQRSfeatures=[mean(wensQRS),min(wensQRS),max(wensQRS),std(wensQRS),moment(wensQRS,3),moment(wensQRS,4),moment(wensQRS,5)];
wensPRfeatures=[mean(wensPR),min(wensPR),max(wensPR),std(wensPR),moment(wensPR,3),moment(wensPR,4),moment(wensPR,5)];

wensTQfeatures=[mean(wensTQ),min(wensTQ),max(wensTQ),std(wensTQ),moment(wensTQ,3),moment(wensTQ,4),moment(wensTQ,5)];
wensTQfeatures2=[mean(wensTQ2),min(wensTQ2),max(wensTQ2),std(wensTQ2),moment(wensTQ2,3),moment(wensTQ2,4),moment(wensTQ2,5)];
diffPP=diff(PPlocs);diffQQ=diff(QQlocs);diffRR=diff(RRlocs);diffSS=diff(SSlocs);diffTT=diff(TTlocs);diffUU=diff(UUlocs);
ddiffPP=diff(diffPP);ddiffQQ=diff(diffQQ);ddiffRR=diff(diffRR);ddiffSS=diff(diffSS);ddiffTT=diff(diffTT);ddiffUU=diff(diffUU);

noBeats=length(RRlocs)-1;
lengthAllBeats=RRlocs(end)-RRlocs(1);
averageBeatAll=noBeats/lengthAllBeats;

averageBeat1=length(RRlocs)*2/3/(RRlocs(round(2*end/3))-RRlocs(1));
averageBeat2=length(RRlocs)*2/3/(RRlocs(round(end))-RRlocs(round(1*end/3)));
averageBeat3=length(RRlocs)*2/3/(RRlocs(round(5*end/6))-RRlocs(round(1*end/6)));
vecBeats=[averageBeat1,averageBeat2,averageBeat3,averageBeatAll];
signsInterval=zeros(1,noBeats);
for kk=1:noBeats
    cInterval=ecg(RRlocs(kk):RRlocs(kk+1)-1);
    meanVal=mean(cInterval);
    signsInterval(kk)=sign(meanVal);
end
averageSign=mean(signsInterval);
averageBeatFeatures=[averageBeat1,averageBeat2,averageBeat3,averageBeatAll,std(vecBeats),mean(vecBeats),averageSign,std(signsInterval)];

featuresPP=[mean(PPvals),std(PPvals),mean(diffPP),std(diffPP),std(PPvals)/std(RRvals),mean(ddiffPP),std(ddiffPP)];
featuresQQ=[mean(QQvals),std(QQvals),mean(diffQQ),std(diffQQ),std(QQvals)/std(RRvals),mean(ddiffQQ),std(ddiffQQ)];
featuresRR=[mean(RRvals),std(RRvals),mean(diffRR),std(diffRR),std(RRvals)/std(RRvals),mean(ddiffRR),std(ddiffRR)];
featuresSS=[mean(SSvals),std(SSvals),mean(diffSS),std(diffSS),std(SSvals)/std(RRvals),mean(ddiffSS),std(ddiffSS)];
featuresTT=[mean(TTvals),std(TTvals),mean(diffTT),std(diffTT),std(TTvals)/std(RRvals),mean(ddiffTT),std(ddiffTT)];
featuresUU=[mean(UUvals),std(UUvals),mean(diffUU),std(diffUU),std(UUvals)/std(RRvals),mean(ddiffUU),std(ddiffUU)];
featuresPR=[mean(PRinterval),min(PRinterval),max(PRinterval),std(PRinterval),std(diff(PRinterval)),mean(diff(PRinterval))];
featuresQRS=[mean(QRSinterval),min(QRSinterval),max(QRSinterval),std(QRSinterval),std(diff(QRSinterval)),mean(diff(QRSinterval))];
featuresQT=[mean(QTinterval),min(QTinterval),max(QTinterval),std(QTinterval),std(diff(QTinterval)),mean(diff(QTinterval))];
if length(featuresPP)<7
    featuresPP=zeros(1,7);
end
if length(featuresQQ)<7
    featuresQQ=zeros(1,7);
end
if length(featuresRR)<7
    featuresRR=zeros(1,7);
end
if length(featuresSS)<7
    featuresSS=zeros(1,7);
end
if length(featuresTT)<7
    featuresTT=zeros(1,7);
end
if length(featuresUU)<7
    featuresUU=zeros(1,7);
end
if length(featuresQRS)<6
    featuresQRS=zeros(1,6);
end
if length(featuresPR)<6
    featuresPR=zeros(1,6);
end
if length(featuresQT)<6
    featuresQT=zeros(1,6);
end

featuresQ=findQRSRelatedFeatures(ecg,QRS,QRS1,QRS2,QRS3,QRS4,QRS5);

[averageDiffTrain,zzz,zzzSTD]=findECGpeaksNewFeaturesPlayQRSWenAlot1(ecgTrainVec,QRS);

averageDiffv=calculateAverageDiff(QRS,QRS1,QRS2,QRS3,QRS4,QRS5);


cres=classifyResult3+classifyResult2+classifyResult1+classifyResult4+classifyResults+classifyResult5;
%     cmax=max([abs(OriginCount1-OriginCount2),abs(OriginCount3-OriginCount1),abs(OriginCount3-OriginCount2)]);
featuresB=[averageDiffTrain,AFEv,AFEv1,AFEv2,...
    AFEv3,AFEv4,AFEv5,ATEv,ATEv1,...
    ATEv2,ATEv3,ATEv4,ATEv5,IrrEv,...
    IrrEv1,IrrEv2,IrrEv3,IrrEv4,IrrEv5,...
    PACEv,PACEv1,PACEv2,PACEv3,PACEv4,PACEv5,...
    OriginCount,OriginCount1,OriginCount2,OriginCount3,...
    OriginCount4,OriginCount5,classifyResults,classifyResult1,...
    classifyResult2,classifyResult3,classifyResult4,classifyResult5,...
    cres,OrgIdx,OrgIdx1,OrgIdx2,OrgIdx3,OrgIdx4,OrgIdx5];


featuresMed=[median([AFEv,AFEv1,AFEv2,AFEv3,AFEv4]),...
    median([IrrEv,IrrEv1,IrrEv2,IrrEv3,IrrEv4,IrrEv5]),...
    median([ATEv,ATEv1,ATEv2,ATEv3,ATEv4,ATEv5]),...
    median([OrgIdx,OrgIdx1,OrgIdx2,OrgIdx3,OrgIdx4,OrgIdx5]),...
    median([PACEv,PACEv1,PACEv2,PACEv3,PACEv4,PACEv5])...
    ];

featuresMedStd=[std([AFEv,AFEv1,AFEv2,AFEv3,AFEv4]),...
    std([IrrEv,IrrEv1,IrrEv2,IrrEv3,IrrEv4,IrrEv5]),...
    std([ATEv,ATEv1,ATEv2,ATEv3,ATEv4,ATEv5]),...
    std([OrgIdx,OrgIdx1,OrgIdx2,OrgIdx3,OrgIdx4,OrgIdx5]),...
    std([PACEv,PACEv1,PACEv2,PACEv3,PACEv4,PACEv5])];


[yy,ymeanv,ystdv]=calculateDiffECG(ecg);
[featuresAdd]=findFeaturesAlotAdd(ecg,QRS);
featuresB2=[AFEvp0,AFEvp1,AFEvp2,...
    AFEvp3,AFEvp4,ATEvp0,ATEvp1,...
    ATEvp2,ATEvp3,ATEvp4,IrrEvp0,...
    IrrEvp1,IrrEvp2,IrrEvp3,IrrEvp4,...
    PACEvp0,PACEvp1,PACEvp2,PACEvp3,PACEvp4,...
    OriginCountp0,OriginCountp1,OriginCountp2,OriginCountp3,OriginCountp4,...
    classifyResultp0,classifyResultp1,...
    classifyResultp2,classifyResultp3,classifyResultp4,...
    OrgIdxp0,OrgIdxp1,OrgIdxp2,OrgIdxp3,OrgIdxp4];

featuresMed2=[median([AFEvp0,AFEvp1,AFEvp2,AFEvp3,AFEvp4]),...
    median([IrrEvp0,IrrEvp1,IrrEvp2,IrrEvp3,IrrEvp4]),...
    median([ATEvp0,ATEvp1,ATEvp2,ATEvp3,ATEvp4]),...
    median([OriginCountp0,OriginCountp1,OriginCountp2,OriginCountp3,OriginCountp4]),...
    median([PACEvp0,PACEvp1,PACEvp2,PACEvp3,PACEvp4]),...
    median([OrgIdxp0,OrgIdxp1,OrgIdxp2,OrgIdxp3,OrgIdxp4])];


featuresMed2Std=[std([AFEvp0,AFEvp1,AFEvp2,AFEvp3,AFEvp4]),...
    std([IrrEvp0,IrrEvp1,IrrEvp2,IrrEvp3,IrrEvp4]),...
    std([ATEvp0,ATEvp1,ATEvp2,ATEvp3,ATEvp4]),...
    std([OriginCountp0,OriginCountp1,OriginCountp2,OriginCountp3,OriginCountp4]),...
    std([PACEvp0,PACEvp1,PACEvp2,PACEvp3,PACEvp4]),...
    std([OrgIdxp0,OrgIdxp1,OrgIdxp2,OrgIdxp3,OrgIdxp4])];


% featuresQp0=findBasicQRSRelatedFeatures(ecg,QRSp0);
% featuresQp1=findBasicQRSRelatedFeatures(ecg,QRSp1);
% featuresQp2=findBasicQRSRelatedFeatures(ecg,QRSp2);
% featuresQp3=findBasicQRSRelatedFeatures(ecg,QRSp3);
% featuresQp4=findBasicQRSRelatedFeatures(ecg,QRSp4);
featuresQp0=[0,0,0,0,0,0];
featuresQp1=[0,0,0,0,0,0];
featuresQp2=[0,0,0,0,0,0];
featuresQp3=[0,0,0,0,0,0];
featuresQp4=[0,0,0,0,0,0];

featuresB=[featuresB,featuresMed,llp,val3,val4,val5,val6,val7,llpa,val3a,val4a,val5a,val6a,val7a,mean(wdt),std(wdt),featuresQ,averageDiffv,zzzSTD,yy,ymeanv,ystdv,featuresAdd,featuresB2,featuresMed2...
    ,featuresQp0,featuresQp1,featuresQp2,featuresQp3,featuresQp4,featuresMedStd,featuresMed2Std,dllpa,dval4a];

featuresTrain=[zzz,featuresB,featuresPP,featuresQQ,featuresRR,featuresSS,featuresTT,featuresUU,featuresPR,featuresQRS,featuresQT,wensTQfeatures,wensTQfeatures2,std(meanBeats),mean(meanBeats),mean(abs(meanBeats)),averageBeatFeatures,wensPRfeatures,wensQRSfeatures,a1sm,a1sp,ffaa];

% QRSi=QRS;
% [QRS]=findBestQRSnewPDo(ecg);
% sum(abs(QRSi-QRS))
% QRS
featuresAll1=calculateTheAdditionalFeatures(ecg,QRS,QRS,QRS,QRS,QRS,QRS,[]);
featuresAll2=calculateFeaturesMean(ecg,QRS);
SelectedFromAddedFeatures=[41;60;63;64;70;76;78;82;87;88;90;92;99;116;118;119;122;125;127;133;140;150;159;162;181;184];
SelectedFromOriginalFeatures=[2;10;54;37;134;135;10;199;221;223;273;323;333;343;346;380;394;447;448;451;452;448;451;464;470;475;481;484;499;587;596;603;624;633;641;646;647;305;317;313];
SelectedFromMeans=[5;14;16;20;28;32;36;37;62;69;71;88;92;96;112;114;118;123;124;132;135;136;149;155;161;162;169;173];
% SelectedFromAddedFeaturesNoZeros=[41;54;56;57;63;69;71;75;80;81;83;84;91;108;110;111;113;116;118;124;131;141;150;153;172;175];

features=[featuresTrain(SelectedFromOriginalFeatures),featuresAll1(SelectedFromAddedFeatures),featuresAll2(SelectedFromMeans)];
features(isnan(features)==1)=0;
load theUsedEnsemble
[labelsS,~]=EnsembleA.predict(features);
if labelsS>4
    labelsS=3;
end
switch labelsS
    case 1
        classifyResult='N';
    case 2
        classifyResult='A';
    case 3
        classifyResult='O';
    case 4
        classifyResult='~';
end