diff --git a/bashscripts/runOnEcitonPos.bash b/bashscripts/runOnEcitonPos.bash new file mode 100755 index 0000000..c5d744a --- /dev/null +++ b/bashscripts/runOnEcitonPos.bash @@ -0,0 +1,10 @@ +#!/bin/bash + +cd /nfs/wsi/ti/messor/hohlochj/origData + +#find -mindepth 2 -maxdepth 2 -type d > dirs.txt + +#sed 's,\./,,g' -i dirs.txt +#sed '/^[[:blank:]]*$/d' -i dirs.txt + +matlab < /home/hohlochj/masterarbeit/usedMcode/callAllPos.m > /nfs/wsi/ti/messor/hohlochj/log/$(date +%y-%m-%d--%T)-PosRidgeOnly.log diff --git a/usedMcode/callAllPos.m b/usedMcode/callAllPos.m new file mode 100644 index 0000000..8277331 --- /dev/null +++ b/usedMcode/callAllPos.m @@ -0,0 +1,77 @@ +addpath('/home/hohlochj/masterarbeit/usedMcode') +pathToFile='/nfs/wsi/ti/messor/hohlochj/origData/'; +%pathToFile='/home/jph/Uni/masterarbeit/Block1_ReachingMovements/'; +maxFile=5; +threshold=10000; %EMG is classified as movement + +allSubjects=true; %run all subjects and days or only one random day for one random subject + +windowEMG=0.2; +windowEEG=1; +shift=0.2; +pburgOrder=50; +minEEGFreq=0; +maxEEGFreq=200; +pause=false; +ridgeParams=[0.1]; +k=10; +maxExpC=0; +maxPerClass=250; +EEG=true; +poolObj=parpool(22); + +[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 +j=j-1; + + +if allSubjects + + %meanAccurancysEMG=zeros([j,1]); + %meanAccurancysEEG=zeros([j,1]); + %maxCEMG=zeros([j,k,1]); + %maxCEEG=zeros([j,k,1]); + cmScaledEMG=zeros([j,5,5]); + cmScaledEEG=zeros([j,5,5]); + maxRidgeParamIndex=zeros([j,3,k]); + correlationEMG=zeros([j,3]); %x,y,angle + correlationEEG=zeros([j,3]); + + parfor j=1:size(numbersMat,2) + number=numbersMat(j); + subject=subjectsForNumbers{j}; + savePath=readEEGPos(pathToFile,subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause); + % [meanAccurancysEMG(j),maxCEMG(j,:),cmScaledEMG(j,:,:)]=svmEciton(savePath,~EEG,k,maxExpC,maxPerClass); + % [meanAccurancysEEG(j),maxCEEG(j,:),cmScaledEEG(j,:,:)]=svmEciton(savePath,EEG,k,maxExpC,maxPerClass); + [correlationEMG(j,:),maxRidgeParamIndex(j,:,:)]=ridgeCV(savePath,false,k,ridgeParams); + [correlationEEG(j,:),maxRidgeParamIndex(j,:,:)]=ridgeCV(savePath,EEG,k,ridgeParams); + fprintf('%s%i finished %s\n',subject,number,datestr(datetime('now'))) + end + + save(strcat(pathToFile,sprintf('../matlabData/%s_callAllPos.mat',datestr(datetime('now'))))); +else + j=fix(rand()*size(numbersMat,2)+1); + + number=numbersMat(j); + subject=subjectsForNumbers{j}; + savePath=readEEG(pathToFile,subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause); + %[meanAccurancysEMG,maxCEMG,cmScaledEMG(:,:)]=svmEciton(savePath,~EEG,k,maxExpC,maxPerClass); + %[meanAccurancysEEG,maxCEEG,cmScaledEEG(:,:)]=svmEciton(savePath,EEG,k,maxExpC,maxPerClass); + [correlationEMG,maxRidgeParamIndex(:,:)]=ridgeCV(savePath,false,k,ridgeParams); + [correlationEEG,maxRidgeParamIndex(:,:)]=ridgeCV(savePath,EEG,k,ridgeParams); + fprintf('%s%i finished %s\n',subject,number,datestr(datetime('now'))) + + save(strcat(pathToFile,sprintf('../matlabData/%s_call%s%iPos.mat',datestr(datetime('now')),subject,number))); +end +delete(poolObj) + + diff --git a/usedMcode/generateTrainingDataPos.m b/usedMcode/generateTrainingDataPos.m new file mode 100644 index 0000000..0ac0041 --- /dev/null +++ b/usedMcode/generateTrainingDataPos.m @@ -0,0 +1,34 @@ +function [trainingDataEEG,trainingDataEMG,kinPerSec] = generateTrainingDataPos(signal,kin,windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq) + bci_sf=params.SamplingRate.NumericValue; + signalWindowEMG=bci_sf*windowEMG; + signalWindowEEG=bci_sf*windowEEG; + tempEEG=zeros([32,fix(floor(size(signal,1)/signalWindowEEG-1)*windowEEG/shift+1),maxEEGFreq-minEEGFreq+1]); + tempEMG=zeros(fix([size(signal,2)-3-32,fix(size(signal,1)/signalWindowEMG-1)*windowEMG/shift+1])); + + %Filter around 50Hz and below 2 Hz + [A,B]= butter(2,[48 52]/(bci_sf/2),'stop'); + [C,D]= butter(2,1/(bci_sf/2),'high'); + [E,F]= butter(2,[148 152]/(bci_sf/2),'stop'); + + parfor i=1:32 %filter single channel, w/o EMG, HEOG, Synchro and 0s + tempEEG(i,:,:)=shiftingPburg(filtfilt(double(E),double(F),filtfilt(double(C),double(D),filtfilt(double(A),double(B),double(signal(:,i))))),bci_sf,windowEEG,shift,pburgOrder,minEEGFreq,maxEEGFreq); + %filtfilt stat filter + end % TODO: 8-30Hz, pburg bei kurzen Fenstern (Ordnung: Je nach SamplingRate, bei 2500: 32-50, Alpha, Beta peaks sollten sichtbar sein) + trainingDataEEG=permute(tempEEG,[2 1 3]); + + parfor i=33:size(signal,2)-3 + tempEMG(i-32,:,:)=waveformLength(filtfilt(double(E),double(F),filtfilt(double(C),double(D),filtfilt(double(A),double(B),double(signal(:,i))))),bci_sf,windowEMG,shift); + end + trainingDataEMG=permute(tempEMG,[2 1 3]); + + %shift kin according to synch channel in first minute + [~,startingPoint]=max(abs(diff(signal(1:60*bci_sf,size(signal,2)-1)))); + % add time offset in ms to kin-time + offset=startingPoint/bci_sf*1000; + kin(:,1)=kin(:,1)+offset; +% disp(offset) + %add dummy timestamp at the end to fill kinematics + kin=[kin;kin(end,:)]; + kin(end,1)=size(trainingDataEEG,1)*1000*shift+windowEEG*1000; + kinPerSec=shiftingPos(kin,windowEEG,shift); +end diff --git a/usedMcode/readEEGPos.m b/usedMcode/readEEGPos.m new file mode 100644 index 0000000..dd6f85b --- /dev/null +++ b/usedMcode/readEEGPos.m @@ -0,0 +1,52 @@ +function [savePath]=readEEGPos(pathToFile, subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq,pause) + + %fprintf('start read %s%i %s\n',subject,number,datestr(datetime('now'))); + savePath=strcat(pathToFile,sprintf('../matlabData/%s%i%imsWindowEMG%isWindowEEG%imsShiftFreq%ito%iPause%ipBurg%iPos.mat',subject,number,windowEMG*1000,windowEEG,shift*1000,minEEGFreq,maxEEGFreq,pause,pburgOrder)); + + %only create file if it doesn't exist yet + if exist(savePath, 'file') ~= 2 + fprintf(strcat(savePath,' not existing; creating\n')); + trainingDataEEGcell=cell(maxFile,1); + trainingDataEMGcell=cell(maxFile,1); + classesCell=cell(maxFile,1); + kin=cell([maxFile,1]); + + for i=1:maxFile + [sig, stat, params] = load_bcidat(strcat(pathToFile,sprintf('%s/%s_B100%i/%s_B1S00%iR0%i.dat',subject,subject,number,subject,number,i))); + tmpKin=csvread(strcat(pathToFile,sprintf('kin/%s_B1S00%iR0%i.txt',subject,number,i)),1,0); + [trainingDataEEGcell{i},trainingDataEMGcell{i},kin{i}]=generateTrainingDataPos(sig,tmpKin(:,1:4),windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq); + classesCell{i}=stat.StimulusCode; + %fprintf('%ith file processed\n',i) + end + bci_sf=params.SamplingRate.NumericValue; + + clear sig + + trainingDataEEG=cell2mat(trainingDataEEGcell); + trainingDataEMG=cell2mat(trainingDataEMGcell); + classesMat=cell2mat(classesCell); + kinMat=cell2mat(kin); + clear trainingDataEEGcell trainingDataEMGcell classesCell kin + + classificationWithPause=classifyAccordingToEMG(trainingDataEEG, trainingDataEMG,classesMat,shift,bci_sf,threshold,pause); + clear classesMat + + smoothClassification=zeros(size(classificationWithPause)); + for i=1:size(classificationWithPause,1) + smoothClassification(i)=round(mode(classificationWithPause(max(i-fix(bci_sf/1000),1):min(i+fix(bci_sf/1000),end)))); + end + + clear classificationWithPause + + trainingDataEEG=trainingDataEEG(smoothClassification~=-1,:,:); + trainingDataEMG=trainingDataEMG(smoothClassification~=-1,:); + classification=smoothClassification(smoothClassification~=-1); + kinematics=kinMat(smoothClassification~=-1,:); + + clear smoothClassification i + save(savePath,'trainingDataEEG','trainingDataEMG','classification','kinematics','-v7.3'); + + + %fprintf('finished reading %s%i %s\n',subject,number,datestr(datetime('now'))); + end +end