function resultsAnalysis_exp1
% Analysis of the results of experiment 1. The experiment was done by 9
% participants. It consisted in a real-time goal-reaching task, where the
% participants controlled a simulated hand model and were asked to imitate
% an other hand model. Two different machine learning methods were tested,
% namely Ridge regression (RR) and Gaussian process (GPR) with a RBF kernel
% computed with Euclidean distance. Models were trained on complete
% activation of six movements and tested on three different activations
% levels (1/3, 2/3, 3/3). The movements were thumb rotation, index and
% little finger flexion, wrist flexion, extension and supination.
% This file computes the success rate (SR), the time to complete task (TCT)
% for successful tasks, the time in the target (TIT) for unsuccessful
% tasks and the RMSE error for all tasks. SR, TCT, TIT and RMSE are
% computed in function of the method (GPR vs RR), per participant, per
% activation level and per movement.
% Results are registered in results/Exp1_results.txt and graphs are
% registered in graphs/ if saveOpt is activated.
%
% 
%
% Writing code takes time. Polishing it and making it available to others takes longer! 
% If some parts of the code were useful for your research of for a better understanding 
% of the algorithms, please cite the related publications.
%
% @article{Jaquier17MDPI,
%   author="Jaquier, N. and Connan, M. and Castellini, C. and Calinon, S.",
%   title="Combining electro- and tactile myography to improve hand and 
%		wrist activity detection in prostheses",
%   year="2017",
%	journal = "Technologies",
%	volume = "",
%	number = "",
%	article number = "",
% }
% 
% Copyright (c) 2017 Idiap Research Institute, http://idiap.ch/
% Written by Noémie Jaquier
% 
% This code is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 3 as
% published by the Free Software Foundation.
% 
% This code is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with this code. If not, see <http://www.gnu.org/licenses/>.

%% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nbSub = 9;

% saveOpt = true to save figures, false otherwise.
saveOpt = true;

firstColor = [0.3151 0.0712 0.4848];
secondColor = [0.9934 0.7663 0.1566];

%% Initialisation of SR, TIT, TCT of Gaussian process experiment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Global results
SR_GP = [];
TCT_GP = [];
TIT_GP = [];

% Results per subject
TCTsubj_GP = [];
TITsubj_GP = [];

% Results per activation level
SR_GP_activ03 = [];
SR_GP_activ06 = [];
SR_GP_activ1 = [];
TIT_GP_activ03 = [];
TCT_GP_activ03 = [];
TIT_GP_activ06 = [];
TCT_GP_activ06 = [];
TIT_GP_activ1 = [];
TCT_GP_activ1 = [];

% Results per movement
SR_GP_thumb = [];
TIT_GP_thumb = [];
TCT_GP_thumb = [];

SR_GP_index = [];
TIT_GP_index = [];
TCT_GP_index = [];

SR_GP_little = [];
TIT_GP_little = [];
TCT_GP_little = [];

SR_GP_wext = [];
TIT_GP_wext = [];
TCT_GP_wext = [];

SR_GP_wflex = [];
TIT_GP_wflex = [];
TCT_GP_wflex = [];

SR_GP_wsup = [];
TIT_GP_wsup = [];
TCT_GP_wsup = [];

lengthTCT_subj_GP = [];
lengthTIT_subj_GP = [];

%% Compute SR, TIT, TCT of Gaussian process regression experiment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for subjectId = 1:nbSub
	% Read data
	resultstr = sprintf('data/exp1/results_GP_S%d.txt',subjectId);
	resMat = dlmread(resultstr,',');
	
	% Global results
	success = resMat(:,end);
	TIT_GP = [TIT_GP; resMat(success == 1,end-1)];
	TCT_GP = [TCT_GP; resMat(success == 2,end-2)];
	SR_GP = [SR_GP; sum(success == 2)/length(success)];
	
	% Results per subject
	TCTsubj_GP = [TCTsubj_GP;[mean(resMat(success == 2,end-2)),std(resMat(success == 2,end-2))]];
	TITsubj_GP = [TITsubj_GP;[mean(resMat(success == 1,end-1)),std(resMat(success == 1,end-1))]];
	lengthTCT_subj_GP = [lengthTCT_subj_GP, size(resMat(success == 2,:),1)];
	lengthTIT_subj_GP = [lengthTIT_subj_GP, size(resMat(success == 1,:),1)];
	
	% Results per activation level
	activation = abs(sum(resMat(:,3:11),2));
	
	TIT_GP_activ03 = [TIT_GP_activ03; resMat(success == 1 & activation == 0.333,end-1)];
	TCT_GP_activ03 = [TCT_GP_activ03; resMat(success == 2 & activation == 0.333,end-2)];
	SR_GP_activ03 = [SR_GP_activ03; sum(success == 2 & activation == 0.333)/length(success(activation == 0.333))];
	
	TIT_GP_activ06 = [TIT_GP_activ06; resMat(success == 1 & activation == 0.666,end-1)];
	TCT_GP_activ06 = [TCT_GP_activ06; resMat(success == 2 & activation == 0.666,end-2)];
	SR_GP_activ06 = [SR_GP_activ06; sum(success == 2 & activation == 0.666)/length(success(activation == 0.666))];

	TIT_GP_activ1 = [TIT_GP_activ1; resMat(success == 1 & activation == 1.000,end-1)];
	TCT_GP_activ1 = [TCT_GP_activ1; resMat(success == 2 & activation == 1.000,end-2)];
	SR_GP_activ1 = [SR_GP_activ1; sum(success == 2 & activation == 1.000)/length(success(activation == 1.000))];
	
	% Results per movement
	dataId = resMat(:,12);
	
	SR_GP_thumb = [SR_GP_thumb; sum(success == 2 & dataId == 2^0)/length(success(dataId == 2^0))];
	TIT_GP_thumb = [TIT_GP_thumb; resMat(success == 1 & dataId == 2^0,end-1)];
	TCT_GP_thumb = [TCT_GP_thumb; resMat(success == 2 & dataId == 2^0,end-2)];
	
	SR_GP_index = [SR_GP_index; sum(success == 2 & dataId == 2^2)/length(success(dataId == 2^2))];
	TIT_GP_index = [TIT_GP_index; resMat(success == 1 & dataId == 2^2,end-1)];
	TCT_GP_index = [TCT_GP_index; resMat(success == 2 & dataId == 2^2,end-2)];
	
	SR_GP_little = [SR_GP_little; sum(success == 2 & dataId == 2^5)/length(success(dataId == 2^5))];
	TIT_GP_little = [TIT_GP_little; resMat(success == 1 & dataId == 2^5,end-1)];
	TCT_GP_little = [TCT_GP_little; resMat(success == 2 & dataId == 2^5,end-2)];
	
	SR_GP_wext = [SR_GP_wext; sum(success == 2 & dataId == 2^6)/length(success(dataId == 2^6))];
	TIT_GP_wext = [TIT_GP_wext; resMat(success == 1 & dataId == 2^6,end-1)];
	TCT_GP_wext = [TCT_GP_wext; resMat(success == 2 & dataId == 2^6,end-2)];
	
	SR_GP_wflex = [SR_GP_wflex; sum(success == 2 & dataId == -2^6)/length(success(dataId == -2^6))];
	TIT_GP_wflex = [TIT_GP_wflex; resMat(success == 1 & dataId == -2^6,end-1)];
	TCT_GP_wflex = [TCT_GP_wflex; resMat(success == 2 & dataId == -2^6,end-2)];
	
	SR_GP_wsup = [SR_GP_wsup; sum(success == 2 & dataId == -2^8)/length(success(dataId == -2^8))];
	TIT_GP_wsup = [TIT_GP_wsup; resMat(success == 1 & dataId == -2^8,end-1)];
	TCT_GP_wsup = [TCT_GP_wsup; resMat(success == 2 & dataId == -2^8,end-2)];

