% addpath('/home/hohlochj/masterarbeit/usedMcode')
% pathToFile='/nfs/wsi/ti/messor/hohlochj/origData/';
pathToFile='/home/jph/Uni/masterarbeit/origData/';
maxFile=5;

maxSize=6;
oneSubject=true;


[subjects,numbers]=namesAndNumbers(pathToFile);
numbersMat=cell2mat(numbers);
subjectsForNumbers=cell(size(numbersMat,2),1);
j=1;
for i=1:size(subjects,2)
    subject=subjects{i};
    for number=numbers{i}
        subjectsForNumbers{j}=subject;
        j=j+1;
    end
end

if oneSubject
    numbersMat=numbersMat(1);
end

EMG=cell(size(numbersMat,2),1);

parfor j=1:size(numbersMat,2)
    number=numbersMat(j);
    subject=subjectsForNumbers{j};
    EMG{j}=readEMGSig(pathToFile,subject,number,maxFile);
end

trainingData=cell2mat(EMG);


r2Autoenc=zeros([maxSize,size(trainingData,2)]);
parfor i=1:maxSize
    ae=trainAutoencoder(trainingData',i,'ShowProgressWindow',false);
    predicted=predict(ae,trainingData');
    r2Autoenc(i,:)=correlation2(trainingData,predicted');
end

fig=figure();
subplot(1,3,1);
plot(r2Autoenc)
title('Autoencoder')
xlabel('number of syergies')
ylabel('R^2')

%PCA
[COEFF,SCORE,latent] = pca(trainingData,'Centered',false);
r2PCA=zeros([maxSize,size(trainingData,2)]);
parfor i=1:maxSize
    predicted=SCORE(:,1:i)*COEFF(:,1:i)';
    r2PCA(i,:)=correlation2(trainingData,predicted);   
end
subplot(1,3,2);
plot(r2PCA)
title('PCA')
xlabel('number of syergies')
ylabel('R^2')

%NNMF
r2NNMF=zeros([maxSize,size(trainingData,2)]);
parfor i=1:maxSize
    [W,H]=nnmf(trainingData,i);
    predicted=W*H;
    r2NNMF(i,:)=correlation2(trainingData,predicted);
end

subplot(1,3,3);
plot(r2NNMF)
title('NNMF')
xlabel('number of syergies')
ylabel('R^2')
