close all
clear variables
clc

load('/home/jph/Uni/masterarbeit/data/AO1200msEMGWindow1sEEGWindow200msShift1sPause.mat');

%k=2; 
%maxExpC=0; % c\in {2^i|i=-maxExpC:1:maxExpC}

%choose to estimate based on EEG or EMG
trainingData=trainingDataEMG;
clear trainingDataEEG;
clear trainingDataEMG;

accurancy=zeros(k,3);
maxC=zeros(k,1);
noClasses=size(unique(classification),1);
cm=zeros(noClasses);
randMap=randperm(size(trainingData,1));
disp('startCV')
for i=1:k
    leaveOut=trainingData(mod(randMap,k)==i-1,:,:);
    leaveClasses=classification(mod(randMap,k)==i-1);
    remaining=trainingData(mod(randMap,k)~=i-1,:,:);
    remainingClasses=classification(mod(randMap,k)~=i-1);
    disp(datestr(datetime('now')))
    fprintf('create %ith model\n',i)
    [model,maxC(i)]=kfoldCV(remainingClasses,remaining,k,maxExpC,1000);
    disp(datestr(datetime('now')))
    
    [predictions,accurancy(i,:),pvalues]=svmpredict(leaveClasses,leaveOut(:,:),model);
    cm=cm+confusionmat(leaveClasses,predictions); %confusion matrix
    %cf: for each cue what was the prediction (maybe colourCoded)
end
meanAccurancy=mean(accurancy(:,1))

fig=figure;
cmScaled=zeros(size(cm));
for i=1:size(cm,1)
    cmScaled(i,:)=cm(i,:)/sum(cm(i,:));
end
imagesc(cmScaled)
colorbar();
saveas(fig,'/home/jph/Uni/masterarbeit/plots/cmEMG200ms1sPause.fig','fig');