end

% Compute means and standard deviations
meanSR_GP = mean(SR_GP);
stdSR_GP = std(SR_GP);

meanTCT_GP = mean(TCT_GP);
stdTCT_GP = std(TCT_GP);

meanTIT_GP = mean(TIT_GP);
stdTIT_GP = std(TIT_GP);

SR_GP_activ03_stats = [mean(SR_GP_activ03), std(SR_GP_activ03)];
TCT_GP_activ03_stats = [mean(TCT_GP_activ03), std(TCT_GP_activ03)];
TIT_GP_activ03_stats = [mean(TIT_GP_activ03), std(TIT_GP_activ03)];

SR_GP_activ06_stats = [mean(SR_GP_activ06), std(SR_GP_activ06)];
TCT_GP_activ06_stats = [mean(TCT_GP_activ06), std(TCT_GP_activ06)];
TIT_GP_activ06_stats = [mean(TIT_GP_activ06), std(TIT_GP_activ06)];

SR_GP_activ1_stats = [mean(SR_GP_activ1), std(SR_GP_activ1)];
TCT_GP_activ1_stats = [mean(TCT_GP_activ1), std(TCT_GP_activ1)];
TIT_GP_activ1_stats = [mean(TIT_GP_activ1), std(TIT_GP_activ1)];

SR_GP_thumb_stats = [mean(SR_GP_thumb), std(SR_GP_thumb)];
TCT_GP_thumb_stats = [mean(TCT_GP_thumb), std(TCT_GP_thumb)];
TIT_GP_thumb_stats = [mean(TIT_GP_thumb), std(TIT_GP_thumb)];

SR_GP_index_stats = [mean(SR_GP_index), std(SR_GP_index)];
TCT_GP_index_stats = [mean(TCT_GP_index), std(TCT_GP_index)];
TIT_GP_index_stats = [mean(TIT_GP_index), std(TIT_GP_index)];

SR_GP_little_stats = [mean(SR_GP_little), std(SR_GP_little)];
TCT_GP_little_stats = [mean(TCT_GP_little), std(TCT_GP_little)];
TIT_GP_little_stats = [mean(TIT_GP_little), std(TIT_GP_little)];

SR_GP_wext_stats = [mean(SR_GP_wext), std(SR_GP_wext)];
TCT_GP_wext_stats = [mean(TCT_GP_wext), std(TCT_GP_wext)];
TIT_GP_wext_stats = [mean(TIT_GP_wext), std(TIT_GP_wext)];

SR_GP_wflex_stats = [mean(SR_GP_wflex), std(SR_GP_wflex)];
TCT_GP_wflex_stats = [mean(TCT_GP_wflex), std(TCT_GP_wflex)];
TIT_GP_wflex_stats = [mean(TIT_GP_wflex), std(TIT_GP_wflex)];

SR_GP_wsup_stats = [mean(SR_GP_wsup), std(SR_GP_wsup)];
TCT_GP_wsup_stats = [mean(TCT_GP_wsup), std(TCT_GP_wsup)];
TIT_GP_wsup_stats = [mean(TIT_GP_wsup), std(TIT_GP_wsup)];

lengthTCT_subj_GP = cumsum(lengthTCT_subj_GP);
lengthTIT_subj_GP = cumsum(lengthTIT_subj_GP);

%% Initialisation of SR, TIT, TCT of Ridge regression experiment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Global results
SR_RR = [];
TIT_RR = [];
TCT_RR = [];

% Results per subject
TCTsubj_RR = [];
TITsubj_RR = [];

% Results per activation level
SR_RR_activ03 = [];
SR_RR_activ06 = [];
SR_RR_activ1 = [];
TIT_RR_activ03 = [];
TCT_RR_activ03 = [];
TIT_RR_activ06 = [];
TCT_RR_activ06 = [];
TIT_RR_activ1 = [];
TCT_RR_activ1 = [];

% Results per movement
SR_RR_thumb = [];
TIT_RR_thumb = [];
TCT_RR_thumb = [];

SR_RR_index = [];
TIT_RR_index = [];
TCT_RR_index = [];

SR_RR_little = [];
TIT_RR_little = [];
TCT_RR_little = [];

SR_RR_wext = [];
TIT_RR_wext = [];
TCT_RR_wext = [];

SR_RR_wflex = [];
TIT_RR_wflex = [];
TCT_RR_wflex = [];

SR_RR_wsup = [];
TIT_RR_wsup = [];
TCT_RR_wsup = [];

lengthTCT_subj_RR = [];
lengthTIT_subj_RR = [];

%% Compute SR, TIT, TCT of Ridge regression experiment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for subjectId = 1:nbSub
	% Read data
	resultstr = sprintf('data/exp1/results_RR_S%d.txt',subjectId);
	resMat = dlmread(resultstr,',');
	
	% Global results
	success = resMat(:,end);
	TIT_RR = [TIT_RR; resMat(success == 1,end-1)];
	TCT_RR = [TCT_RR; resMat(success == 2,end-2)];
	SR_RR = [SR_RR; sum(success == 2)/length(success)];
	
	% Results per subject
	TCTsubj_RR = [TCTsubj_RR;[mean(resMat(success == 2,end-2)),std(resMat(success == 2,end-2))]];
	TITsubj_RR = [TITsubj_RR;[mean(resMat(success == 1,end-1)),std(resMat(success == 1,end-1))]];
	lengthTCT_subj_RR = [lengthTCT_subj_RR, size(resMat(success == 2,:),1)];
	lengthTIT_subj_RR = [lengthTIT_subj_RR, size(resMat(success == 1,:),1)];
	
	% Results per activation level
	activation = abs(sum(resMat(:,3:11),2));
	
	TIT_RR_activ03 = [TIT_RR_activ03; resMat(success == 1 & activation == 0.333,end-1)];
	TCT_RR_activ03 = [TCT_RR_activ03; resMat(success == 2 & activation == 0.333,end-2)];
	SR_RR_activ03 = [SR_RR_activ03; sum(success == 2 & activation == 0.333)/length(success(activation == 0.333))];
	
	TIT_RR_activ06 = [TIT_RR_activ06; resMat(success == 1 & activation == 0.666,end-1)];
	TCT_RR_activ06 = [TCT_RR_activ06; resMat(success == 2 & activation == 0.666,end-2)];
	SR_RR_activ06 = [SR_RR_activ06; sum(success == 2 & activation == 0.666)/length(success(activation == 0.666))];

	TIT_RR_activ1 = [TIT_RR_activ1; resMat(success == 1 & activation == 1.000,end-1)];
	TCT_RR_activ1 = [TCT_RR_activ1; resMat(success == 1 & activation == 1.000,end-2)];
	SR_RR_activ1 = [SR_RR_activ1; sum(success == 1 & activation == 1.000)/length(success(activation == 1.000))];
	
	% Results per movement
	dataId = resMat(:,12);
	
	SR_RR_thumb = [SR_RR_thumb; sum(success == 2 & dataId == 2^0)/length(success(dataId == 2^0))];
	TIT_RR_thumb = [TIT_RR_thumb; resMat(success == 1 & dataId == 2^0,end-1)];
	TCT_RR_thumb = [TCT_RR_thumb; resMat(success == 2 & dataId == 2^0,end-2)];
	
	SR_RR_index = [SR_RR_index; sum(success == 2 & dataId == 2^2)/length(success(dataId == 2^2))];
	TIT_RR_index = [TIT_RR_index; resMat(success == 1 & dataId == 2^2,end-1)];
	TCT_RR_index = [TCT_RR_index; resMat(success == 2 & dataId == 2^2,end-2)];
	
	SR_RR_little = [SR_RR_little; sum(success == 2 & dataId == 2^5)/length(success(dataId == 2^5))];
	TIT_RR_little = [TIT_RR_little; resMat(success == 1 & dataId == 2^5,end-1)];
	TCT_RR_little = [TCT_RR_little; resMat(success == 2 & dataId == 2^5,end-2)];
	
	SR_RR_wext = [SR_RR_wext; sum(success == 2 & dataId == 2^6)/length(success(dataId == 2^6))];
	TIT_RR_wext = [TIT_RR_wext; resMat(success == 1 & dataId == 2^6,end-1)];
	TCT_RR_wext = [TCT_RR_wext; resMat(success == 2 & dataId == 2^6,end-2)];
	
	SR_RR_wflex = [SR_RR_wflex; sum(success == 2 & dataId == -2^6)/length(success(dataId == -2^6))];
	TIT_RR_wflex = [TIT_RR_wflex; resMat(success == 1 & dataId == -2^6,end-1)];
	TCT_RR_wflex = [TCT_RR_wflex; resMat(success == 2 & dataId == -2^6,end-2)];
	
	SR_RR_wsup = [SR_RR_wsup; sum(success == 2 & dataId == -2^8)/length(success(dataId == -2^8))];
	TIT_RR_wsup = [TIT_RR_wsup; resMat(success == 1 & dataId == -2^8,end-1)];
	TCT_RR_wsup = [TCT_RR_wsup; resMat(success == 2 & dataId == -2^8,end-2)];
	
