diff --git a/2015-sarasola-sanz-EMG-based multi-joint kinematics decoding for robot-aided rehabilitation therapies.pdf b/2015-sarasola-sanz-EMG-based multi-joint kinematics decoding for robot-aided rehabilitation therapies.pdf deleted file mode 100644 index 077f0c6..0000000 --- a/2015-sarasola-sanz-EMG-based multi-joint kinematics decoding for robot-aided rehabilitation therapies.pdf +++ /dev/null Binary files differ diff --git a/2015-shiman-Towards Decoding of Functional Movements from the Same Limb using EEG.pdf b/2015-shiman-Towards Decoding of Functional Movements from the Same Limb using EEG.pdf deleted file mode 100644 index ac26fcf..0000000 --- a/2015-shiman-Towards Decoding of Functional Movements from the Same Limb using EEG.pdf +++ /dev/null Binary files differ diff --git a/2016-spueler-Comparing Methods for Decoding Movement Trajectory from ECoG in Chronic Stroke Patients.pdf b/2016-spueler-Comparing Methods for Decoding Movement Trajectory from ECoG in Chronic Stroke Patients.pdf deleted file mode 100644 index 33278f5..0000000 --- a/2016-spueler-Comparing Methods for Decoding Movement Trajectory from ECoG in Chronic Stroke Patients.pdf +++ /dev/null Binary files differ diff --git a/analyze.m b/analyze.m deleted file mode 100644 index dbcc954..0000000 --- a/analyze.m +++ /dev/null @@ -1,24 +0,0 @@ -[signal, states, params] = load_bcidat('/home/jph/Uni/Masterarbeit/Block1_ReachingMovements/AO/AO_B1001/AO_B1S001R01.dat'); - -% [ splitSignals, code ] = cutAll( states, signal ); -[ splitSignals, code ] = cutWindows( states, signal, params, 1); - -NFFT=2048; % should be 2^n - -[ filteredSig, pwMeanMove, pwMeanRest,hz ] = myFilter( splitSignals,code,NFFT ); - -%plotChannelProzDiff(pwMeanMove,pwMeanRest,hz,params); -%plotChannels(pwMeanMove,hz,params); -plotImgScProzDiff( pwMeanMove, pwMeanRest) - -% figure -% subplot(2,1,1) -% plot(log(pwMeanMove)) -% xlim([0 150]) -% %ylim([-2 6]) -% subplot(2,1,2) -% plot(log(pwMeanRest)) -% xlim([0 150]) -% %ylim([-2 6]) - - \ No newline at end of file diff --git a/balance2Classes.m b/balance2Classes.m deleted file mode 100644 index f4bae25..0000000 --- a/balance2Classes.m +++ /dev/null @@ -1,14 +0,0 @@ -function [trainClasses,trainData]=balance2Classes(trainClasses,trainData) - %balances two classes (0,1) - eps=0.00001; - while mean(trainClasses)> 0.5+eps || mean(trainClasses) < 0.5-eps - r=floor(0.5*size(trainClasses)*rand(1)+1); - if mean(trainClasses) < 0.5 - more=find(trainClasses==0); - else - more=find(trainClasses==1); - end - trainClasses=trainClasses([1:more(r)-1,more(r)+1:end]); - trainData=trainData([1:more(r)-1,more(r)+1:end],:); - end -end \ No newline at end of file diff --git a/balanceClasses.m b/balanceClasses.m deleted file mode 100644 index 54e7ba0..0000000 --- a/balanceClasses.m +++ /dev/null @@ -1,20 +0,0 @@ -function [trainClasses,trainData]=balanceClasses(trainClasses,trainData,maxPerClass,noClasses) -% if maxPerClass > min(histcounts(trainClasses,noClasses)) -% maxPerClass=min(histcounts(trainClasses)); -% warning('not enough Data per Class; set maxPerClass to %i', maxPerClass); -% end - i=1; - delPerClass=histcounts(trainClasses,noClasses)-maxPerClass; - delIndex=zeros(sum(delPerClass),1); - while sum(delPerClass)>0 - r=fix(size(trainClasses,1)*rand(1)+1); - if delPerClass(trainClasses(r)+1)>0 && all(delIndex~=r) - delIndex(i)=r; - i=i+1; - delPerClass(trainClasses(r)+1)=delPerClass(trainClasses(r)+1)-1; - end - end - trainClasses(delIndex)=[]; - trainData(delIndex,:)=[]; - assert(size(trainClasses,1)>0) -end \ No newline at end of file diff --git a/callAll.m b/callAll.m deleted file mode 100644 index aceaf65..0000000 --- a/callAll.m +++ /dev/null @@ -1,27 +0,0 @@ -addpath('/home/hohlochj/masterarbeit') -pathToFile='/nfs/wsi/ti/messor/hohlochj/origData/dirs.txt'; -% pathToFile='/home/jph/Uni/masterarbeit/Block1_ReachingMovements/dirs.txt'; - -windowEMG=0.2; -windowEEG=1; -shift=0.2; -pburgOrder=50; -minEEGFreq=0; -maxEEGFreq=200; -k=10; -maxExpC=0; -maxPerClass=250; -EEG=true; -poolObj=parpool(32); - -[subjects,numbers]=structForAllDays(pathToFile); -for i=1:size(subjects,2) - subject=subjects{i}; - for number=numbers{i} -% fprintf('%s%i\n',subject,number); - readEEGEciton(subject,number,windowEMG,windowEEG,shift,5,7500,pburgOrder,minEEGFreq,maxEEGFreq); - svmEciton(subject,number,false,k,maxExpC,maxPerClass); - svmEciton(subject,number,EEG,k,maxExpC,maxPerClass); - end -end -delete(poolObj) diff --git a/chooseForCode.m b/chooseForCode.m deleted file mode 100644 index aa15422..0000000 --- a/chooseForCode.m +++ /dev/null @@ -1,12 +0,0 @@ -function [ splitSamplesCode ] = chooseForCode(code,codes, splitSamples) -%chooseForCode chooses the samples for given code -% input the code to choose, the list of codes, the number of Samples and -% the list of Samples to get the list with the given code - splitSamplesCode=[]; - for i=1:size(codes) - if code == codes(i) - splitSamplesCode=[splitSamplesCode,splitSamples(:,:,i)]; - end - end -end - diff --git a/classifyAccordingToEMG.m b/classifyAccordingToEMG.m deleted file mode 100644 index 4937188..0000000 --- a/classifyAccordingToEMG.m +++ /dev/null @@ -1,26 +0,0 @@ -function [classification]=classifyAccordingToEMG(trainingDataEEG, trainingDataEMG, classes,shift,frequency,threshold) -% classifyAccordingToEMG data classified as -1 should be taken out - classification=zeros([size(trainingDataEEG,1),1]); - oldclass=0; - for i=1:size(trainingDataEEG,1) - if mean(trainingDataEMG(i,:)) > threshold % is movement - class=mode(classes(max(round(shift*frequency*i-frequency),1):min(round(i*shift*frequency-1),end))); - if class <= 5 % task is to move - if oldclass==0 - for j=max(fix(i-1/shift),1):fix(i-0.5/shift) % Pause before beginning of movement - classification(j)=-1; %code for pause - end - for j=max(fix(i-0.5/shift),1):i % half a second before executed movement there should be activity (esp. EEG) - classification(j)=-1; %if -1 there is a pause of 1s befor movement onset - end - oldclass=class; - end - classification(i)=class; - else % task is to rest but movement not finished - classification(i)=oldclass; - end - else - oldclass=0; - end - end -end \ No newline at end of file diff --git a/cutAll.m b/cutAll.m deleted file mode 100644 index 123d7b6..0000000 --- a/cutAll.m +++ /dev/null @@ -1,16 +0,0 @@ -function [ splitSignals, code ] = cutAll( states, signal ) -% cut all in same length by dropping the tail - -changeIndices=[find(diff(double(states.StimulusCode)));size(states.StimulusCode,1)]; -codeCount=[diff(changeIndices); size(states.StimulusCode,1)-sum(diff(changeIndices))]; - -splitSignals=[]; -code =zeros(size(codeCount,1)); -c=codeCount(1); - -for i=2:size(codeCount,1) - splitSignals=cat(3,splitSignals,signal(c:c+4998,:)); - code(i)=states.StimulusCode(c); - c=c+codeCount(i); -end -end \ No newline at end of file diff --git a/cutMovements.m b/cutMovements.m deleted file mode 100644 index bcd0132..0000000 --- a/cutMovements.m +++ /dev/null @@ -1,22 +0,0 @@ -function [ splitSignals, code ] = cutMovements( stimulusCodes, signal ) -%cut Trials only, ignore resting phases - -noSamplesPerTrial=15000; -% count how many recorded values belong to one trial -changeIndices=[find(diff(double(stimulusCodes)));size(stimulusCodes,1)]; -codeCount=[diff(changeIndices); size(stimulusCodes,1)-sum(diff(changeIndices))]; - -splitSignals=[]; -code=[]; - -c=1; - -for i=1:size(codeCount,1) - if(codeCount(i)==noSamplesPerTrial && stimulusCodes(c)~=0) - splitSignals=cat(3,splitSignals,signal(c:c+codeCount(i)-1,:)); - code=cat(1,code,stimulusCodes(c)); - end - c=c+codeCount(i); -end -end - diff --git a/cutWindows.m b/cutWindows.m deleted file mode 100644 index 61b5b46..0000000 --- a/cutWindows.m +++ /dev/null @@ -1,21 +0,0 @@ -function [ splitSignals, code ] = cutWindows( stimulusCodes, signal, params, t ) - changeIndices=cat(1,0,find(diff(double(stimulusCodes)))); - codeCount=diff(changeIndices); - interval=t*params.SamplingRate.NumericValue; - - splitSignals=[]; - code=[]; - - c=codeCount(1)+1; - %cut windows of 1s - for i=2:(size(codeCount,1)) %exclude beginning and end (stimulus 0) - tempc=0; - while codeCount(i)-tempc >= interval - splitSignals=cat(3,splitSignals,signal(c+tempc:c+tempc+interval-1,:)); - code=cat(1,code,double(stimulusCodes(c+tempc))); - tempc=tempc+interval; - end - c=c+codeCount(i); - end -end - diff --git a/fileInfo.txt b/fileInfo.txt deleted file mode 100644 index a4a7f43..0000000 --- a/fileInfo.txt +++ /dev/null @@ -1,11 +0,0 @@ -Stimuli Sequence -12 3 8 1 10 4 7 2 15 4 12 1 14 2 9 3 11 2 13 4 10 3 7 3 12 1 9 1 15 1 6 3 13 2 8 4 11 1 14 4 6 4 9 1 14 2 7 4 6 1 8 3 10 3 7 2 15 4 12 1 14 2 9 4 11 3 13 4 10 2 7 1 12 3 9 2 15 2 6 3 13 - -Pre & Post Run -3s - -Stimulus duration -4s - -Sampling Rate -2500 diff --git a/generateTrainingData.m b/generateTrainingData.m deleted file mode 100644 index 6cc8601..0000000 --- a/generateTrainingData.m +++ /dev/null @@ -1,23 +0,0 @@ -function [trainingDataEEG,trainingDataEMG] = generateTrainingData(signal,windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq) - frequency=params.SamplingRate.NumericValue; - signalWindowEMG=frequency*windowEMG; - signalWindowEEG=frequency*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]/(frequency/2),'stop'); - [C,D]= butter(2,1/(frequency/2),'high'); - [E,F]= butter(2,[148 152]/(frequency/2),'stop'); - - parfor i=1:32 %filter single channel, w/o 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))))),frequency,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(filter(C,D,filter(A,B,signal(:,i))),frequency,windowEMG,shift); - end - trainingDataEMG=permute(tempEMG,[2 1 3]); -end diff --git a/kfoldCV.m b/kfoldCV.m deleted file mode 100644 index 94f27d8..0000000 --- a/kfoldCV.m +++ /dev/null @@ -1,32 +0,0 @@ -function [model,maxC] = kfoldCV(classification,trainingData,k,cExpMax,maxDataPerClass) - noClasses=size(unique(classification),1); -if cExpMax>0 %CV only if necessary - cvAccurancy=zeros(2*cExpMax+1,1); - parfor cExp=1:2*cExpMax+1 - c=2^(cExp-cExpMax-1); - - randomMapping=transpose(randperm(size(trainingData,1))); - accurancy=zeros(k,3); - - for i=1:k - trainData=trainingData(mod(randomMapping,k)+1~=i,:,:); - testData=trainingData(mod(randomMapping,k)+1==i,:,:); - trainClasses=classification(mod(randomMapping,k)+1~=i); - testClasses=classification(mod(randomMapping,k)+1==i); - %fprintf('i=%i, k=%i, c=%i\n',i,k,c) - [trainClasses,trainData]=balanceClasses(trainClasses,trainData,maxDataPerClass,noClasses); - model=svmtrain(trainClasses,trainData(:,:),sprintf('-t 0 -c %f -q',c)); - [~, accurancy(i,:), ~]=svmpredict(testClasses, testData(:,:), model,'-q'); - end - - cvAccurancy(cExp)=mean(accurancy(:,1)); - end - [~,maxC]=max(cvAccurancy); -else - maxC=1; %no gridsearch since only one C possible -end - bestC=2^(maxC-cExpMax-1); - [balancedClasses, balanceData]=balanceClasses(classification,trainingData,maxDataPerClass,noClasses); - model=svmtrain(balancedClasses, balanceData(:,:),sprintf('-t 0 -c %f -q',bestC)); - -end diff --git a/myFilter.m b/myFilter.m deleted file mode 100644 index 8f6c13d..0000000 --- a/myFilter.m +++ /dev/null @@ -1,26 +0,0 @@ -function [ filteredSig, pwMeanMove, pwMeanRest, hz ] = myFilter( splitSignals,code,NFFT ) - %Filter around 50Hz and below 2 Hz - [A,B]= butter(2,[48 52]/1250,'stop'); - [C,D]= butter(2,2/1250,'high'); - - - filteredSig=zeros(size(splitSignals)); - pwMeanMove=zeros(NFFT/2+1,size(splitSignals,2)); - pwMeanRest=zeros(NFFT/2+1,size(splitSignals,2)); - - for i=1:size(splitSignals,2) %filter single channel - for j=1:size(splitSignals,3) %filter single trial - filteredSig(:,i,j)=filter(C,D,filter(A,B,splitSignals(:,i,j))); - if code(j)<=5 - pwMeanMove(:,i)=pwMeanMove(:,i)+pwelch(filteredSig(:,i,j),[],[],NFFT,2500); - elseif code(j) > 5 - pwMeanRest(:,i)=pwMeanRest(:,i)+pwelch(filteredSig(:,i,j),[],[],NFFT,2500); - end - %s=cat(1,s,size(pwelch(filteredSig(:,i,j),[],[],[],2500))); - end - pwMeanMove(:,i)=pwMeanMove(:,i) /size(find(code <=5),1); - pwMeanRest(:,i)=pwMeanRest(:,i) /size(find(code > 5),1); - end - [a,hz]=pwelch(filteredSig(:,1,1),[],[],NFFT,2500); -end - diff --git a/oldMcode/analyze.m b/oldMcode/analyze.m new file mode 100644 index 0000000..dbcc954 --- /dev/null +++ b/oldMcode/analyze.m @@ -0,0 +1,24 @@ +[signal, states, params] = load_bcidat('/home/jph/Uni/Masterarbeit/Block1_ReachingMovements/AO/AO_B1001/AO_B1S001R01.dat'); + +% [ splitSignals, code ] = cutAll( states, signal ); +[ splitSignals, code ] = cutWindows( states, signal, params, 1); + +NFFT=2048; % should be 2^n + +[ filteredSig, pwMeanMove, pwMeanRest,hz ] = myFilter( splitSignals,code,NFFT ); + +%plotChannelProzDiff(pwMeanMove,pwMeanRest,hz,params); +%plotChannels(pwMeanMove,hz,params); +plotImgScProzDiff( pwMeanMove, pwMeanRest) + +% figure +% subplot(2,1,1) +% plot(log(pwMeanMove)) +% xlim([0 150]) +% %ylim([-2 6]) +% subplot(2,1,2) +% plot(log(pwMeanRest)) +% xlim([0 150]) +% %ylim([-2 6]) + + \ No newline at end of file diff --git a/oldMcode/balance2Classes.m b/oldMcode/balance2Classes.m new file mode 100644 index 0000000..f4bae25 --- /dev/null +++ b/oldMcode/balance2Classes.m @@ -0,0 +1,14 @@ +function [trainClasses,trainData]=balance2Classes(trainClasses,trainData) + %balances two classes (0,1) + eps=0.00001; + while mean(trainClasses)> 0.5+eps || mean(trainClasses) < 0.5-eps + r=floor(0.5*size(trainClasses)*rand(1)+1); + if mean(trainClasses) < 0.5 + more=find(trainClasses==0); + else + more=find(trainClasses==1); + end + trainClasses=trainClasses([1:more(r)-1,more(r)+1:end]); + trainData=trainData([1:more(r)-1,more(r)+1:end],:); + end +end \ No newline at end of file diff --git a/oldMcode/cutAll.m b/oldMcode/cutAll.m new file mode 100644 index 0000000..123d7b6 --- /dev/null +++ b/oldMcode/cutAll.m @@ -0,0 +1,16 @@ +function [ splitSignals, code ] = cutAll( states, signal ) +% cut all in same length by dropping the tail + +changeIndices=[find(diff(double(states.StimulusCode)));size(states.StimulusCode,1)]; +codeCount=[diff(changeIndices); size(states.StimulusCode,1)-sum(diff(changeIndices))]; + +splitSignals=[]; +code =zeros(size(codeCount,1)); +c=codeCount(1); + +for i=2:size(codeCount,1) + splitSignals=cat(3,splitSignals,signal(c:c+4998,:)); + code(i)=states.StimulusCode(c); + c=c+codeCount(i); +end +end \ No newline at end of file diff --git a/oldMcode/cutMovements.m b/oldMcode/cutMovements.m new file mode 100644 index 0000000..bcd0132 --- /dev/null +++ b/oldMcode/cutMovements.m @@ -0,0 +1,22 @@ +function [ splitSignals, code ] = cutMovements( stimulusCodes, signal ) +%cut Trials only, ignore resting phases + +noSamplesPerTrial=15000; +% count how many recorded values belong to one trial +changeIndices=[find(diff(double(stimulusCodes)));size(stimulusCodes,1)]; +codeCount=[diff(changeIndices); size(stimulusCodes,1)-sum(diff(changeIndices))]; + +splitSignals=[]; +code=[]; + +c=1; + +for i=1:size(codeCount,1) + if(codeCount(i)==noSamplesPerTrial && stimulusCodes(c)~=0) + splitSignals=cat(3,splitSignals,signal(c:c+codeCount(i)-1,:)); + code=cat(1,code,stimulusCodes(c)); + end + c=c+codeCount(i); +end +end + diff --git a/oldMcode/cutSingleWindow.m b/oldMcode/cutSingleWindow.m new file mode 100644 index 0000000..526a8bf --- /dev/null +++ b/oldMcode/cutSingleWindow.m @@ -0,0 +1,18 @@ +function [ splitSignals, code ] = cutSingleWindow( stimulusCodes, signal, params, t ) + changeIndices=cat(1,0,find(diff(double(stimulusCodes)))); + codeCount=diff(changeIndices); + interval=t*params.SamplingRate.NumericValue; + + splitSignals=[]; + code=[]; + + c=codeCount(1)+1; + %cut windows of 1s + for i=2:(size(codeCount,1)) %exclude beginning and end (stimulus 0) + if codeCount(i) >= interval + splitSignals=cat(3,splitSignals,signal(c:c+interval-1,:)); + code=cat(1,code,double(stimulusCodes(c))); + end + c=c+codeCount(i); + end +end diff --git a/oldMcode/cutTrainingData.m b/oldMcode/cutTrainingData.m new file mode 100644 index 0000000..413658c --- /dev/null +++ b/oldMcode/cutTrainingData.m @@ -0,0 +1,29 @@ +load('/home/jph/Uni/masterarbeit/data/AO1.mat'); + +NFFT=2048; +cutWindowEEG=2; +window=0.2; +shift=0.05; +k=10; +maxExpC=7; + +%[ splitSignals, code ] = cutMovements( stimulusCodes, signal ); +[ splitSignals, code ] = cutSingleWindow( stimulusCodes, signal, params, cutWindowEEG ); + +%trainingData has format trial x ch x powerSpectrum +[trainingDataEEG,trainingDataEMG]=generateTrainingData(splitSignals,NFFT,window,shift,params,cutWindowEEG); + +%%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 +% can't use cutMovements then +classification=double(code<=5); diff --git a/oldMcode/cutWindows.m b/oldMcode/cutWindows.m new file mode 100644 index 0000000..61b5b46 --- /dev/null +++ b/oldMcode/cutWindows.m @@ -0,0 +1,21 @@ +function [ splitSignals, code ] = cutWindows( stimulusCodes, signal, params, t ) + changeIndices=cat(1,0,find(diff(double(stimulusCodes)))); + codeCount=diff(changeIndices); + interval=t*params.SamplingRate.NumericValue; + + splitSignals=[]; + code=[]; + + c=codeCount(1)+1; + %cut windows of 1s + for i=2:(size(codeCount,1)) %exclude beginning and end (stimulus 0) + tempc=0; + while codeCount(i)-tempc >= interval + splitSignals=cat(3,splitSignals,signal(c+tempc:c+tempc+interval-1,:)); + code=cat(1,code,double(stimulusCodes(c+tempc))); + tempc=tempc+interval; + end + c=c+codeCount(i); + end +end + diff --git a/oldMcode/myFilter.m b/oldMcode/myFilter.m new file mode 100644 index 0000000..8f6c13d --- /dev/null +++ b/oldMcode/myFilter.m @@ -0,0 +1,26 @@ +function [ filteredSig, pwMeanMove, pwMeanRest, hz ] = myFilter( splitSignals,code,NFFT ) + %Filter around 50Hz and below 2 Hz + [A,B]= butter(2,[48 52]/1250,'stop'); + [C,D]= butter(2,2/1250,'high'); + + + filteredSig=zeros(size(splitSignals)); + pwMeanMove=zeros(NFFT/2+1,size(splitSignals,2)); + pwMeanRest=zeros(NFFT/2+1,size(splitSignals,2)); + + for i=1:size(splitSignals,2) %filter single channel + for j=1:size(splitSignals,3) %filter single trial + filteredSig(:,i,j)=filter(C,D,filter(A,B,splitSignals(:,i,j))); + if code(j)<=5 + pwMeanMove(:,i)=pwMeanMove(:,i)+pwelch(filteredSig(:,i,j),[],[],NFFT,2500); + elseif code(j) > 5 + pwMeanRest(:,i)=pwMeanRest(:,i)+pwelch(filteredSig(:,i,j),[],[],NFFT,2500); + end + %s=cat(1,s,size(pwelch(filteredSig(:,i,j),[],[],[],2500))); + end + pwMeanMove(:,i)=pwMeanMove(:,i) /size(find(code <=5),1); + pwMeanRest(:,i)=pwMeanRest(:,i) /size(find(code > 5),1); + end + [a,hz]=pwelch(filteredSig(:,1,1),[],[],NFFT,2500); +end + diff --git a/oldMcode/plotChannelDiff.m b/oldMcode/plotChannelDiff.m new file mode 100644 index 0000000..e3e0e01 --- /dev/null +++ b/oldMcode/plotChannelDiff.m @@ -0,0 +1,17 @@ +function [] = plotChannelDiff( pwMeanMove, pwMeanRest,hz, params ) + +for i=1:size(params.ChannelNames.Value,1) + n=mod(i,9); + if n==1 + figure + elseif n==0; + n=9; + end + subplot(3,3,n); + plot(hz,pwMeanMove(:,i)-pwMeanRest(:,i)) + xlim([0 150]) + title(params.ChannelNames.Value(i)) +end + +end + diff --git a/oldMcode/plotChannelProzDiff.m b/oldMcode/plotChannelProzDiff.m new file mode 100644 index 0000000..b57bd79 --- /dev/null +++ b/oldMcode/plotChannelProzDiff.m @@ -0,0 +1,17 @@ +function [] = plotChannelProzDiff( pwMeanMove, pwMeanRest,hz, params ) + +for i=1:size(params.ChannelNames.Value,1) + n=mod(i,9); + if n==1 + figure + elseif n==0; + n=9; + end + subplot(3,3,n); + plot(hz,(pwMeanMove(:,i)-pwMeanRest(:,i))./pwMeanRest(:,i)) + xlim([0 150]) + title(params.ChannelNames.Value(i)) +end + +end + diff --git a/oldMcode/plotChannels.m b/oldMcode/plotChannels.m new file mode 100644 index 0000000..9126ec1 --- /dev/null +++ b/oldMcode/plotChannels.m @@ -0,0 +1,17 @@ +function [] = plotChannels( pwMean, hz, params ) + +for i=1:size(params.ChannelNames.Value,1) + n=mod(i,9); + if n==1 + figure + elseif n==0; + n=9; + end + subplot(3,3,n); + plot(log(hz),log(pwMean(:,i,:))) + xlim([0 150]) + title(params.ChannelNames.Value(i)) +end + +end + diff --git a/oldMcode/plotImgScProzDiff.m b/oldMcode/plotImgScProzDiff.m new file mode 100644 index 0000000..a1e6b34 --- /dev/null +++ b/oldMcode/plotImgScProzDiff.m @@ -0,0 +1,8 @@ +function [] = plotImgScProzDiff( pwMeanMove, pwMeanRest) + +prozDiff=(pwMeanMove-pwMeanRest)./pwMeanRest; +imagesc(1:1.22:100,1:32,transpose(prozDiff(1:82,1:32))) +colorbar(); + +end + diff --git a/oldMcode/plotStairs.m b/oldMcode/plotStairs.m new file mode 100644 index 0000000..ee7577a --- /dev/null +++ b/oldMcode/plotStairs.m @@ -0,0 +1,10 @@ +function [ ] = plotStairs( inVec ) +%plotStairs plots Stairs for Vector +% plots sum up to Index i as value for i + stair=zeros(size(inVec)); + for i=1:size(classification,1) + stair(i)=sum(inVec(1:i)); + end + plot(stairs) +end + diff --git a/oldMcode/plots/cmEMGAO1200msWindow1sPause.fig b/oldMcode/plots/cmEMGAO1200msWindow1sPause.fig new file mode 100644 index 0000000..8da1819 --- /dev/null +++ b/oldMcode/plots/cmEMGAO1200msWindow1sPause.fig Binary files differ diff --git a/oldMcode/plots/emgHist.fig b/oldMcode/plots/emgHist.fig new file mode 100644 index 0000000..e9e770a --- /dev/null +++ b/oldMcode/plots/emgHist.fig Binary files differ diff --git a/oldMcode/plots/imgSc.jpg b/oldMcode/plots/imgSc.jpg new file mode 100644 index 0000000..8fa50af --- /dev/null +++ b/oldMcode/plots/imgSc.jpg Binary files differ diff --git a/oldMcode/plots/mitFilter13.fig b/oldMcode/plots/mitFilter13.fig new file mode 100644 index 0000000..e12cb52 --- /dev/null +++ b/oldMcode/plots/mitFilter13.fig Binary files differ diff --git a/oldMcode/plots/ohneFilter13.fig b/oldMcode/plots/ohneFilter13.fig new file mode 100644 index 0000000..45b6eec --- /dev/null +++ b/oldMcode/plots/ohneFilter13.fig Binary files differ diff --git a/oldMcode/plots/prozDiff1.fig b/oldMcode/plots/prozDiff1.fig new file mode 100644 index 0000000..f862267 --- /dev/null +++ b/oldMcode/plots/prozDiff1.fig Binary files differ diff --git a/oldMcode/plots/prozDiff2.fig b/oldMcode/plots/prozDiff2.fig new file mode 100644 index 0000000..8ee7748 --- /dev/null +++ b/oldMcode/plots/prozDiff2.fig Binary files differ diff --git a/oldMcode/plots/prozDiff3.fig b/oldMcode/plots/prozDiff3.fig new file mode 100644 index 0000000..5394b69 --- /dev/null +++ b/oldMcode/plots/prozDiff3.fig Binary files differ diff --git a/oldMcode/plots/prozDiff4.fig b/oldMcode/plots/prozDiff4.fig new file mode 100644 index 0000000..3bcf571 --- /dev/null +++ b/oldMcode/plots/prozDiff4.fig Binary files differ diff --git a/oldMcode/plots/prozDiff5.fig b/oldMcode/plots/prozDiff5.fig new file mode 100644 index 0000000..5635ff3 --- /dev/null +++ b/oldMcode/plots/prozDiff5.fig Binary files differ diff --git a/oldMcode/plots/sig13.fig b/oldMcode/plots/sig13.fig new file mode 100644 index 0000000..68fe997 --- /dev/null +++ b/oldMcode/plots/sig13.fig Binary files differ diff --git a/oldMcode/plots/wlHist.fig b/oldMcode/plots/wlHist.fig new file mode 100644 index 0000000..e3bdf1f --- /dev/null +++ b/oldMcode/plots/wlHist.fig Binary files differ diff --git a/oldMcode/shiftingPwelch.m b/oldMcode/shiftingPwelch.m new file mode 100644 index 0000000..c0dc716 --- /dev/null +++ b/oldMcode/shiftingPwelch.m @@ -0,0 +1,9 @@ +function [EEG] = shiftingPwelch(signal,frequency,window,windowShift,NFFT) + signalShift=frequency*windowShift; + signalWindow=frequency*window; + shiftProp=window/windowShift; + EEG=zeros([fix(floor(size(signal,1)/signalWindow)-1)*shiftProp+1,NFFT/2+1]); + for j=1:fix(floor(size(signal,1)/signalWindow-1)*shiftProp+1) + EEG(j,:)=pwelch(signal((j-1)*signalShift+1:(j-1)*signalShift+signalWindow),[],[],NFFT,2500); + end +end \ No newline at end of file diff --git a/oldMcode/svm.m b/oldMcode/svm.m new file mode 100644 index 0000000..88e36fa --- /dev/null +++ b/oldMcode/svm.m @@ -0,0 +1,44 @@ +close all +clear variables +clc + +load('/home/Uni/masterarbeit/data/AO1200msWindowEMG1sWindowEEG200msShift1sPauseFreq0to200.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/Uni/masterarbeit/plots/cmEMG200ms1sPause.fig','fig'); diff --git a/papers/2015-sarasola-sanz-EMG-based multi-joint kinematics decoding for robot-aided rehabilitation therapies.pdf b/papers/2015-sarasola-sanz-EMG-based multi-joint kinematics decoding for robot-aided rehabilitation therapies.pdf new file mode 100644 index 0000000..077f0c6 --- /dev/null +++ b/papers/2015-sarasola-sanz-EMG-based multi-joint kinematics decoding for robot-aided rehabilitation therapies.pdf Binary files differ diff --git a/papers/2015-shiman-Towards Decoding of Functional Movements from the Same Limb using EEG.pdf b/papers/2015-shiman-Towards Decoding of Functional Movements from the Same Limb using EEG.pdf new file mode 100644 index 0000000..ac26fcf --- /dev/null +++ b/papers/2015-shiman-Towards Decoding of Functional Movements from the Same Limb using EEG.pdf Binary files differ diff --git a/papers/2016-spueler-Comparing Methods for Decoding Movement Trajectory from ECoG in Chronic Stroke Patients.pdf b/papers/2016-spueler-Comparing Methods for Decoding Movement Trajectory from ECoG in Chronic Stroke Patients.pdf new file mode 100644 index 0000000..33278f5 --- /dev/null +++ b/papers/2016-spueler-Comparing Methods for Decoding Movement Trajectory from ECoG in Chronic Stroke Patients.pdf Binary files differ diff --git a/plotChannelDiff.m b/plotChannelDiff.m deleted file mode 100644 index e3e0e01..0000000 --- a/plotChannelDiff.m +++ /dev/null @@ -1,17 +0,0 @@ -function [] = plotChannelDiff( pwMeanMove, pwMeanRest,hz, params ) - -for i=1:size(params.ChannelNames.Value,1) - n=mod(i,9); - if n==1 - figure - elseif n==0; - n=9; - end - subplot(3,3,n); - plot(hz,pwMeanMove(:,i)-pwMeanRest(:,i)) - xlim([0 150]) - title(params.ChannelNames.Value(i)) -end - -end - diff --git a/plotChannelProzDiff.m b/plotChannelProzDiff.m deleted file mode 100644 index b57bd79..0000000 --- a/plotChannelProzDiff.m +++ /dev/null @@ -1,17 +0,0 @@ -function [] = plotChannelProzDiff( pwMeanMove, pwMeanRest,hz, params ) - -for i=1:size(params.ChannelNames.Value,1) - n=mod(i,9); - if n==1 - figure - elseif n==0; - n=9; - end - subplot(3,3,n); - plot(hz,(pwMeanMove(:,i)-pwMeanRest(:,i))./pwMeanRest(:,i)) - xlim([0 150]) - title(params.ChannelNames.Value(i)) -end - -end - diff --git a/plotChannels.m b/plotChannels.m deleted file mode 100644 index 9126ec1..0000000 --- a/plotChannels.m +++ /dev/null @@ -1,17 +0,0 @@ -function [] = plotChannels( pwMean, hz, params ) - -for i=1:size(params.ChannelNames.Value,1) - n=mod(i,9); - if n==1 - figure - elseif n==0; - n=9; - end - subplot(3,3,n); - plot(log(hz),log(pwMean(:,i,:))) - xlim([0 150]) - title(params.ChannelNames.Value(i)) -end - -end - diff --git a/plotImgScProzDiff.m b/plotImgScProzDiff.m deleted file mode 100644 index a1e6b34..0000000 --- a/plotImgScProzDiff.m +++ /dev/null @@ -1,8 +0,0 @@ -function [] = plotImgScProzDiff( pwMeanMove, pwMeanRest) - -prozDiff=(pwMeanMove-pwMeanRest)./pwMeanRest; -imagesc(1:1.22:100,1:32,transpose(prozDiff(1:82,1:32))) -colorbar(); - -end - diff --git a/plotStairs.m b/plotStairs.m deleted file mode 100644 index ee7577a..0000000 --- a/plotStairs.m +++ /dev/null @@ -1,10 +0,0 @@ -function [ ] = plotStairs( inVec ) -%plotStairs plots Stairs for Vector -% plots sum up to Index i as value for i - stair=zeros(size(inVec)); - for i=1:size(classification,1) - stair(i)=sum(inVec(1:i)); - end - plot(stairs) -end - diff --git a/read.m b/read.m deleted file mode 100644 index f7d1925..0000000 --- a/read.m +++ /dev/null @@ -1,42 +0,0 @@ -function readEEG(subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq) - - fprintf('start read %s%i %s\n',subject,number,datestr(datetime('now'))); - - trainingDataEEGcell=cell(maxFile,1); - trainingDataEMGcell=cell(maxFile,1); - classesCell=cell(maxFile,1); - - for i=1:maxFile - [sig, stat, params] = load_bcidat(sprintf('/home/jph/Uni/masterarbeit/Block1_ReachingMovements/%s/%s_B100%i/AO_B1S001R0%i.dat',subject,subject,number,i)); - [trainingDataEEGcell{i},trainingDataEMGcell{i}]=generateTrainingData(sig,windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq); - classesCell{i}=stat.StimulusCode; - fprintf('%ith file processed\n',i) - end - - clear sig - - trainingDataEEG=cell2mat(trainingDataEEGcell); - trainingDataEMG=cell2mat(trainingDataEMGcell); - classesMat=cell2mat(classesCell); - clear trainingDataEEGcell trainingDataEMGcell classesCell - - classificationWithPause=classifyAccordingToEMG(trainingDataEEG, trainingDataEMG,classesMat,shift,params.SamplingRate.NumericValue,threshold); - clear classesMat - - smoothClassification=zeros(size(classificationWithPause)); - for i=1:size(classificationWithPause,1) - smoothClassification(i)=round(mode(classificationWithPause(max(i-2,1):min(i+2,end)))); - end - - clear classificationWithPause - - trainingDataEEG=trainingDataEEG(smoothClassification~=-1,:,:); - trainingDataEMG=trainingDataEMG(smoothClassification~=-1,:); - classification=smoothClassification(smoothClassification~=-1); - - clear smoothClassification i - save(sprintf('/nfs/wsi/ti/messor/hohlochj/matlabData/%s%i200msWindowEMG1sWindowEEG200msShift1sPauseFreq0to200.mat',subject,number),trainingDataEEG,trainingDataEMG,classification); - - - fprintf('end read %s%i %s\n',subject,number,datestr(datetime('now'))); -end \ No newline at end of file diff --git a/readEEGEciton.m b/readEEGEciton.m deleted file mode 100644 index 0a527cb..0000000 --- a/readEEGEciton.m +++ /dev/null @@ -1,42 +0,0 @@ -function readEEGEciton(subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq) - - fprintf('start read %s%i %s\n',subject,number,datestr(datetime('now'))); - - trainingDataEEGcell=cell(maxFile,1); - trainingDataEMGcell=cell(maxFile,1); - classesCell=cell(maxFile,1); - - for i=1:maxFile - [sig, stat, params] = load_bcidat(sprintf('/nfs/wsi/ti/messor/hohlochj/origData/%s/%s_B100%i/%s_B1S00%iR0%i.dat',subject,subject,number,subject,number,i)); - [trainingDataEEGcell{i},trainingDataEMGcell{i}]=generateTrainingData(sig,windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq); - classesCell{i}=stat.StimulusCode; - fprintf('%ith file processed\n',i) - end - - clear sig - - trainingDataEEG=cell2mat(trainingDataEEGcell); - trainingDataEMG=cell2mat(trainingDataEMGcell); - classesMat=cell2mat(classesCell); - clear trainingDataEEGcell trainingDataEMGcell classesCell - - classificationWithPause=classifyAccordingToEMG(trainingDataEEG, trainingDataEMG,classesMat,shift,params.SamplingRate.NumericValue,threshold); - clear classesMat - - smoothClassification=zeros(size(classificationWithPause)); - for i=1:size(classificationWithPause,1) - smoothClassification(i)=round(mode(classificationWithPause(max(i-2,1):min(i+2,end)))); - end - - clear classificationWithPause - - trainingDataEEG=trainingDataEEG(smoothClassification~=-1,:,:); - trainingDataEMG=trainingDataEMG(smoothClassification~=-1,:); - classification=smoothClassification(smoothClassification~=-1); - - clear smoothClassification i - save(sprintf('/nfs/wsi/ti/messor/hohlochj/matlabData/%s%i200msWindowEMG1sWindowEEG200msShift1sPauseFreq0to200.mat',subject,number),'trainingDataEEG','trainingDataEMG','classification','-v7.3'); - - - fprintf('end read %s%i %s\n',subject,number,datestr(datetime('now'))); -end diff --git a/readEciton.m b/readEciton.m deleted file mode 100644 index 205a4ca..0000000 --- a/readEciton.m +++ /dev/null @@ -1,55 +0,0 @@ -close all -clear variables -%params -disp('start') -disp(datestr(datetime('now'))) -windowEMG=0.2; -windowEEG=1; -shift=0.2; -k=10; -maxExpC=5; -maxFile=5; -threshold=7500; -pburgOrder=32; -minEEGFreq=0; %Hz -maxEEGFreq=200; %Hz - - -trainingDataEEGcell=cell(maxFile,1); -trainingDataEMGcell=cell(maxFile,1); -classesCell=cell(maxFile,1); - -parfor i=1:maxFile - [sig, stat, params] = load_bcidat(sprintf('/nfs/wsi/ti/messor/hohlochj/Block1_ReachingMovements/AO/AO_B1001/AO_B1S001R0%i.dat',i)); - [trainingDataEEGcell{i},trainingDataEMGcell{i}]=generateTrainingData(sig,windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq); - classesCell{i}=stat.StimulusCode; - fprintf('%ith file processed\n',i) -end - -clear sig - -trainingDataEEG=cell2mat(trainingDataEEGcell); -trainingDataEMG=cell2mat(trainingDataEMGcell); -classesMat=cell2mat(classesCell); -clear trainingDataEEGcell trainingDataEMGcell classesCell - -classificationWithPause=classifyAccordingToEMG(trainingDataEEG, trainingDataEMG,classesMat,shift,params.SamplingRate.NumericValue,threshold); -clear classesMat - -smoothClassification=zeros(size(classificationWithPause)); -for i=1:size(classificationWithPause,1) - smoothClassification(i)=round(mode(classificationWithPause(max(i-2,1):min(i+2,end)))); -end - -clear classificationWithPause - -trainingDataEEG=trainingDataEEG(smoothClassification~=-1,:,:); -trainingDataEMG=trainingDataEMG(smoothClassification~=-1,:); -classification=smoothClassification(smoothClassification~=-1); - -clear smoothClassification i -save /nfs/wsi/ti/messor/hohlochj/matlabData/AO1200msWindowEMG1sWindowEEG200msShift1sPauseFreq0to200.mat - - -disp('end') -disp(datestr(datetime('now'))) diff --git a/runSvm.bash b/runSvm.bash deleted file mode 100755 index e28cb28..0000000 --- a/runSvm.bash +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -git pull - -DATE=$(date +%y-%m-%d--%T) -matlab -nosplash -nodisplay -nodesktop < svm.m > log/$DATE.log - -git add log/$DATE.log -git commit -m "add log for $DATE" && git pull && git push diff --git a/shiftingPburg.m b/shiftingPburg.m deleted file mode 100644 index e43fbaa..0000000 --- a/shiftingPburg.m +++ /dev/null @@ -1,12 +0,0 @@ -function [ EEG ] = shiftingPburg( signal,frequency,windowEEG,shift,order,minEEGFreq,maxEEGFreq) -%shiftingPburg uses pburg to calculate the power spectrum -% Detailed explanation goes here - signalShift=frequency*shift; - signalWindow=frequency*windowEEG; - shiftProp=windowEEG/shift; - EEG=zeros([fix(floor(size(signal,1)/signalWindow)-1)*shiftProp+1,fix(maxEEGFreq-minEEGFreq+1)]); - for j=1:fix(floor(size(signal,1)/signalWindow-1)*shiftProp+1) - [EEG(j,:),~]=pburg(signal((j-1)*signalShift+1:(j-1)*signalShift+signalWindow),order,minEEGFreq:maxEEGFreq,frequency); - end -end - diff --git a/shiftingPwelch.m b/shiftingPwelch.m deleted file mode 100644 index c0dc716..0000000 --- a/shiftingPwelch.m +++ /dev/null @@ -1,9 +0,0 @@ -function [EEG] = shiftingPwelch(signal,frequency,window,windowShift,NFFT) - signalShift=frequency*windowShift; - signalWindow=frequency*window; - shiftProp=window/windowShift; - EEG=zeros([fix(floor(size(signal,1)/signalWindow)-1)*shiftProp+1,NFFT/2+1]); - for j=1:fix(floor(size(signal,1)/signalWindow-1)*shiftProp+1) - EEG(j,:)=pwelch(signal((j-1)*signalShift+1:(j-1)*signalShift+signalWindow),[],[],NFFT,2500); - end -end \ No newline at end of file diff --git a/stimuliMatrix.txt b/stimuliMatrix.txt deleted file mode 100644 index 091d456..0000000 --- a/stimuliMatrix.txt +++ /dev/null @@ -1,4 +0,0 @@ -BlueGo RedGo GreenGo BrownGo PinkGo REST REST REST REST REST REST REST REST REST REST % -% % % % % % % % % % % % % % % % -sounds\BlueGo.wav sounds\RedGo.wav sounds\GreenGo.wav sounds\BrownGo.wav sounds\PinkGo.wav sounds\Rest.wav sounds\Rest.wav sounds\Rest.wav sounds\Rest.wav sounds\Rest.wav sounds\Rest.wav sounds\Rest.wav sounds\Rest.wav sounds\Rest.wav sounds\Rest.wav % -6000ms 6000ms 6000ms 6000ms 6000ms 2000ms 2100ms 2200ms 2300ms 2400ms 2500ms 2600ms 2700ms 2800ms 2900ms 2000ms diff --git a/structForAllDays.m b/structForAllDays.m deleted file mode 100644 index d8c5f3a..0000000 --- a/structForAllDays.m +++ /dev/null @@ -1,21 +0,0 @@ -function [names,numbers]=structForAllDays(dirs) - fileID=fopen(dirs); - text=textscan(fileID,'%s','Delimiter','\n'); - text=text{1}; - j=0; - k=0; - names=cell(1); - for i=1:size(text) - tmp=char(text(i)); - name=strcat(tmp(1:2)); - if (j~=0 && all(squeeze(names{j}==name))) - k=k+1; - numbers{j}(k)=str2double(tmp(end)); - else - j=j+1; - names{j}=name; - k=1; - numbers{j}(k)=str2double(tmp(end)); - end - end -end \ No newline at end of file diff --git a/svm.m b/svm.m deleted file mode 100644 index 88e36fa..0000000 --- a/svm.m +++ /dev/null @@ -1,44 +0,0 @@ -close all -clear variables -clc - -load('/home/Uni/masterarbeit/data/AO1200msWindowEMG1sWindowEEG200msShift1sPauseFreq0to200.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/Uni/masterarbeit/plots/cmEMG200ms1sPause.fig','fig'); diff --git a/svmEciton.m b/svmEciton.m deleted file mode 100644 index bfb1733..0000000 --- a/svmEciton.m +++ /dev/null @@ -1,52 +0,0 @@ -function svmEciton(subject,number,EEG,k,maxExpC,maxPerClass) - load(sprintf('/nfs/wsi/ti/messor/hohlochj/matlabData/%s%i200msWindowEMG1sWindowEEG200msShift1sPauseFreq0to200.mat',subject,number)); -% fprintf('%i,%i,%i',size(trainingDataEMG,1),size(trainingDataEEG,1),size(classification,1)) - addpath('/nfs/wsi/ti/messor/hohlochj/libsvm/matlab'); - %k=2; - %maxExpC=0; % c\in {2^i|i=-maxExpC:1:maxExpC} - - %choose to estimate based on EEG or EMG - if EEG - trainingData=trainingDataEEG; - else - trainingData=trainingDataEMG; - end - clear trainingDataEEG; - clear trainingDataEMG; -% addAttachedFiles(poolObj,'classification'); -% addAttachedFiles(poolObj,'trainingData'); - - accurancy=zeros(k,3); - maxC=zeros(k,1); - noClasses=size(unique(classification),1); - cm=zeros(noClasses); - randMap=randperm(size(trainingData,1)); - disp('startCV') - classes=classification; %necessary since otherwise classes are not passed to workers - clear classification; - parfor i=1:k - leaveOut=trainingData(mod(randMap,k)==i-1,:,:); - leaveClasses=classes(mod(randMap,k)==i-1); - remaining=trainingData(mod(randMap,k)~=i-1,:,:); - remainingClasses=classes(mod(randMap,k)~=i-1); - fprintf('%s create %ith model\n',datestr(datetime('now')),i) - - [model,maxC(i)]=kfoldCV(remainingClasses,remaining,k,maxExpC,maxPerClass); - disp(datestr(datetime('now'))) - - [predictions,accurancy(i,:),~]=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,sprintf('/nfs/wsi/ti/messor/hohlochj/plots/%s%i%icm200ms1sPause.fig',subject,number,EEG),'fig'); - save(sprintf('/nfs/wsi/ti/messor/hohlochj/matlabData/%s%i%i200ms1sPause.fig',subject,number,EEG),'meanAccurancy','maxC','cmScaled','-v7.3'); -end diff --git a/usedMcode/balanceClasses.m b/usedMcode/balanceClasses.m new file mode 100644 index 0000000..54e7ba0 --- /dev/null +++ b/usedMcode/balanceClasses.m @@ -0,0 +1,20 @@ +function [trainClasses,trainData]=balanceClasses(trainClasses,trainData,maxPerClass,noClasses) +% if maxPerClass > min(histcounts(trainClasses,noClasses)) +% maxPerClass=min(histcounts(trainClasses)); +% warning('not enough Data per Class; set maxPerClass to %i', maxPerClass); +% end + i=1; + delPerClass=histcounts(trainClasses,noClasses)-maxPerClass; + delIndex=zeros(sum(delPerClass),1); + while sum(delPerClass)>0 + r=fix(size(trainClasses,1)*rand(1)+1); + if delPerClass(trainClasses(r)+1)>0 && all(delIndex~=r) + delIndex(i)=r; + i=i+1; + delPerClass(trainClasses(r)+1)=delPerClass(trainClasses(r)+1)-1; + end + end + trainClasses(delIndex)=[]; + trainData(delIndex,:)=[]; + assert(size(trainClasses,1)>0) +end \ No newline at end of file diff --git a/usedMcode/callAll.m b/usedMcode/callAll.m new file mode 100644 index 0000000..aceaf65 --- /dev/null +++ b/usedMcode/callAll.m @@ -0,0 +1,27 @@ +addpath('/home/hohlochj/masterarbeit') +pathToFile='/nfs/wsi/ti/messor/hohlochj/origData/dirs.txt'; +% pathToFile='/home/jph/Uni/masterarbeit/Block1_ReachingMovements/dirs.txt'; + +windowEMG=0.2; +windowEEG=1; +shift=0.2; +pburgOrder=50; +minEEGFreq=0; +maxEEGFreq=200; +k=10; +maxExpC=0; +maxPerClass=250; +EEG=true; +poolObj=parpool(32); + +[subjects,numbers]=structForAllDays(pathToFile); +for i=1:size(subjects,2) + subject=subjects{i}; + for number=numbers{i} +% fprintf('%s%i\n',subject,number); + readEEGEciton(subject,number,windowEMG,windowEEG,shift,5,7500,pburgOrder,minEEGFreq,maxEEGFreq); + svmEciton(subject,number,false,k,maxExpC,maxPerClass); + svmEciton(subject,number,EEG,k,maxExpC,maxPerClass); + end +end +delete(poolObj) diff --git a/usedMcode/chooseForCode.m b/usedMcode/chooseForCode.m new file mode 100644 index 0000000..aa15422 --- /dev/null +++ b/usedMcode/chooseForCode.m @@ -0,0 +1,12 @@ +function [ splitSamplesCode ] = chooseForCode(code,codes, splitSamples) +%chooseForCode chooses the samples for given code +% input the code to choose, the list of codes, the number of Samples and +% the list of Samples to get the list with the given code + splitSamplesCode=[]; + for i=1:size(codes) + if code == codes(i) + splitSamplesCode=[splitSamplesCode,splitSamples(:,:,i)]; + end + end +end + diff --git a/usedMcode/classifyAccordingToEMG.m b/usedMcode/classifyAccordingToEMG.m new file mode 100644 index 0000000..4937188 --- /dev/null +++ b/usedMcode/classifyAccordingToEMG.m @@ -0,0 +1,26 @@ +function [classification]=classifyAccordingToEMG(trainingDataEEG, trainingDataEMG, classes,shift,frequency,threshold) +% classifyAccordingToEMG data classified as -1 should be taken out + classification=zeros([size(trainingDataEEG,1),1]); + oldclass=0; + for i=1:size(trainingDataEEG,1) + if mean(trainingDataEMG(i,:)) > threshold % is movement + class=mode(classes(max(round(shift*frequency*i-frequency),1):min(round(i*shift*frequency-1),end))); + if class <= 5 % task is to move + if oldclass==0 + for j=max(fix(i-1/shift),1):fix(i-0.5/shift) % Pause before beginning of movement + classification(j)=-1; %code for pause + end + for j=max(fix(i-0.5/shift),1):i % half a second before executed movement there should be activity (esp. EEG) + classification(j)=-1; %if -1 there is a pause of 1s befor movement onset + end + oldclass=class; + end + classification(i)=class; + else % task is to rest but movement not finished + classification(i)=oldclass; + end + else + oldclass=0; + end + end +end \ No newline at end of file diff --git a/usedMcode/generateTrainingData.m b/usedMcode/generateTrainingData.m new file mode 100644 index 0000000..6cc8601 --- /dev/null +++ b/usedMcode/generateTrainingData.m @@ -0,0 +1,23 @@ +function [trainingDataEEG,trainingDataEMG] = generateTrainingData(signal,windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq) + frequency=params.SamplingRate.NumericValue; + signalWindowEMG=frequency*windowEMG; + signalWindowEEG=frequency*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]/(frequency/2),'stop'); + [C,D]= butter(2,1/(frequency/2),'high'); + [E,F]= butter(2,[148 152]/(frequency/2),'stop'); + + parfor i=1:32 %filter single channel, w/o 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))))),frequency,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(filter(C,D,filter(A,B,signal(:,i))),frequency,windowEMG,shift); + end + trainingDataEMG=permute(tempEMG,[2 1 3]); +end diff --git a/usedMcode/kfoldCV.m b/usedMcode/kfoldCV.m new file mode 100644 index 0000000..94f27d8 --- /dev/null +++ b/usedMcode/kfoldCV.m @@ -0,0 +1,32 @@ +function [model,maxC] = kfoldCV(classification,trainingData,k,cExpMax,maxDataPerClass) + noClasses=size(unique(classification),1); +if cExpMax>0 %CV only if necessary + cvAccurancy=zeros(2*cExpMax+1,1); + parfor cExp=1:2*cExpMax+1 + c=2^(cExp-cExpMax-1); + + randomMapping=transpose(randperm(size(trainingData,1))); + accurancy=zeros(k,3); + + for i=1:k + trainData=trainingData(mod(randomMapping,k)+1~=i,:,:); + testData=trainingData(mod(randomMapping,k)+1==i,:,:); + trainClasses=classification(mod(randomMapping,k)+1~=i); + testClasses=classification(mod(randomMapping,k)+1==i); + %fprintf('i=%i, k=%i, c=%i\n',i,k,c) + [trainClasses,trainData]=balanceClasses(trainClasses,trainData,maxDataPerClass,noClasses); + model=svmtrain(trainClasses,trainData(:,:),sprintf('-t 0 -c %f -q',c)); + [~, accurancy(i,:), ~]=svmpredict(testClasses, testData(:,:), model,'-q'); + end + + cvAccurancy(cExp)=mean(accurancy(:,1)); + end + [~,maxC]=max(cvAccurancy); +else + maxC=1; %no gridsearch since only one C possible +end + bestC=2^(maxC-cExpMax-1); + [balancedClasses, balanceData]=balanceClasses(classification,trainingData,maxDataPerClass,noClasses); + model=svmtrain(balancedClasses, balanceData(:,:),sprintf('-t 0 -c %f -q',bestC)); + +end diff --git a/usedMcode/readEEG.m b/usedMcode/readEEG.m new file mode 100644 index 0000000..f7d1925 --- /dev/null +++ b/usedMcode/readEEG.m @@ -0,0 +1,42 @@ +function readEEG(subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq) + + fprintf('start read %s%i %s\n',subject,number,datestr(datetime('now'))); + + trainingDataEEGcell=cell(maxFile,1); + trainingDataEMGcell=cell(maxFile,1); + classesCell=cell(maxFile,1); + + for i=1:maxFile + [sig, stat, params] = load_bcidat(sprintf('/home/jph/Uni/masterarbeit/Block1_ReachingMovements/%s/%s_B100%i/AO_B1S001R0%i.dat',subject,subject,number,i)); + [trainingDataEEGcell{i},trainingDataEMGcell{i}]=generateTrainingData(sig,windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq); + classesCell{i}=stat.StimulusCode; + fprintf('%ith file processed\n',i) + end + + clear sig + + trainingDataEEG=cell2mat(trainingDataEEGcell); + trainingDataEMG=cell2mat(trainingDataEMGcell); + classesMat=cell2mat(classesCell); + clear trainingDataEEGcell trainingDataEMGcell classesCell + + classificationWithPause=classifyAccordingToEMG(trainingDataEEG, trainingDataEMG,classesMat,shift,params.SamplingRate.NumericValue,threshold); + clear classesMat + + smoothClassification=zeros(size(classificationWithPause)); + for i=1:size(classificationWithPause,1) + smoothClassification(i)=round(mode(classificationWithPause(max(i-2,1):min(i+2,end)))); + end + + clear classificationWithPause + + trainingDataEEG=trainingDataEEG(smoothClassification~=-1,:,:); + trainingDataEMG=trainingDataEMG(smoothClassification~=-1,:); + classification=smoothClassification(smoothClassification~=-1); + + clear smoothClassification i + save(sprintf('/nfs/wsi/ti/messor/hohlochj/matlabData/%s%i200msWindowEMG1sWindowEEG200msShift1sPauseFreq0to200.mat',subject,number),trainingDataEEG,trainingDataEMG,classification); + + + fprintf('end read %s%i %s\n',subject,number,datestr(datetime('now'))); +end \ No newline at end of file diff --git a/usedMcode/readEEGEciton.m b/usedMcode/readEEGEciton.m new file mode 100644 index 0000000..0a527cb --- /dev/null +++ b/usedMcode/readEEGEciton.m @@ -0,0 +1,42 @@ +function readEEGEciton(subject,number,windowEMG,windowEEG,shift,maxFile,threshold,pburgOrder,minEEGFreq,maxEEGFreq) + + fprintf('start read %s%i %s\n',subject,number,datestr(datetime('now'))); + + trainingDataEEGcell=cell(maxFile,1); + trainingDataEMGcell=cell(maxFile,1); + classesCell=cell(maxFile,1); + + for i=1:maxFile + [sig, stat, params] = load_bcidat(sprintf('/nfs/wsi/ti/messor/hohlochj/origData/%s/%s_B100%i/%s_B1S00%iR0%i.dat',subject,subject,number,subject,number,i)); + [trainingDataEEGcell{i},trainingDataEMGcell{i}]=generateTrainingData(sig,windowEMG,windowEEG,shift,params,pburgOrder,minEEGFreq,maxEEGFreq); + classesCell{i}=stat.StimulusCode; + fprintf('%ith file processed\n',i) + end + + clear sig + + trainingDataEEG=cell2mat(trainingDataEEGcell); + trainingDataEMG=cell2mat(trainingDataEMGcell); + classesMat=cell2mat(classesCell); + clear trainingDataEEGcell trainingDataEMGcell classesCell + + classificationWithPause=classifyAccordingToEMG(trainingDataEEG, trainingDataEMG,classesMat,shift,params.SamplingRate.NumericValue,threshold); + clear classesMat + + smoothClassification=zeros(size(classificationWithPause)); + for i=1:size(classificationWithPause,1) + smoothClassification(i)=round(mode(classificationWithPause(max(i-2,1):min(i+2,end)))); + end + + clear classificationWithPause + + trainingDataEEG=trainingDataEEG(smoothClassification~=-1,:,:); + trainingDataEMG=trainingDataEMG(smoothClassification~=-1,:); + classification=smoothClassification(smoothClassification~=-1); + + clear smoothClassification i + save(sprintf('/nfs/wsi/ti/messor/hohlochj/matlabData/%s%i200msWindowEMG1sWindowEEG200msShift1sPauseFreq0to200.mat',subject,number),'trainingDataEEG','trainingDataEMG','classification','-v7.3'); + + + fprintf('end read %s%i %s\n',subject,number,datestr(datetime('now'))); +end diff --git a/usedMcode/shiftingPburg.m b/usedMcode/shiftingPburg.m new file mode 100644 index 0000000..e43fbaa --- /dev/null +++ b/usedMcode/shiftingPburg.m @@ -0,0 +1,12 @@ +function [ EEG ] = shiftingPburg( signal,frequency,windowEEG,shift,order,minEEGFreq,maxEEGFreq) +%shiftingPburg uses pburg to calculate the power spectrum +% Detailed explanation goes here + signalShift=frequency*shift; + signalWindow=frequency*windowEEG; + shiftProp=windowEEG/shift; + EEG=zeros([fix(floor(size(signal,1)/signalWindow)-1)*shiftProp+1,fix(maxEEGFreq-minEEGFreq+1)]); + for j=1:fix(floor(size(signal,1)/signalWindow-1)*shiftProp+1) + [EEG(j,:),~]=pburg(signal((j-1)*signalShift+1:(j-1)*signalShift+signalWindow),order,minEEGFreq:maxEEGFreq,frequency); + end +end + diff --git a/usedMcode/structForAllDays.m b/usedMcode/structForAllDays.m new file mode 100644 index 0000000..d8c5f3a --- /dev/null +++ b/usedMcode/structForAllDays.m @@ -0,0 +1,21 @@ +function [names,numbers]=structForAllDays(dirs) + fileID=fopen(dirs); + text=textscan(fileID,'%s','Delimiter','\n'); + text=text{1}; + j=0; + k=0; + names=cell(1); + for i=1:size(text) + tmp=char(text(i)); + name=strcat(tmp(1:2)); + if (j~=0 && all(squeeze(names{j}==name))) + k=k+1; + numbers{j}(k)=str2double(tmp(end)); + else + j=j+1; + names{j}=name; + k=1; + numbers{j}(k)=str2double(tmp(end)); + end + end +end \ No newline at end of file diff --git a/usedMcode/svmEciton.m b/usedMcode/svmEciton.m new file mode 100644 index 0000000..992191a --- /dev/null +++ b/usedMcode/svmEciton.m @@ -0,0 +1,47 @@ +function svmEciton(subject,number,EEG,k,maxExpC,maxPerClass) + load(sprintf('/nfs/wsi/ti/messor/hohlochj/matlabData/%s%i200msWindowEMG1sWindowEEG200msShift1sPauseFreq0to200.mat',subject,number)); +% fprintf('%i,%i,%i',size(trainingDataEMG,1),size(trainingDataEEG,1),size(classification,1)) + addpath('/nfs/wsi/ti/messor/hohlochj/libsvm/matlab'); + + %choose to estimate based on EEG or EMG + if EEG + trainingData=trainingDataEEG; + else + trainingData=trainingDataEMG; + end + 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') + classes=classification; %necessary since otherwise classes are not passed to workers + clear classification; + parfor i=1:k + leaveOut=trainingData(mod(randMap,k)==i-1,:,:); + leaveClasses=classes(mod(randMap,k)==i-1); + remaining=trainingData(mod(randMap,k)~=i-1,:,:); + remainingClasses=classes(mod(randMap,k)~=i-1); + fprintf('%s create %ith model\n',datestr(datetime('now')),i) + + [model,maxC(i)]=kfoldCV(remainingClasses,remaining,k,maxExpC,maxPerClass); + disp(datestr(datetime('now'))) + + [predictions,accurancy(i,:),~]=svmpredict(leaveClasses,leaveOut(:,:),model); + cm=cm+confusionmat(leaveClasses,predictions); %confusion matrix + 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,sprintf('/nfs/wsi/ti/messor/hohlochj/plots/%s%i%icm200ms1sPause.fig',subject,number,EEG),'fig'); + save(sprintf('/nfs/wsi/ti/messor/hohlochj/matlabData/%s%i%i200ms1sPause.fig',subject,number,EEG),'meanAccurancy','maxC','cmScaled','-v7.3'); +end diff --git a/usedMcode/waveformLength.m b/usedMcode/waveformLength.m new file mode 100644 index 0000000..3b927f3 --- /dev/null +++ b/usedMcode/waveformLength.m @@ -0,0 +1,12 @@ +function [WL]=waveformLength(signal,frequency,window,windowShift) +%waveformLength computes waveformLength of signal with given frequency, window and +%shift + signalShift=frequency*windowShift; + signalWindow=frequency*window; + shiftProp=window/windowShift; + WL=zeros((floor(size(signal,1)/signalWindow)-1)*shiftProp+1,1); + for j=1:fix(floor(size(signal,1)/signalWindow-1)*shiftProp+1) + WL(j)=sum(abs(diff(signal((j-1)*signalShift+1:(j-1)*signalShift+signalWindow)))); + end +end + diff --git a/waveformLength.m b/waveformLength.m deleted file mode 100644 index 3b927f3..0000000 --- a/waveformLength.m +++ /dev/null @@ -1,12 +0,0 @@ -function [WL]=waveformLength(signal,frequency,window,windowShift) -%waveformLength computes waveformLength of signal with given frequency, window and -%shift - signalShift=frequency*windowShift; - signalWindow=frequency*window; - shiftProp=window/windowShift; - WL=zeros((floor(size(signal,1)/signalWindow)-1)*shiftProp+1,1); - for j=1:fix(floor(size(signal,1)/signalWindow-1)*shiftProp+1) - WL(j)=sum(abs(diff(signal((j-1)*signalShift+1:(j-1)*signalShift+signalWindow)))); - end -end -