function classifyResult = challenge(recordName)
%
% Sample entry for the 2017 PhysioNet/CinC Challenge.
%
% INPUTS:
% recordName: string specifying the record name to process
%
% OUTPUTS:
% classifyResult: integer value where
%                     N = normal rhythm
%                     A = AF
%                     O = other rhythm
%                     ~ = noisy recording (poor signal quality)
%
% To run your entry on the entire training set in a format that is
% compatible with PhysioNet's scoring enviroment, run the script
% generateValidationSet.m
%
% The challenge function requires that you have downloaded the challenge
% data 'training_set' in a subdirectory of the current directory.
%    http://physionet.org/physiobank/database/challenge/2017/
%
% This dataset is used by the generateValidationSet.m script to create
% the annotations on your training set that will be used to verify that
% your entry works properly in the PhysioNet testing environment.
%
%
% Version 1.0
%
%
% Written by: Chengyu Liu and Qiao Li January 20 2017
%             chengyu.liu@emory.edu  qiao.li@emory.edu
%
% Last modified by:
%
%

classifyResult ='~'; %'O'; % default output other rhythm
[tm,ecg,fs,siginfo]=rdmat(recordName);

%classified=classifyResult;
%% You can add the determination rules for other rhythm and noisy recordings here
if ~isrow(ecg)
    ecg=ecg';
end

persistent correlation_template1;
if isempty(correlation_template1) 
load('correlation_template.mat')
correlation_template1=correlation_template;
end

persistent others_template1;
if isempty(others_template1) 
load('others_template.mat')
others_template1=others_template;
end

persistent T_elevation_template1;
if isempty(T_elevation_template1) 
load('T_elevation_template.mat')
T_elevation_template1=T_elevation_template;
end

[data_for_log]=segment_classifier(ecg,fs,0,0,recordName,'N',3.2,'Challenge',correlation_template1,others_template1,T_elevation_template1); 
 persistent my_classifier;
if isempty(my_classifier) 
load('Last_SVM_102_plus.mat')
my_classifier=trainedClassifier;
end

yfit = my_classifier.predictFcn(data_for_log);
yfit=additional_conditions_classifier(cellstr(char(yfit)),data_for_log(17),data_for_log);

persistent my_is_in_use
if isempty(my_is_in_use) 
load('is_in_use.mat')
my_is_in_use=is_in_use;
end

persistent my_net
if isempty(my_net) 
load('additional_net.mat')
my_net=net;
end
                                    if strcmp(yfit,'N')
                                        if my_net(data_for_log(find(my_is_in_use))')>0.65
                                            yfit='O';
                                        end
                                    end
 classifyResult = char(yfit);