%	OVERVIEW:
%       This function demonstrates the function of the synthetic RR interval 
%       generator RRGEN and the calculation of HRV metrics
%   INPUT:
%       No input necessary. RRGEN data is generated within this script
%   OUTPUT:
%       HRV Metrics exported to .cvs files
%
%   DEPENDENCIES & LIBRARIES:
%       https://github.com/cliffordlab/PhysioNet-Cardiovascular-Signal-Toolbox
%   REFERENCE: 
%       Vest et al. "An Open Source Benchmarked HRV Toolbox for Cardiovascular 
%       Waveform and Interval Analysis" Physiological Measurement (In Press), 2018. 
%	REPO:       
%       https://github.com/cliffordlab/PhysioNet-Cardiovascular-Signal-Toolbox
%   ORIGINAL SOURCE AND AUTHORS:     
%       Giulia Da Poian   
%	COPYRIGHT (C) 2018 
%   LICENSE:    
%       This software is offered freely and without warranty under 
%       the GNU (v3 or later) public license. See license file for
%       more information
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear; clc; close all;

% Remove old files generated by this demo
OldFolder = [pwd, filesep, 'OutputData', filesep, 'ResultsRRgen'];
if exist(OldFolder, 'dir')
    rmdir(OldFolder, 's');
    fprintf('Old Demo Folder deleted \n');
end


% Initialize settings for demo
HRVparams = InitializeHRVparams('demo_RRgen');  
HRVparams.MSE.on = 0; % No MSE analysis for this demo
HRVparams.DFA.on = 0; % No DFA analysis for this demo
HRVparams.HRT.on = 0; % No HRT analysis for this demo



% 1. Generate Data

rr = rrgen(HRVparams.demo.length,HRVparams.demo.pe,HRVparams.demo.pn,...
                                                      HRVparams.demo.seed);
t = cumsum(rr);

% 2. Analyze RR Data - Using HRV Toolbox Main function

[results, resFilename] = Main_HRV_Analysis(rr,t,'RRIntervals',HRVparams,...
                                                           'rrgenData',[]);


% 3 Compare generated output file with the reference one
        
currentFile = [HRVparams.writedata filesep resFilename.HRV '.csv'];
referenceFile = ['ReferenceOutput' filesep 'StandardizedData_HRV_allwindows.csv'];
testHRV = CompareOutput(currentFile,referenceFile);

if testHRV
    fprintf('** DemoStandardizedData: TEST SUCCEEDED ** \n ')
     fprintf('A file named %s.csv \n has been saved in %s \n', ...
    resFilename.HRV, HRVparams.writedata);
else
    fprintf('** DemoStandardizedData: TEST FAILED ** \n')
end
