function label = challenge(recordName)

label = 'N';

[tm,ecg,fsOrg,siginfo]=rdmat(recordName);

td = find(tm<=1);
fsOrg = length(td)-1;
fs = 300;

% === Get ECG signal ===
[ecgSig, ecgSig_org, blsig] = getECGsig_normalized(ecg, fsOrg);

blCorrected = ecgSig_org-blsig;
blmask = BLWmask(blsig, 0.4);

[Rpeaks, ann_res, res] = getECG_Rpeaks(ecgSig);

[erId_ecg, errLoc_ecg] = err_zrs_mod(res, 4, 200);

VM = errLoc_ecg.*blmask;
% %%
%% Check signal direction and morphology

lead_direction = getLeadDirection(ecgSig, ann_res);
ecgSig = lead_direction*ecgSig;

%     %% AFIB detection


%%  Other signal detection based on PVCs

if(sum(VM)<0.4*length(VM))
    label = '~'
    return
end

RpeaksVM = Rpeaks.*VM;

RRind = find(RpeaksVM==1);

if(RRind<5)
    label = '~';
    return
end

diffRR = diff(RRind);

[diffSort, diffInd] = sort(diffRR, 'ascend');

Rmask = ones(size(Rpeaks));

if((RRind(diffInd(1))>200)&(RRind(diffInd(1)+1)+200)<length(Rmask))
    Rmask(RRind(diffInd(1))-200:RRind(diffInd(1)+1)+200) = 0;
elseif((RRind(diffInd(1))<200))
    Rmask(1:RRind(diffInd(1)+1)+200) = 0;
elseif((RRind(diffInd(1)+1)+200)>length(Rmask))
    Rmask(RRind(diffInd(1))-200:end) = 0;
end

Rpeaksmasked = RpeaksVM.*Rmask;

SC = 7;

if(length(ecgSig)<6000)
    
    [ChunkStack] = genChunks_SeqN(ecgSig, RpeaksVM, SC);
    
    load('Itr2_CNN_params_First500_fil_10_filDim_100.mat');
    
    if(numel(ChunkStack)==0)
        label = '~';
        return
    end
    
    [cost, grad, preds] = cnnCost_MC(opttheta,ChunkStack,[],numClasses,...
        filterDimX,filterDimY,numFilters,poolDim,'True');
    
    testLab = mode(preds);
    
    lab = ['N', 'A', 'O', '~'];
    label = lab(testLab);
    return
end

[ChunkStack] = genChunksTest_Seq(ecgSig, RpeaksVM, SC);

if(numel(ChunkStack)==0)
    label = '~';
    return
end

% load('Itr1_CNN_SigAO_params_3_filDim_100.mat')
load('FullRep_Itr_1_CNN_params_10_filDim_100.mat')
[cost, grad, preds1] = cnnCost_MC(opttheta,ChunkStack,[],numClasses,...
    filterDimX,filterDimY,numFilters,poolDim,'True');

load('FullRep_Itr_2_CNN_params_10_filDim_100.mat')
[cost, grad, preds2] = cnnCost_MC(opttheta,ChunkStack,[],numClasses,...
    filterDimX,filterDimY,numFilters,poolDim,'True');

load('FullRep_Itr_3_CNN_params_10_filDim_100.mat')
[cost, grad, preds3] = cnnCost_MC(opttheta,ChunkStack,[],numClasses,...
    filterDimX,filterDimY,numFilters,poolDim,'True');
% 
% load('FullRep_Itr_4_CNN_params_10_filDim_100.mat')
% [cost, grad, preds4] = cnnCost_MC(opttheta,ChunkStack,[],numClasses,...
%     filterDimX,filterDimY,numFilters,poolDim,'True');
% 
% 
% load('FullRep_Itr_5_CNN_params_10_filDim_100.mat')
% [cost, grad, preds] = cnnCost_MC(opttheta,ChunkStack,[],numClasses,...
%     filterDimX,filterDimY,numFilters,poolDim,'True');



for ik = 1:4
    p1 = find(preds1 == ik);
    p2 = find(preds2 == ik);
    p3 = find(preds3 == ik);
%     p4 = find(preds4 == ik);
%     p = find(preds == ik);
    pcount1(ik) = length(p1);
    pcount2(ik) = length(p2);
    pcount3(ik) = length(p3);
%     pcount4(ik) = length(p4);
%     pcount(ik) = length(p);
end

%predProb = pcount/length(preds);
predProb1 = pcount1/length(preds1);
predProb2 = pcount2/length(preds1);
predProb3 = pcount3/length(preds1);
%predProb4 = pcount4/length(preds);


%PP = predProb+predProb1+predProb2+predProb3+predProb4;
PP = predProb1+predProb2+predProb3;


[predConf,predInd] = max(PP);
lab = ['N', 'A', 'O', '~'];

label = lab(predInd);

if(predConf>2.5)
    if(predInd == 2 || predInd == 4)
        label = lab(predInd);
    else
        [RpeaksPVC] = locatePVCbeats(ecgSig, Rpeaksmasked);
        
        if(sum(RpeaksPVC)>0.5*sum(Rpeaksmasked))
            
            [pvcCheck] = IsPVC(ecgSig, RpeaksPVC);
            if(pvcCheck == 2)
                label = 'O';
                return
            end
        end
    end
    label = lab(predInd);
    return
end

%%
Ia1  = preds1==2;
Ia2  = preds2==2;
Ia3  = preds3==2;
%Ia4  = preds4==2;
%Ia5  = preds==2;

predLen = length(preds1);

%I = Ia1.*Ia2.*Ia3.*Ia4.*Ia5;
I = Ia1.*Ia2.*Ia3;

Ilen = sum(I);

if(Ilen>predLen/3)
    label = 'A';
    recordName
    return
end


%%
[RpeaksPVC] = locatePVCbeats(ecgSig, Rpeaksmasked);

if(sum(RpeaksPVC)>0.5*sum(Rpeaksmasked))
    
    [pvcCheck] = IsPVC(ecgSig, RpeaksPVC);
    if(pvcCheck == 2)
        label = 'O';
        return
    end
end

% [labelP3, flagP3, seqFlagP3, predProb7, predProb8, predProb9] = PredPass_3(ChunkStack);
% 
% if(flagP3 == 1)
%     label = char(labelP3);
%     return
% end


load('Odom_Itr_1_CNN_params_10_filDim_100.mat')
[cost, grad, predsO1] = cnnCost_MC(opttheta,ChunkStack,[],numClasses,...
    filterDimX,filterDimY,numFilters,poolDim,'True');

load('Odom_Itr_2_CNN_params_10_filDim_100.mat')
[cost, grad, predsO2] = cnnCost_MC(opttheta,ChunkStack,[],numClasses,...
    filterDimX,filterDimY,numFilters,poolDim,'True');


for ik = 1:4
    po1 = find(predsO1 == ik);
    po2 = find(predsO2 == ik);
   
    pcounto1(ik) = length(po1);
    pcounto2(ik) = length(po2);
      
end

predProbO1 = pcounto1/length(predsO1);
predProbO2 = pcounto2/length(predsO1);


%PPo = predProb+predProb1+predProb2+predProb3+predProb4+predProbO1+predProbO2;
PPo = predProb1+predProb2+predProb3+predProbO1+predProbO2;

[~,predIndO] = max(PPo);
lab = ['N', 'A', 'O', '~'];

label = lab(predIndO);


end