end

% Compute means and standard deviations
meanSR_RR = mean(SR_RR);
stdSR_RR = std(SR_RR);

meanTCT_RR = mean(TCT_RR);
stdTCT_RR = std(TCT_RR);

meanTIT_RR = mean(TIT_RR);
stdTIT_RR = std(TIT_RR);

SR_RR_activ03_stats = [mean(SR_RR_activ03), std(SR_RR_activ03)];
TCT_RR_activ03_stats = [mean(TCT_RR_activ03), std(TCT_RR_activ03)];
TIT_RR_activ03_stats = [mean(TIT_RR_activ03), std(TIT_RR_activ03)];

SR_RR_activ06_stats = [mean(SR_RR_activ06), std(SR_RR_activ06)];
TCT_RR_activ06_stats = [mean(TCT_RR_activ06), std(TCT_RR_activ06)];
TIT_RR_activ06_stats = [mean(TIT_RR_activ06), std(TIT_RR_activ06)];

SR_RR_activ1_stats = [mean(SR_RR_activ1), std(SR_RR_activ1)];
TCT_RR_activ1_stats = [mean(TCT_RR_activ1), std(TCT_RR_activ1)];
TIT_RR_activ1_stats = [mean(TIT_RR_activ1), std(TIT_RR_activ1)];

SR_RR_thumb_stats = [mean(SR_RR_thumb), std(SR_RR_thumb)];
TCT_RR_thumb_stats = [mean(TCT_RR_thumb), std(TCT_RR_thumb)];
TIT_RR_thumb_stats = [mean(TIT_RR_thumb), std(TIT_RR_thumb)];

SR_RR_index_stats = [mean(SR_RR_index), std(SR_RR_index)];
TCT_RR_index_stats = [mean(TCT_RR_index), std(TCT_RR_index)];
TIT_RR_index_stats = [mean(TIT_RR_index), std(TIT_RR_index)];

SR_RR_little_stats = [mean(SR_RR_little), std(SR_RR_little)];
TCT_RR_little_stats = [mean(TCT_RR_little), std(TCT_RR_little)];
TIT_RR_little_stats = [mean(TIT_RR_little), std(TIT_RR_little)];

SR_RR_wext_stats = [mean(SR_RR_wext), std(SR_RR_wext)];
TCT_RR_wext_stats = [mean(TCT_RR_wext), std(TCT_RR_wext)];
TIT_RR_wext_stats = [mean(TIT_RR_wext), std(TIT_RR_wext)];

SR_RR_wflex_stats = [mean(SR_RR_wflex), std(SR_RR_wflex)];
TCT_RR_wflex_stats = [mean(TCT_RR_wflex), std(TCT_RR_wflex)];
TIT_RR_wflex_stats = [mean(TIT_RR_wflex), std(TIT_RR_wflex)];

SR_RR_wsup_stats = [mean(SR_RR_wsup), std(SR_RR_wsup)];
TCT_RR_wsup_stats = [mean(TCT_RR_wsup), std(TCT_RR_wsup)];
TIT_RR_wsup_stats = [mean(TIT_RR_wsup), std(TIT_RR_wsup)];

lengthTCT_subj_RR = cumsum(lengthTCT_subj_RR);
lengthTIT_subj_RR = cumsum(lengthTIT_subj_RR);

%% Compute RMSE between stimulus and prediction
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Gaussian Process experiment
for subjectId = 1:nbSub
	datastr = sprintf('data/exp1/data_GP_S%d.mat',subjectId);
	load(datastr);
	
	activeIdx = find(stim(:,15) == 2 | stim(:,15) == 3);
	stim = stim(activeIdx,:);
	pred = pred(activeIdx,:);
	
	error_GP(subjectId,:) = computeRMSE(pred, stim(:,1:9));
	
	activation = abs(sum(stim(:,1:9),2));
	
	error_GP_activ03(subjectId,:) = computeRMSE(pred(activation == 0.333, :), stim(activation == 0.333,1:9));
	error_GP_activ06(subjectId,:) = computeRMSE(pred(activation == 0.666, :), stim(activation == 0.666,1:9));
	error_GP_activ1(subjectId,:) = computeRMSE(pred(activation == 1.000, :), stim(activation == 1.000,1:9));

	dataId = stim(:,10);
	error_GP_thumb(subjectId,:) = computeRMSE(pred(dataId == 2^0, :), stim(dataId == 2^0,1:9));
	error_GP_index(subjectId,:) = computeRMSE(pred(dataId == 2^2, :), stim(dataId == 2^2,1:9));
	error_GP_little(subjectId,:) = computeRMSE(pred(dataId == 2^5, :), stim(dataId == 2^5,1:9));
	error_GP_wext(subjectId,:) = computeRMSE(pred(dataId == 2^6, :), stim(dataId == 2^6,1:9));
	error_GP_wflex(subjectId,:) = computeRMSE(pred(dataId == -2^6, :), stim(dataId == -2^6,1:9));
	error_GP_wsup(subjectId,:) = computeRMSE(pred(dataId == -2^8, :), stim(dataId == -2^8,1:9));
end

meanRMSE_GP = mean(error_GP);
stdRMSE_GP = std(error_GP);

