%params
NFFT=2048;
cutWindow=1;
window=0.2;
shift=0.05;
k=10;
rounds=75;
maxExpC=7;

%svm
[signal, states, params] = load_bcidat('/home/jph/Uni/masterarbeit/Block1_ReachingMovements/AO/AO_B1001/AO_B1S001R01.dat');
% [ splitSignals, code ] = cutTrials( states, signal );
[ splitSignals, code ] = cutWindows( states, signal, params, cutWindow );

%trainingData has format trial x ch x powerSpectrum
[trainingDataEEG,trainingDataEMG]=generateTrainingData(splitSignals,NFFT,window,shift,params);

%classify for different movements and rest
classification=zeros(size(code));
for i=1:size(code)
    if code(i)<=5
        classification(i)=code(i);
    else
        classification(i)=0;
    end
end

%classify for move/rest
%classification=double(code<=5);


accurancy=zeros(rounds,3);
means=zeros(rounds,1);
for i=1:rounds
    randMap=randperm(size(trainingDataEEG,1));
    leaveOut=trainingDataEEG(mod(randMap,k+2)==0,:,:);
    leaveClasses=classification(mod(randMap,k+2)==0);
    remaining=trainingDataEEG(mod(randMap,k+2)~=0,:,:);
    remainingClasses=classification(mod(randMap,k+2)~=0);

    model=kfoldCV(remainingClasses,remaining,k,maxExpC);

    [predictions,accurancy(i,:),pvalues]=svmpredict(leaveClasses,leaveOut(:,:),model);
    means(i)=mean(leaveClasses-predictions);
end
m=mean(accurancy(:,1))
mm=mean(means)