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] = getECGsig_normalized(ecg, fsOrg);

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

[zI_ecg, zLoc_ecg, erId_ecg, errLoc_ecg] = err_zrs(res, 5);

vid = zLoc_ecg.*errLoc_ecg;
if(sum(vid)<0.4*length(vid))
    label = '~'
    return
end

%% Mask correction
validMask = ValidMaskGen(ecgSig, Rpeaks, vid);
VM = validMask.*zLoc_ecg;
% %%
% TRpeaks = Rpeaks;%.*validMask;
% tachFil = ones(1500,1);
%
% RR_rateTach = conv(TRpeaks, tachFil, 'valid');
%
% RRmeanTach = mean(RR_rateTach);
%
% if(RRmeanTach>16)
%     label = 'O';
%     return
% end
%
% TachCheck = ones(size(RR_rateTach));
%
% TachCheck(RR_rateTach>8.5)= -1;
%
% if(sum(abs(diff(TachCheck)/2))>10)
%     isTach= 1;
% else
%     isTach = 0;
% end
% %% Regularity Check
%
% Reg = unique(RR_rateTach);
%
% if(length(Reg)<3)
%     Regularity = 1;
% else
%     Regularity = 0;
% end

%% Bradycardia

BRpeaks = Rpeaks;
if(numel(zI_ecg)>0)
    BRpeaks(zI_ecg) = 1;
end
bradFil = ones(1500,1);

RR_rateBrad = conv(BRpeaks, bradFil, 'valid');
BradCheck = ones(size(RR_rateBrad));
BradCheck(RR_rateBrad<3.3)= -1;

RRmeanBrad = mean(RR_rateBrad);

if(sum(abs(diff(BradCheck)/2))>5)
    isBrad= 1;
    label = 'O'
    return
else
    isBrad = 0;
end

%% Check signal direction and morphology

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

%     %% AFIB detection


load AFIB_dict.mat
load NSR_dict.mat
load ST_mat.mat
load ST_mat_noise.mat
[ChunkBeatVec, ChunkBeatVecr, L, Lr] = genAFIBchunks(ecgSig, Rpeaks, VM);
AfVal = 0;
NSR_Val = 0;
if(L <= 5  && Lr <= 5)
    label = '~';
elseif(L>Lr)
    AfTestBeat = mean(ChunkBeatVec,2)/norm(mean(ChunkBeatVec,2));
    chkAF = AfTestBeat'*AfDictM1;
    chkNSR = AfTestBeat'*NSR_DictM1;
    [AfVal, AfInd] = max(chkAF);
    [NSR_Val, NSR_Ind] = max(chkNSR);
    
    if(abs(AfVal-1)<10e-8)
        label = 'A';
        return
    end
    
    if(abs(NSR_Val-1)<10e-8)
        label = 'N';
        return
    end
    %
    %     if(AfVal>NSR_Val)
    %         label = 'A';
    %         if(AfVal>0.999)
    %             return
    %         end
    %     end
    %
    %     if(NSR_Val>0.999)
    %         label = 'N'
    %         return
    %     end
    
elseif(L<Lr)
    AfTestBeat = mean(ChunkBeatVecr,2)/norm(mean(ChunkBeatVecr,2));
    chkAF = AfTestBeat'*AfDictM2;
    chkNSR = AfTestBeat'*NSR_DictM2;
    [AfVal, AfInd] = max(chkAF);
    [NSR_Val, NSR_Ind] = max(chkNSR);
    
    if(abs(AfVal-1)<10e-8)
        label = 'A';
        return
    end
    
    if(abs(NSR_Val-1)<10e-8)
        label = 'N';
        return
    end
    %
    %     if(AfVal>NSR_Val)
    %         label = 'A';
    %         if(AfVal>0.999)
    %             return
    %         end
    %     end
    %
    %     if(NSR_Val>0.999)
    %         label = 'N'
    %         return
    %     end
end

% %% Other signal detection based on QRS durtation

[ChunkBeatVecOTHR] = genOTHRchunks(ecgSig, Rpeaks, VM);
QRSwidthSig = zeros(size(ChunkBeatVecOTHR));
if(numel(ChunkBeatVecOTHR>0))
    [QRSwidthSig, QRSwidth] = genQRSwidth(ChunkBeatVecOTHR);
end
if(QRSwidth>19)
    if(QRSwidth>40)
        label = '~'
        return
    else
        label = 'O';
        return
    end
end
%
%%  Other signal detection based on PVCs

[RpeaksPVC, PVCbeatVec] = locatePVCbeats(ecgSig, Rpeaks.*VM);

load PVC_mat.mat
dpval = 0;
testBeat = median(PVCbeatVec,2)/norm(median(PVCbeatVec,2));
if(length(testBeat)>0)
    dpV = testBeat'*PVC_mat;
    [dpval, dpind] = max(dpV);
    if(dpval>0.999)
        label = 'O';
        return
    end
    
end

ChunkBeatVecST = genSTchunks(ecgSig, Rpeaks, VM);
testSTchunk = ChunkBeatVecST/norm(ChunkBeatVecST);
stV = testSTchunk'*ST_mat;
stN = testSTchunk'*ST_mat_noise;
[stval, stind] = max(stV);
[stval_noise, stind_noise] = max(stN);
if(abs(stval-1)<10e-8)
    label = 'O';
    return
end
if(abs(stval_noise-1)<10e-8)
    label = '~';
    return
end
%%
TRpeaks = Rpeaks;%.*validMask;
tachFil = ones(1500,1);

RR_rateTach = conv(TRpeaks, tachFil, 'valid');


%% Regularity Check

Reg = unique(RR_rateTach);

if(length(Reg)<3)
    Regularity = 1;
    
    lead_direction = getLeadDirection(ecgSig, ann_res);
    ecgSig = lead_direction*ecgSig;
    
    
    [ChunkBeatVecOTHR] = genOTHRchunks(ecgSig, Rpeaks, VM);
    
    %QRSwidthSig = zeros(size(ChunkBeatVecOTHR));
    if(numel(ChunkBeatVecOTHR>0))
        [QRSwidthSig, QRSwidth] = genQRSwidth(ChunkBeatVecOTHR);
    end
    
    if(QRSwidth>20)
        label = 'O'
        return
    end
else
    Regularity = 0;
end

[RpeaksPVC, PVCbeatVec] = locatePVCbeats(ecgSig, Rpeaks.*VM);

if(nnz(RpeaksPVC)>3)
    label = 'O'
    return
end

[QRS,sign,en_thres] = qrs_detect2(ecgSig',0.25,0.6,fs);
% if length(QRS)<length(ann)
% else
RR=diff(QRS')/fs;
AFEv = comput_AFEv(RR);
if AFEv>1
    label = 'A';
    %     end
end


end