% Ridge regression experiment
for subjectId = 1:nbSub
	datastr = sprintf('data/exp1/data_RR_S%d.mat',subjectId);
	load(datastr);
	
	activeIdx = find(stim(:,15) == 2 | stim(:,15) == 3);
	stim = stim(activeIdx,:);
	pred = pred(activeIdx,:);
	
	error_RR(subjectId,:) = computeRMSE(pred, stim(:,1:9));
	
	activation = abs(sum(stim(:,1:9),2));
	
	error_RR_activ03(subjectId,:) = computeRMSE(pred(activation == 0.333, :), stim(activation == 0.333,1:9));
	error_RR_activ06(subjectId,:) = computeRMSE(pred(activation == 0.666, :), stim(activation == 0.666,1:9));
	error_RR_activ1(subjectId,:) = computeRMSE(pred(activation == 1.000, :), stim(activation == 1.000,1:9));

	dataId = stim(:,10);
	error_RR_thumb(subjectId,:) = computeRMSE(pred(dataId == 2^0, :), stim(dataId == 2^0,1:9));
	error_RR_index(subjectId,:) = computeRMSE(pred(dataId == 2^2, :), stim(dataId == 2^2,1:9));
	error_RR_little(subjectId,:) = computeRMSE(pred(dataId == 2^5, :), stim(dataId == 2^5,1:9));
	error_RR_wext(subjectId,:) = computeRMSE(pred(dataId == 2^6, :), stim(dataId == 2^6,1:9));
	error_RR_wflex(subjectId,:) = computeRMSE(pred(dataId == -2^6, :), stim(dataId == -2^6,1:9));
	error_RR_wsup(subjectId,:) = computeRMSE(pred(dataId == -2^8, :), stim(dataId == -2^8,1:9));
	
end

meanRMSE_RR = mean(error_RR);
stdRMSE_RR = std(error_RR);

%% Write results in txt file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fileID = fopen('results/Exp1_results.txt','w');

