% This program performs the following operations
% 1) Load Test data (already preprocessed!)
% 2) Plot the envelopes (w/ mean and "true" curve")
% 3) Invokes the RSSVP code (version w/out preprocessing)

close all; clear all; clc; format long
global ini delta

% Main data matrices

FNAME = {'Lier RR1','Trl RR1','Auto RR1','Cidaut RR1','Hut RR1'}; % vector w/ name for each test

% Input data
n_curves = length(FNAME);

for i = 1:n_curves % cycle for all the tests
fname = cell2mat(FNAME(i));
data = load(fname);
Data(:,:,i) = data;
end
l = size(Data,1);

% Create the 90th percentile envelope

% 1] Comparison w/ True curve
Envelope_type3 % One of the curves as the "true one" and a variable Std
% Create the graphs
Graphs_true
mkdir('True_curve_comparison');
cd True_curve_comparison;
saveas(gcf,'Curves_True','bmp');
cd ..

% Metrics evaluation using a reduced version of RSSVP (No preprocessing)

for q = 1:n_curves-1
%Test curve
Time = Data(:,1,q);
Sim = Data(:,2,q);
Test = Data(:,2,n_curves);
delta = Time(2)-Time(1);
RSSVP_reduced
% Create a local subdirectory and move the data in it
source = cd;
zip('Images','*.bmp');
source_zip = strcat(cd,'\*.zip');
source_xls = strcat(cd,'\*.xls');
source_bmp = strcat(cd,'\*.bmp');
delete(source_bmp);
destination = cell2mat(FNAME(q));
destination_full = strcat('True_curve_comparison\',destination);
mkdir(destination_full);
movefile(source_zip,destination_full);
movefile(source_xls,destination_full);
Time = []; Test = []; Sim = []; delta = [];
disp(strcat('Wrote file to:',destination))
close all
end

% 2] Comparison w/ Mean curve
l = size(Data,1);
Envelope_type2 % Mean curve and a constant Std
Graphs_mean
mkdir('Mean_curve_comparison');
cd Mean_curve_comparison;
saveas(gcf,'Curves_Mean','bmp');
cd ..

fid = fopen('Mean_curve','w')
output = [Data(:,1,1)';avg_curve'];
fprintf(fid, '%10.5f %10.5f\n', output);
fclose(fid)

% Metrics evaluation using a reduced version of RSSVP (No preprocessing)

for q = 1:n_curves
%Test curve
Time = Data(:,1,q);
Sim = Data(:,2,q);
Test = avg_curve;
delta = Time(2)-Time(1);
RSSVP_reduced
% Create a local subdirectory and move the data in it
source = cd;
zip('Images','*.bmp');
source_zip = strcat(cd,'\*.zip');
source_xls = strcat(cd,'\*.xls');
source_bmp = strcat(cd,'\*.bmp');
delete(source_bmp);
destination = cell2mat(FNAME(q));
destination_full = strcat('Mean_curve_comparison\',destination);
mkdir(destination_full);
movefile(source_zip,destination_full);
movefile(source_xls,destination_full);
Time = []; Test = []; Sim = []; delta = [];
disp(strcat('Wrote file to:',destination))
close all
end