fprintf(fileID, 'GP: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	meanSR_GP*100, stdSR_GP*100, meanTCT_GP, stdTCT_GP, meanTIT_GP, stdTIT_GP, meanRMSE_GP, stdRMSE_GP);
fprintf(fileID, 'RR: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n\n',...
	meanSR_RR*100, stdSR_RR*100, meanTCT_RR, stdTCT_RR, meanTIT_RR, stdTIT_RR, meanRMSE_RR, stdRMSE_RR);

fprintf(fileID, 'GP activation 1/3: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_GP_activ03_stats(1)*100, SR_GP_activ03_stats(2)*100, TCT_GP_activ03_stats(1), TCT_GP_activ03_stats(2), TIT_GP_activ03_stats(1), TIT_GP_activ03_stats(2), mean(error_GP_activ03), std(error_GP_activ03));
fprintf(fileID, 'RR activation 1/3: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_RR_activ03_stats(1)*100, SR_RR_activ03_stats(2)*100, TCT_RR_activ03_stats(1), TCT_RR_activ03_stats(2), TIT_RR_activ03_stats(1), TIT_RR_activ03_stats(2), mean(error_RR_activ03), std(error_RR_activ03));

fprintf(fileID, 'GP activation 2/3: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_GP_activ06_stats(1)*100, SR_GP_activ06_stats(2)*100, TCT_GP_activ06_stats(1), TCT_GP_activ06_stats(2), TIT_GP_activ06_stats(1), TIT_GP_activ06_stats(2), mean(error_GP_activ06), std(error_GP_activ06));
fprintf(fileID, 'RR activation 2/3: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_RR_activ06_stats(1)*100, SR_RR_activ06_stats(2)*100, TCT_RR_activ06_stats(1), TCT_RR_activ06_stats(2), TIT_RR_activ06_stats(1), TIT_RR_activ06_stats(2), mean(error_RR_activ06), std(error_RR_activ06));

fprintf(fileID, 'GP activation 3/3: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_GP_activ1_stats(1)*100, SR_GP_activ1_stats(2)*100, TCT_GP_activ1_stats(1), TCT_GP_activ1_stats(2), TIT_GP_activ1_stats(1), TIT_GP_activ1_stats(2), mean(error_GP_activ1), std(error_GP_activ1));
fprintf(fileID, 'RR activation 3/3: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n\n',...
	SR_RR_activ1_stats(1)*100, SR_RR_activ1_stats(2)*100, TCT_RR_activ1_stats(1), TCT_RR_activ1_stats(2), TIT_RR_activ1_stats(1), TIT_RR_activ1_stats(2), mean(error_RR_activ1), std(error_RR_activ1));

fprintf(fileID, 'GP thumb: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_GP_thumb_stats(1)*100, SR_GP_thumb_stats(2)*100, TCT_GP_thumb_stats(1), TCT_GP_thumb_stats(2), TIT_GP_thumb_stats(1), TIT_GP_thumb_stats(2), mean(error_GP_thumb), std(error_GP_thumb));
fprintf(fileID, 'RR thumb: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_RR_thumb_stats(1)*100, SR_RR_thumb_stats(2)*100, TCT_RR_thumb_stats(1), TCT_RR_thumb_stats(2), TIT_RR_thumb_stats(1), TIT_RR_thumb_stats(2), mean(error_RR_thumb), std(error_RR_thumb));

fprintf(fileID, 'GP index: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_GP_index_stats(1)*100, SR_GP_index_stats(2)*100, TCT_GP_index_stats(1), TCT_GP_index_stats(2), TIT_GP_index_stats(1), TIT_GP_index_stats(2), mean(error_GP_index), std(error_GP_index));
fprintf(fileID, 'RR index: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_RR_index_stats(1)*100, SR_RR_index_stats(2)*100, TCT_RR_index_stats(1), TCT_RR_index_stats(2), TIT_RR_index_stats(1), TIT_RR_index_stats(2), mean(error_RR_index), std(error_RR_index));

fprintf(fileID, 'GP little: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_GP_little_stats(1)*100, SR_GP_little_stats(2)*100, TCT_GP_little_stats(1), TCT_GP_little_stats(2), TIT_GP_little_stats(1), TIT_GP_little_stats(2), mean(error_GP_little), std(error_GP_little));
fprintf(fileID, 'RR little: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n\n',...
	SR_RR_little_stats(1)*100, SR_RR_little_stats(2)*100, TCT_RR_little_stats(1), TCT_RR_little_stats(2), TIT_RR_little_stats(1), TIT_RR_little_stats(2), mean(error_RR_little), std(error_RR_little));

fprintf(fileID, 'GP wext: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_GP_wext_stats(1)*100, SR_GP_wext_stats(2)*100, TCT_GP_wext_stats(1), TCT_GP_wext_stats(2), TIT_GP_wext_stats(1), TIT_GP_wext_stats(2), mean(error_GP_wext), std(error_GP_wext));
fprintf(fileID, 'RR wext: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_RR_wext_stats(1)*100, SR_RR_wext_stats(2)*100, TCT_RR_wext_stats(1), TCT_RR_wext_stats(2), TIT_RR_wext_stats(1), TIT_RR_wext_stats(2), mean(error_RR_wext), std(error_RR_wext));

fprintf(fileID, 'GP wflex: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_GP_wflex_stats(1)*100, SR_GP_wflex_stats(2)*100, TCT_GP_wflex_stats(1), TCT_GP_wflex_stats(2), TIT_GP_wflex_stats(1), TIT_GP_wflex_stats(2), mean(error_GP_wflex), std(error_GP_wflex));
fprintf(fileID, 'RR wflex: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_RR_wflex_stats(1)*100, SR_RR_wflex_stats(2)*100, TCT_RR_wflex_stats(1), TCT_RR_wflex_stats(2), TIT_RR_wflex_stats(1), TIT_RR_wflex_stats(2), mean(error_RR_wflex), std(error_RR_wflex));

fprintf(fileID, 'GP wsup: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n',...
	SR_GP_wsup_stats(1)*100, SR_GP_wsup_stats(2)*100, TCT_GP_wsup_stats(1), TCT_GP_wsup_stats(2), TIT_GP_wsup_stats(1), TIT_GP_wsup_stats(2), mean(error_GP_wsup), std(error_GP_wsup));
fprintf(fileID, 'RR wsup: SR = %2.2f%% pm %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs, RMSE = %2.2f pm %2.2f\n\n',...
	SR_RR_wsup_stats(1)*100, SR_RR_wsup_stats(2)*100, TCT_RR_wsup_stats(1), TCT_RR_wsup_stats(2), TIT_RR_wsup_stats(1), TIT_RR_wsup_stats(2), mean(error_RR_wsup), std(error_RR_wsup));

for subjectId = 1:nbSub
	fprintf(fileID, 'GP subject %d: SR = %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs\n', ...
		subjectId, SR_GP(subjectId,1)*100, TCTsubj_GP(subjectId,1), TCTsubj_GP(subjectId,2), TITsubj_GP(subjectId,1), TITsubj_GP(subjectId,2));
	fprintf(fileID, 'RR subject %d: SR = %2.2f%%, TCT = %2.2fs pm %2.2fs, TIT = %2.2fs pm %2.2fs\n\n', ...
		subjectId, SR_RR(subjectId,1)*100, TCTsubj_RR(subjectId,1), TCTsubj_RR(subjectId,2), TITsubj_RR(subjectId,1), TITsubj_RR(subjectId,2));
end

fclose(fileID);

%% Plots of the results: Success rate
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SR in function of the method
figure(); hold on
groups = [repmat({'GP'},nbSub,1);repmat({'RR'},nbSub,1)];
boxplot([SR_GP;SR_RR],groups, ...
	'Colors','k','widths',0.2,'positions',[0.85 1.15]);
h = findobj(gca,'Tag','Box');
for j=1:2:length(h)
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
end
for j=2:2:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
end

ylim([0 1]);
set(gca,'Fontsize',14);
ylabel('Success rate','Fontsize',18);
legend([p2,p1],'GP','RR','location','southwest');
if(saveOpt)
	print('-dpng','graphs/exp1_SRmethods.png');
end

% SR in function of activation level
figure(); hold on
groups = [repmat({'GP1/3'},nbSub,1);repmat({'RR1/3'},nbSub,1);repmat({'GP2/3'},nbSub,1);...
	repmat({'RR2/3'},nbSub,1);repmat({'GP3/3'},nbSub,1);repmat({'RR3/3'},nbSub,1)];
boxplot([SR_GP_activ03;SR_RR_activ03;SR_GP_activ06;SR_RR_activ06;SR_GP_activ1;SR_RR_activ1],groups, ...
	'Colors','k','widths',0.2,'positions',[0.85 1.15 1.85 2.15 2.85 3.15]);
h = findobj(gca,'Tag','Box');
for j=1:2:length(h)
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
end
for j=2:2:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
end

ylim([0 1]);
set(gca,'xtick',1:3, 'xticklabel',{'1/3','2/3','3/3'},'Fontsize',14);
xlabel('Activation level','Fontsize',18);
ylabel('Success rate','Fontsize',18);
legend([p2,p1],'GP','RR','location','southwest');
if(saveOpt)
	print('-dpng','graphs/exp1_SRactivations.png');
end

% SR in function of movements
figure('position',[500,300,900,450]); hold on
groups = [repmat({'GP thumb'},nbSub,1);repmat({'RR thum'},nbSub,1);repmat({'GP index'},nbSub,1);...
	repmat({'RR index'},nbSub,1);repmat({'GP little'},nbSub,1);repmat({'RR little'},nbSub,1)...
	;repmat({'GP wext'},nbSub,1);repmat({'RR wext'},nbSub,1);repmat({'GP wflex'},nbSub,1);...
	repmat({'RR wflex'},nbSub,1);repmat({'GP wsup'},nbSub,1);repmat({'RR wsup'},nbSub,1)];
boxplot([SR_GP_thumb;SR_RR_thumb;SR_GP_index;SR_RR_index;SR_GP_little;SR_RR_little...
	;SR_GP_wext;SR_RR_wext;SR_GP_wflex;SR_RR_wflex;SR_GP_wsup;SR_RR_wsup],groups, ...
	'Colors','k','widths',0.2,'positions',[0.85 1.15 1.85 2.15 2.85 3.15 3.85 4.15 4.85 5.15 5.85 6.15]);
h = findobj(gca,'Tag','Box');
for j=1:2:length(h)
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
end
for j=2:2:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
end

ylim([0 1]);
set(gca,'xtick',1:6, 'xticklabel',{'Thumb flex.','Index flex.','Little flex.', 'Wrist ext.',...
	'Wrist flex.','Wrist sup.'},'Fontsize',14);
xlabel('Movements','Fontsize',18);
ylabel('Success rate','Fontsize',18);
legend([p2,p1],'GP','RR','location','northwest');
if(saveOpt)
	print('-dpng','graphs/exp1_SRmovements.png');
end

% SR in function of subjects
figure('position',[500,300,900,450]); hold on
SRvec = [SR_GP'; SR_RR'];
groups = [{'GP1'};{'GP2'};{'GP3'};{'GP4'};{'GP5'};{'GP6'};{'GP7'};{'GP8'};{'GP9'};...
	{'RR1'};{'RR2'};{'RR3'};{'RR4'};{'RR5'};{'RR6'};{'RR7'};{'RR8'};{'RR9'}];
boxplot(SRvec(:),groups,'Colors','k','widths',0.2,...
	'positions',[0.85 1.15 1.85 2.15 2.85 3.15 3.85 4.15 4.85 5.15 5.85 6.15 6.85 7.15 7.85 8.15 8.85 9.15]);
h = findobj(gca,'Tag','Box');
for j=1:2:length(h)
	xp = get(h(j),'XData');
	yp = get(h(j),'YData');
	yp([1,end-1,end]) = 0;
	p1 = patch(xp,yp,secondColor,'FaceAlpha',.75);
end
for j=2:2:length(h)
	xp = get(h(j),'XData');
	yp = get(h(j),'YData');
	yp([1,end-1,end]) = 0;
	p2 = patch(xp,yp,firstColor,'FaceAlpha',.75);
end

ylim([0 1]);
set(gca,'xtick',1:nbSub, 'xticklabel',{'1','2','3','4','5','6','7','8','9'},'Fontsize',14);
xlabel('Participant','Fontsize',18);
ylabel('Success rate','Fontsize',18);
legend([p2,p1],'GP','RR');
if(saveOpt)
	print('-dpng','graphs/exp1_SRsubjects.png');
end

%% Plots of the results: TCT, TIT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TCT, TIT in function of the method
figure('position',[300,400,550,350]); hold on
groups = [repmat({'TCT GP'},length(TCT_GP),1); repmat({'TIT GP'},length(TIT_GP),1); ...
	repmat({'TCT RR'},length(TCT_RR),1);repmat({'TIT RR'},length(TIT_RR),1)];
boxplot([-TCT_GP;TIT_GP;-TCT_RR;TIT_RR],groups, ...
	'Colors','k','widths',0.2,'orientation','horizontal','positions',[1 1.001 1.5 1.501]);
h = findobj(gca,'Tag','Box');
for j=1:length(h)/2
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
end
for j=length(h)/2+1:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
end

set(gca,'xtick',-14:2:4, 'xticklabel',{'14','12','10','8','6','4','2','0','2','4'},'Fontsize',14);
xlim([-16 6])
set(gca,'ytick',[1,1.5], 'yticklabel',{'GP','RR'},'Fontsize',14);
xlabel('                 TCT[s]                      TIT[s]','Fontsize',18);
print('-dpng','graphs/exp1_TCTTITmethods.png');

% TCT, TIT in function of activation
figure('position',[300,400,700,550]); hold on
groups = [repmat({'TCT GP1'},length(TCT_GP_activ1),1); repmat({'TIT GP1'},length(TIT_GP_activ1),1); ...
	repmat({'TCT RR1'},length(TCT_RR_activ1),1);repmat({'TIT RR1'},length(TIT_RR_activ1),1); ...
	repmat({'TCT GP06'},length(TCT_GP_activ06),1); repmat({'TIT GP06'},length(TIT_GP_activ06),1); ...
	repmat({'TCT RR06'},length(TCT_RR_activ06),1);repmat({'TIT RR06'},length(TIT_RR_activ06),1); ...
	repmat({'TCT GP03'},length(TCT_GP_activ03),1); repmat({'TIT GP03'},length(TIT_GP_activ03),1); ...
	repmat({'TCT RR03'},length(TCT_RR_activ03),1);repmat({'TIT RR03'},length(TIT_RR_activ03),1)];
boxplot([-TCT_GP_activ1;TIT_GP_activ1;-TCT_RR_activ1;TIT_RR_activ1; ...
	-TCT_GP_activ06;TIT_GP_activ06;-TCT_RR_activ06;TIT_RR_activ06; ...
	-TCT_GP_activ03;TIT_GP_activ03;-TCT_RR_activ03;TIT_RR_activ03],groups, ...
	'Colors','k','widths',0.2,'orientation','horizontal',...
	'positions',[1.35 1.351 1.65 1.651 2.35 2.351 2.65 2.651 3.35 3.351 3.65 3.651]);
h = findobj(gca,'Tag','Box');
for j=1:4:length(h)
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
	p1 = patch(get(h(j+1),'XData'),get(h(j+1),'YData'),secondColor,'FaceAlpha',.75);
end
for j=3:4:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
	p2 = patch(get(h(j+1),'XData'),get(h(j+1),'YData'),firstColor,'FaceAlpha',.75);
end

set(gca,'xtick',-14:2:4, 'xticklabel',{'14','12','10','8','6','4','2','0','2','4'},'Fontsize',14);
xlim([-16 6])
set(gca,'ytick',[1.5, 2.5, 3.5], 'yticklabel',{'1/3','2/3','3/3'},'Fontsize',14);
xlabel('                 TCT[s]                      TIT[s]','Fontsize',18);
ylabel('Activation level','Fontsize',18);
legend([p2,p1],'GP','RR','location','northeast');
if(saveOpt)
	print('-dpng','graphs/exp1_TCTTITactivations.png');
end

% TCT, TIT in function of movements
figure('position',[300,400,700,750]); hold on
groups = [repmat({'TCT GPwsup'},length(TCT_GP_wsup),1); repmat({'TIT GP1wsup'},length(TIT_GP_wsup),1); ...
	repmat({'TCT RRwsup'},length(TCT_RR_wsup),1); repmat({'TIT RRwsup'},length(TIT_RR_wsup),1); ...
	repmat({'TCT GPwflex'},length(TCT_GP_wflex),1); repmat({'TIT GPwflex'},length(TIT_GP_wflex),1); ...
	repmat({'TCT RRwflex'},length(TCT_RR_wflex),1); repmat({'TIT RRwflex'},length(TIT_RR_wflex),1); ...
	repmat({'TCT GPwext'},length(TCT_GP_wext),1); repmat({'TIT GPwext'},length(TIT_GP_wext),1); ...
	repmat({'TCT RRwext'},length(TCT_RR_wext),1); repmat({'TIT RRwext'},length(TIT_RR_wext),1); ...
	repmat({'TCT GPlittle'},length(TCT_GP_little),1); repmat({'TIT GPlittle'},length(TIT_GP_little),1); ...
	repmat({'TCT RRlittle'},length(TCT_RR_little),1); repmat({'TIT RRlittle'},length(TIT_RR_little),1); ...
	repmat({'TCT GPindex'},length(TCT_GP_index),1); repmat({'TIT GPindex'},length(TIT_GP_index),1); ...
	repmat({'TCT RRindex'},length(TCT_RR_index),1); repmat({'TIT RRindex'},length(TIT_RR_index),1); ...
	repmat({'TCT GPthumb'},length(TCT_GP_thumb),1); repmat({'TIT GPthumb'},length(TIT_GP_thumb),1); ...
	repmat({'TCT RRthumb'},length(TCT_RR_thumb),1); repmat({'TIT RRthumb'},length(TIT_RR_thumb),1)];
boxplot([-TCT_GP_wsup;TIT_GP_wsup;-TCT_RR_wsup;TIT_RR_wsup; ...
	-TCT_GP_wflex;TIT_GP_wflex;-TCT_RR_wflex;TIT_RR_wflex; ...
	-TCT_GP_wext;TIT_GP_wext;-TCT_RR_wext;TIT_RR_wext; ...
	-TCT_GP_little;TIT_GP_little;-TCT_RR_little;TIT_RR_little; ...
	-TCT_GP_index;TIT_GP_index;-TCT_RR_index;TIT_RR_index; ...
	-TCT_GP_thumb;TIT_GP_thumb;-TCT_RR_thumb;TIT_RR_thumb],groups, ...
	'Colors','k','widths',0.2,'orientation','horizontal',...
	'positions',[1.35 1.351 1.65 1.651 2.35 2.351 2.65 2.651 3.35 3.351 3.65 3.651 ...
		4.35 4.351 4.65 4.651 5.35 5.351 5.65 5.651 6.35 6.351 6.65 6.651]);
h = findobj(gca,'Tag','Box');
for j=1:4:length(h)
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
	p1 = patch(get(h(j+1),'XData'),get(h(j+1),'YData'),secondColor,'FaceAlpha',.75);
end
for j=3:4:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
	p2 = patch(get(h(j+1),'XData'),get(h(j+1),'YData'),firstColor,'FaceAlpha',.75);
end

set(gca,'xtick',-14:2:4, 'xticklabel',{'14','12','10','8','6','4','2','0','2','4'},'Fontsize',14);
xlim([-16 6])
set(gca,'ytick',[1.5, 2.5, 3.5 4.5 5.5 6.5], 'yticklabel',{'Thumb flex.','Index flex.', ...
	'Little flex.', 'Wrist ext.', 'Wrist flex.','Wrist sup.'},'Fontsize',14);
xlabel('                 TCT[s]                      TIT[s]','Fontsize',18);
ylabel('Activation level','Fontsize',18);
legend([p2,p1],'GP','RR','location','northeast');
if(saveOpt)
	print('-dpng','graphs/exp1_TCTTITmovements.png');
end

% TCT, TIT in function of subjects
figure('position',[300,400,700,750]); hold on
groups = [repmat({'TCT GP9'},lengthTCT_subj_GP(end)-lengthTCT_subj_GP(end-1)+1,1); repmat({'TIT GP9'},lengthTIT_subj_GP(end)-lengthTIT_subj_GP(end-1)+1,1); ...
	repmat({'TCT RR9'},lengthTCT_subj_RR(end)-lengthTCT_subj_RR(end-1)+1,1); repmat({'TIT RR9'},lengthTIT_subj_RR(end)-lengthTIT_subj_RR(end-1)+1,1); ...
	repmat({'TCT GP8'},lengthTCT_subj_GP(end-1)-lengthTCT_subj_GP(end-2)+1,1); repmat({'TIT GP8'},lengthTIT_subj_GP(end-1)-lengthTIT_subj_GP(end-2)+1,1); ...
	repmat({'TCT RR8'},lengthTCT_subj_RR(end-1)-lengthTCT_subj_RR(end-2)+1,1); repmat({'TIT RR8'},lengthTIT_subj_RR(end-1)-lengthTIT_subj_RR(end-2)+1,1); ...
	repmat({'TCT GP7'},lengthTCT_subj_GP(end-2)-lengthTCT_subj_GP(end-3)+1,1); repmat({'TIT GP7'},lengthTIT_subj_GP(end-2)-lengthTIT_subj_GP(end-3)+1,1); ...
	repmat({'TCT RR7'},lengthTCT_subj_RR(end-2)-lengthTCT_subj_RR(end-3)+1,1); repmat({'TIT RR7'},lengthTIT_subj_RR(end-2)-lengthTIT_subj_RR(end-3)+1,1); ...
	repmat({'TCT GP6'},lengthTCT_subj_GP(end-3)-lengthTCT_subj_GP(end-4)+1,1); repmat({'TIT GP6'},lengthTIT_subj_GP(end-3)-lengthTIT_subj_GP(end-4)+1,1); ...
	repmat({'TCT RR6'},lengthTCT_subj_RR(end-3)-lengthTCT_subj_RR(end-4)+1,1); repmat({'TIT RR6'},lengthTIT_subj_RR(end-3)-lengthTIT_subj_RR(end-4)+1,1); ...
	repmat({'TCT GP5'},lengthTCT_subj_GP(end-4)-lengthTCT_subj_GP(end-5)+1,1); repmat({'TIT GP5'},lengthTIT_subj_GP(end-4)-lengthTIT_subj_GP(end-5)+1,1); ...
	repmat({'TCT RR5'},lengthTCT_subj_RR(end-4)-lengthTCT_subj_RR(end-5)+1,1); repmat({'TIT RR5'},lengthTIT_subj_RR(end-4)-lengthTIT_subj_RR(end-5)+1,1); ...
	repmat({'TCT GP4'},lengthTCT_subj_GP(end-5)-lengthTCT_subj_GP(end-6)+1,1); repmat({'TIT GP4'},lengthTIT_subj_GP(end-5)-lengthTIT_subj_GP(end-6)+1,1); ...
	repmat({'TCT RR4'},lengthTCT_subj_RR(end-5)-lengthTCT_subj_RR(end-6)+1,1); repmat({'TIT RR4'},lengthTIT_subj_RR(end-5)-lengthTIT_subj_RR(end-6)+1,1); ...
	repmat({'TCT GP3'},lengthTCT_subj_GP(end-6)-lengthTCT_subj_GP(end-7)+1,1); repmat({'TIT GP3'},lengthTIT_subj_GP(end-6)-lengthTIT_subj_GP(end-7)+1,1); ...
	repmat({'TCT RR3'},lengthTCT_subj_RR(end-6)-lengthTCT_subj_RR(end-7)+1,1); repmat({'TIT RR3'},lengthTIT_subj_RR(end-6)-lengthTIT_subj_RR(end-7)+1,1); ...
	repmat({'TCT GP2'},lengthTCT_subj_GP(end-7)-lengthTCT_subj_GP(end-8)+1,1); repmat({'TIT GP2'},lengthTIT_subj_GP(end-7)-lengthTIT_subj_GP(end-8)+1,1); ...
	repmat({'TCT RR2'},lengthTCT_subj_RR(end-7)-lengthTCT_subj_RR(end-8)+1,1); repmat({'TIT RR2'},lengthTIT_subj_RR(end-7)-lengthTIT_subj_RR(end-8)+1,1); ...
	repmat({'TCT GP1'},lengthTCT_subj_GP(end-8),1); repmat({'TIT GP1'},lengthTIT_subj_GP(end-8),1); ...
	repmat({'TCT RR1'},lengthTCT_subj_RR(end-8),1); repmat({'TIT RR1'},lengthTIT_subj_RR(end-8),1)];
boxplot([-TCT_GP(lengthTCT_subj_GP(end-1):lengthTCT_subj_GP(end));TIT_GP(lengthTIT_subj_GP(end-1):lengthTIT_subj_GP(end));-TCT_RR(lengthTCT_subj_RR(end-1):lengthTCT_subj_RR(end));TIT_RR(lengthTIT_subj_RR(end-1):lengthTIT_subj_RR(end)); ...
	-TCT_GP(lengthTCT_subj_GP(end-2):lengthTCT_subj_GP(end-1));TIT_GP(lengthTIT_subj_GP(end-2):lengthTIT_subj_GP(end-1));-TCT_RR(lengthTCT_subj_RR(end-2):lengthTCT_subj_RR(end-1));TIT_RR(lengthTIT_subj_RR(end-2):lengthTIT_subj_RR(end-1)); ...
	-TCT_GP(lengthTCT_subj_GP(end-3):lengthTCT_subj_GP(end-2));TIT_GP(lengthTIT_subj_GP(end-3):lengthTIT_subj_GP(end-2));-TCT_RR(lengthTCT_subj_RR(end-3):lengthTCT_subj_RR(end-2));TIT_RR(lengthTIT_subj_RR(end-3):lengthTIT_subj_RR(end-2)); ...
	-TCT_GP(lengthTCT_subj_GP(end-4):lengthTCT_subj_GP(end-3));TIT_GP(lengthTIT_subj_GP(end-4):lengthTIT_subj_GP(end-3));-TCT_RR(lengthTCT_subj_RR(end-4):lengthTCT_subj_RR(end-3));TIT_RR(lengthTIT_subj_RR(end-4):lengthTIT_subj_RR(end-3)); ...
	-TCT_GP(lengthTCT_subj_GP(end-5):lengthTCT_subj_GP(end-4));TIT_GP(lengthTIT_subj_GP(end-5):lengthTIT_subj_GP(end-4));-TCT_RR(lengthTCT_subj_RR(end-5):lengthTCT_subj_RR(end-4));TIT_RR(lengthTIT_subj_RR(end-5):lengthTIT_subj_RR(end-4)); ...
	-TCT_GP(lengthTCT_subj_GP(end-6):lengthTCT_subj_GP(end-5));TIT_GP(lengthTIT_subj_GP(end-6):lengthTIT_subj_GP(end-5));-TCT_RR(lengthTCT_subj_RR(end-6):lengthTCT_subj_RR(end-5));TIT_RR(lengthTIT_subj_RR(end-6):lengthTIT_subj_RR(end-5)); ...
	-TCT_GP(lengthTCT_subj_GP(end-7):lengthTCT_subj_GP(end-6));TIT_GP(lengthTIT_subj_GP(end-7):lengthTIT_subj_GP(end-6));-TCT_RR(lengthTCT_subj_RR(end-7):lengthTCT_subj_RR(end-6));TIT_RR(lengthTIT_subj_RR(end-7):lengthTIT_subj_RR(end-6)); ...
	-TCT_GP(lengthTCT_subj_GP(end-8):lengthTCT_subj_GP(end-7));TIT_GP(lengthTIT_subj_GP(end-8):lengthTIT_subj_GP(end-7));-TCT_RR(lengthTCT_subj_RR(end-8):lengthTCT_subj_RR(end-7));TIT_RR(lengthTIT_subj_RR(end-8):lengthTIT_subj_RR(end-7)); ...
	-TCT_GP(1:lengthTCT_subj_GP(end-8));TIT_GP(1:lengthTIT_subj_GP(end-8));-TCT_RR(1:lengthTCT_subj_RR(end-8));TIT_RR(1:lengthTIT_subj_RR(end-8))],groups, ...
	'Colors','k','widths',0.2,'orientation','horizontal',...
	'positions',[1.35 1.351 1.65 1.651 2.35 2.351 2.65 2.651 3.35 3.351 3.65 3.651 ...
		4.35 4.351 4.65 4.651 5.35 5.351 5.65 5.651 6.35 6.351 6.65 6.651 ...
		7.35 7.351 7.65 7.651 8.35 8.351 8.65 8.651 9.35 9.351 9.65 9.651]);
h = findobj(gca,'Tag','Box');
for j=1:4:length(h)
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
	p1 = patch(get(h(j+1),'XData'),get(h(j+1),'YData'),secondColor,'FaceAlpha',.75);
end
for j=3:4:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
	p2 = patch(get(h(j+1),'XData'),get(h(j+1),'YData'),firstColor,'FaceAlpha',.75);
end

set(gca,'xtick',-14:2:4, 'xticklabel',{'14','12','10','8','6','4','2','0','2','4'},'Fontsize',14);
xlim([-16 6])
set(gca,'ytick',[1.5, 2.5, 3.5 4.5 5.5 6.5 7.5 8.5 9.5], 'yticklabel',{'1' '2' '3' '4' '5' '6' '7' '8' '9'},'Fontsize',14);
xlabel('                 TCT[s]                      TIT[s]','Fontsize',18);
ylabel('Activation level','Fontsize',18);
legend([p2,p1],'GP','RR','location','northeast');
if(saveOpt)
	print('-dpng','graphs/exp1_TCTTITsubjects.png');
end

%% Plots of the results: RMSE
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% RMSE in function of the method
figure(); hold on
groups = [repmat({'GP'},nbSub,1);repmat({'RR'},nbSub,1)];
boxplot([error_GP;error_RR],groups, ...
	'Colors','k','widths',0.2,'positions',[0.85 1.15]);
h = findobj(gca,'Tag','Box');
for j=1:2:length(h)
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
end
for j=2:2:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
end

ylim([0 1]);
set(gca,'Fontsize',14);
ylabel('RMSE','Fontsize',18);
legend([p2,p1],'GP','RR','location','southwest');
if(saveOpt)
	print('-dpng','graphs/exp1_errorMethods.png');
end

% RMSE in function of activation level
figure(); hold on
groups = [repmat({'GP1/3'},nbSub,1);repmat({'RR1/3'},nbSub,1);repmat({'GP2/3'},nbSub,1);...
	repmat({'RR2/3'},nbSub,1);repmat({'GP3/3'},nbSub,1);repmat({'RR3/3'},nbSub,1)];
boxplot([error_GP_activ03;error_RR_activ03;error_GP_activ06;error_RR_activ06;error_GP_activ1;error_RR_activ1],groups, ...
	'Colors','k','widths',0.2,'positions',[0.85 1.15 1.85 2.15 2.85 3.15]);
h = findobj(gca,'Tag','Box');
for j=1:2:length(h)
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
end
for j=2:2:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
end

ylim([0 1.5]);
set(gca,'xtick',1:3, 'xticklabel',{'1/3','2/3','3/3'},'Fontsize',14);
xlabel('Activation level','Fontsize',18);
ylabel('RMSE','Fontsize',18);
legend([p2,p1],'GP','RR','location','northeast');
if(saveOpt)
	print('-dpng','graphs/exp1_errorActivations.png');
end

% RMSE in function of movement
figure('position',[500,300,900,450]); hold on
groups = [repmat({'GP thumb'},nbSub,1);repmat({'RR thum'},nbSub,1);repmat({'GP index'},nbSub,1);...
	repmat({'RR index'},nbSub,1);repmat({'GP little'},nbSub,1);repmat({'RR little'},nbSub,1)...
	;repmat({'GP wextflex'},nbSub,1);repmat({'RR wextflex'},nbSub,1);repmat({'GP flex'},nbSub,1);...
	repmat({'RR flex'},nbSub,1)	;repmat({'GP wsup'},nbSub,1);repmat({'RR wsup'},nbSub,1)];
boxplot([error_GP_thumb;error_RR_thumb;error_GP_index;error_RR_index;error_GP_little;error_RR_little...
	;error_GP_wext;error_RR_wext;error_GP_wflex;error_RR_wflex;error_GP_wsup;error_RR_wsup],groups, ...
	'Colors','k','widths',0.2,'positions',[0.85 1.15 1.85 2.15 2.85 3.15 3.85 4.15 4.85 5.15 5.85 6.15]);
h = findobj(gca,'Tag','Box');
for j=1:2:length(h)
	p1 = patch(get(h(j),'XData'),get(h(j),'YData'),secondColor,'FaceAlpha',.75);
end
for j=2:2:length(h)
	p2 = patch(get(h(j),'XData'),get(h(j),'YData'),firstColor,'FaceAlpha',.75);
end

ylim([0 1.5]);
set(gca,'xtick',1:6, 'xticklabel',{'Thumb flex.','Index flex.','Little flex.', 'Wrist ext.', 'Wrist flex.','Wrist sup.'},'Fontsize',14);
xlabel('Movement','Fontsize',18);
ylabel('RMSE','Fontsize',18);
legend([p2,p1],'GP','RR','location','northeast');
if(saveOpt)
	print('-dpng','graphs/exp1_errorMovements.png');
end

% RMSE in function of subjects
figure('position',[500,300,900,450]); hold on
error_vec = [error_GP'; error_RR'];
groups = [{'GP1'};{'GP2'};{'GP3'};{'GP4'};{'GP5'};{'GP6'};{'GP7'};{'GP8'};{'GP9'};...
	{'RR1'};{'RR2'};{'RR3'};{'RR4'};{'RR5'};{'RR6'};{'RR7'};{'RR8'};{'RR9'}];
boxplot(error_vec(:),groups,'Colors','k','widths',0.2,...
	'positions',[0.85 1.15 1.85 2.15 2.85 3.15 3.85 4.15 4.85 5.15 5.85 6.15 6.85 7.15 7.85 8.15 8.85 9.15]);
h = findobj(gca,'Tag','Box');
for j=1:2:length(h)
	xp = get(h(j),'XData');
	yp = get(h(j),'YData');
	yp([1,end-1,end]) = 0;
	p1 = patch(xp,yp,secondColor,'FaceAlpha',.75);
end
for j=2:2:length(h)
	xp = get(h(j),'XData');
	yp = get(h(j),'YData');
	yp([1,end-1,end]) = 0;
	p2 = patch(xp,yp,firstColor,'FaceAlpha',.75);
end

ylim([0 1]);
set(gca,'xtick',1:nbSub, 'xticklabel',{'1','2','3','4','5','6','7','8','9'},'Fontsize',14);
xlabel('Participant','Fontsize',18);
ylabel('RMSE','Fontsize',18);
legend([p2,p1],'GP','RR');
if(saveOpt)
	print('-dpng','graphs/exp1_errorSubjects.png');
end

end

function [ error ] = computeRMSE( Y, Yhat )
%computeRMSE Computes the root mean square error
%
%   Y       Expected output N x d
%   YHat    Predicted output N x d

N = length(Y);

diff_square = sum((Y-Yhat).^2,2);

error = sum(diff_square);

error = sqrt(error./N);
end














