clc 
clear all

prompt = {'Subject Number:','Affected Arm (1) for L, (2) for R'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'S01','2'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);    
arm = str2double(answer(2,1));

%% Control Baseline Movement

if arm == 1
    [NUM] = xlsread('S00_SRT', 'Left');
    [NUMR] = xlsread('S00_SRT_Rapid','Left');
else
    [NUM] = xlsread('S00_SRT','Right');
    [NUMR] = xlsread('S00_SRT_Rapid','Right');
end

[PKS,LOCS]=findpeaks(NUM(:,3),'MinPeakProminence',300);

FrC = [LOCS(1) LOCS(2)-LOCS(1) LOCS(3)-LOCS(2) LOCS(4)-LOCS(3) ...
    LOCS(5)-LOCS(4) LOCS(6)-LOCS(5) LOCS(7)-LOCS(6) LOCS(8)-LOCS(7) ...
     LOCS(9)-LOCS(8) LOCS(10)-LOCS(9)];
Avg_TC = mean(FrC)*(10/3);
DimC = round(mean(FrC));

CON{1} = zeros(max(FrC),4); 
CON{1}(1:LOCS(1),1:size(NUM,2)) = NUM(1:LOCS(1),1:size(NUM,2));
for i = 2:size(LOCS,1)-1
    CON{i} = zeros(max(FrC),4); 
    CON{i}(1:LOCS(i)-LOCS(i-1)+1,1:size(NUM,2)) = NUM(LOCS(i-1):LOCS(i),1:size(NUM,2));
end
CON{10} = zeros(max(FrC),4); 
CON{10}(1:LOCS(10)-LOCS(9),1:size(NUM,2)) = NUM(LOCS(9):LOCS(10)-1,1:size(NUM,2));

StackC = cat(3,CON{1},CON{2},CON{3},CON{4},CON{5},CON{6},CON{7},CON{8},CON{9},CON{10});

% figure(1)
% plot(NUM(:,1),NUM(:,3),'b*')
% hold on
% plot(CON{1}(:,1),CON{1}(:,3),'ro')
% plot(CON{4}(:,1),CON{4}(:,3),'ko')
% hold off

StackC(StackC == 0) = NaN;
MMC = nanmean(StackC,3);
SDMC = std(StackC,[],3);

XC = [MMC(:,2)];
YC = [MMC(:,3)];
ZC = [MMC(:,4)];

FrameC = linspace(0,Avg_TC,length(XC));

clear PKS LOCS

%% Control Rapid Movement

[PKS,LOCS]=findpeaks(NUMR(:,3),'MinPeakProminence',300);

FrCR = [LOCS(1) LOCS(2)-LOCS(1) LOCS(3)-LOCS(2) LOCS(4)-LOCS(3) ...
    LOCS(5)-LOCS(4) LOCS(6)-LOCS(5) LOCS(7)-LOCS(6) LOCS(8)-LOCS(7) ...
     LOCS(9)-LOCS(8) LOCS(10)-LOCS(9)];
Avg_TCR = mean(FrCR)*(10/3);
DimCR = round(mean(FrCR));

CONR{1} = zeros(max(FrCR),4); 
CONR{1}(1:LOCS(1),1:size(NUMR,2)) = NUMR(1:LOCS(1),1:size(NUMR,2));
for i = 2:size(LOCS,1)-1
    CONR{i} = zeros(max(FrCR),4); 
    CONR{i}(1:LOCS(i)-LOCS(i-1)+1,1:size(NUMR,2)) = NUMR(LOCS(i-1):LOCS(i),1:size(NUMR,2));
end
CONR{10} = zeros(max(FrCR),4); 
CONR{10}(1:LOCS(10)-LOCS(9),1:size(NUMR,2)) = NUMR(LOCS(9):LOCS(10)-1,1:size(NUMR,2));

CONR{7}(end,:)=[];

StackCR = cat(3,CONR{1},CONR{2},CONR{3},CONR{4},CONR{5},...
    CONR{6},CONR{7},CONR{8},CONR{9},CONR{10});

% figure(2)
% plot(NUMR(:,1),NUMR(:,3),'b*')
% hold on
% plot(CONR{1}(:,1),CONR{1}(:,3),'ro')
% plot(CONR{4}(:,1),CONR{4}(:,3),'ko')
% hold off

StackCR(StackCR == 0) = NaN;
MMCR = nanmean(StackCR,3);
SDMCR = std(StackCR,[],3);

XCR = [MMCR(:,2)];
YCR = [MMCR(:,3)];
ZCR = [MMCR(:,4)];

FrameCR = linspace(0,Avg_TCR,length(XCR));

clear PKS LOCS

% %% Individual Control Baseline
% 
% if isfile(char(strcat(answer(1,1),'_SRT_NP2')))
%     [Indiv] = [xlsread(char(strcat(answer(1,1),'_SRT_NP1'))) ; ...
%     xlsread(char(strcat(answer(1,1),'_SRT_NP2')))];
% else
%     [Indiv] = xlsread(char(strcat(answer(1,1),'_SRT_NP1')));
% end
%     
% if arm == 1
%     [PKS,LOCS]=findpeaks(Indiv(:,3),'MinPeakProminence',300);
%     [DPS,DLOCS]=findpeaks(-Indiv(:,3),'MinPeakProminence',300);
% else
%     [PKS,LOCS]=findpeaks(Indiv(:,5),'MinPeakProminence',0);
%     [DPS,DLOCS]=findpeaks(-Indiv(:,5),'MinPeakProminence',0);
% end
% 
% FrCI = [LOCS(1) LOCS(2)-LOCS(1) LOCS(3)-LOCS(2) LOCS(4)-LOCS(3) ...
%     LOCS(5)-LOCS(4) LOCS(6)-LOCS(5) LOCS(7)-LOCS(6) LOCS(8)-LOCS(7) ...
%      LOCS(9)-LOCS(8) LOCS(10)-LOCS(9)];
% Avg_TCI = mean(FrCI)*(10/3);
% DimCI = round(mean(FrCI));
% 
% CONI{1} = zeros(max(FrCI),4); 
% CONI{1}(1:LOCS(1),1:size(Indiv,2)) = Indiv(1:LOCS(1),1:size(Indiv,2));
% for i = 2:size(LOCS,1)-1
%     CONI{i} = zeros(max(FrCI),4); 
%     CONI{i}(1:LOCS(i)-LOCS(i-1)+1,1:size(Indiv,2)) = Indiv(DLOCS(i-1):LOCS(i),1:size(Indiv,2));
% end
% CONI{10} = zeros(max(FrCI),4); 
% CONI{10}(1:LOCS(10)-LOCS(9),1:size(Indiv,2)) = Indiv(DLOCS(9):LOCS(10)-1,1:size(Indiv,2));
% 
% %CONI{7}(end,:)=[];
% 
% StackCI = cat(3,CONI{1},CONI{2},CONI{3},CONI{4},CONI{5},...
%     CONI{6},CONI{7},CONI{8},CONI{9},CONI{10});
% 
% % figure(2)
% % plot(NUMR(:,1),NUMR(:,3),'b*')
% % hold on
% % plot(CONR{1}(:,1),CONR{1}(:,3),'ro')
% % plot(CONR{4}(:,1),CONR{4}(:,3),'ko')
% % hold off
% 
% StackCI(StackCI == 0) = NaN;
% MMCI = nanmean(StackCI,3);
% SDMCI = std(StackCI,[],3);
% 
% XCI = [MMCI(:,2)];
% YCI = [MMCI(:,3)];
% ZCI = [MMCI(:,4)];
% 
% FrameCI = linspace(0,Avg_TCI,length(XCI));
% 
% clear PKS LOCS DPS DLOCS

%% Subject PIH and SRT

%Passive Ideal Hand Path

filename = char(strcat(answer(1,1),'_PIH_P1'));
filename2 = char(strcat(answer(1,1),'_PIH_P2'));

if isfile(filename2)
    [NUMP] = [xlsread(filename); xlsread(filename2)];
else
    [NUMP] = xlsread(filename);
end

if arm == 1
    [PKS,LOCS]=findpeaks(NUMP(:,3));
    [DPS,DLOCS]=findpeaks(-NUMP(:,3));
    
    FrCP = [LOCS(1) LOCS(2)];
    Avg_TCP = mean(FrCP)*(10/3);
    DimCP = round(mean(FrCP));
    
CONP{1} = zeros(max(FrCP),7); 
CONP{1}(1:LOCS(1),1:size(NUMP,2)) = NUMP(1:LOCS(1),1:size(NUMP,2));
CONP{2} = zeros(max(FrCP),7); 
CONP{2}(1:LOCS(2)-LOCS(1)+1,1:size(NUMP,2)) = NUMP(DLOCS(1):LOCS(2),1:size(NUMP,2));

StackCP = cat(3,CONP{1},CONP{2});

StackCP(StackCP == 0) = NaN;
MMCP = nanmean(StackCP,3);
SDMCP = std(StackCP,[],3);

XCP = [MMCP(:,2)];
YCP = [MMCP(:,3)];
ZCP = [MMCP(:,4)];

FrameCP = linspace(0,Avg_TCP,length(XCP)); 
    
else
    [PKS,LOCS]=findpeaks(NUMP(:,5));
    [DPS,DLOCS]=findpeaks(-NUMP(:,5));
    
    FrCP = [LOCS(1) LOCS(2)];
    Avg_TCP = mean(FrCP)*(10/3);
    DimCP = round(mean(FrCP));
    
CONP{1} = zeros(max(FrCP),7); 
CONP{1}(1:LOCS(1),1:size(NUMP,2)) = NUMP(1:LOCS(1),1:size(NUMP,2));
CONP{2} = zeros(max(FrCP),7); 
CONP{2}(1:LOCS(2)-LOCS(1)-1,1:size(NUMP,2)) = NUMP(DLOCS(1):LOCS(2),1:size(NUMP,2));

StackCP = cat(3,CONP{1},CONP{2});

StackCP(StackCP == 0) = NaN;
MMCP = nanmean(StackCP,3);
SDMCP = std(StackCP,[],3);

XCP = [MMCP(:,5)];
YCP = [MMCP(:,6)];
ZCP = [MMCP(:,7)];

FrameCP = linspace(0,Avg_TCP,length(XCP)); 

end

clear PKS LOCS DPS DLOCS
    
%Simple Reaching Test

filename3 = char(strcat(answer(1,1),'_SRT_P1'));
filename4 = char(strcat(answer(1,1),'_SRT_P2'));

if isfile(filename2)
    [NUMT] = [xlsread(filename3); xlsread(filename4)];
else
    [NUMT] = xlsread(filename3);
end

if arm == 1
    [PKS,LOCS]=findpeaks(NUMT(:,3),'MinPeakProminence',300);
    [DPS,DLOCS]=findpeaks(-NUMT(:,3),'MinPeakProminence',300);
    
    FrCT = [LOCS(1)];
    for i = 2:size(LOCS,1)
        FrCT = [FrCT LOCS(i)-LOCS(i-2)];
        Avg_TCT = mean(FrCT)*(10/3);
        DimCT = round(mean(FrCT));
    end
    
CONT{1} = zeros(max(FrCT),7); 
CONT{1}(1:LOCS(1),1:size(NUMT,2)) = NUMT(1:LOCS(1),1:size(NUMT,2));
for i = 2:size(LOCS,1)-1
    CONT{i} = zeros(max(FrCT),7); 
    CONT{i}(1:LOCS(i)-LOCS(i-1)+1,1:size(NUMT,2)) = NUMT(DLOCS(i-1):LOCS(i),1:size(NUMT,2));
end
CONT{size(LOCS,1)} = zeros(max(FrCT),7); 
CONT{size(LOCS,1)}(1:LOCS(size(LOCS,1))-LOCS(size(LOCS,1)-1),1:size(NUMT,2)) = NUMT(DLOCS(size(LOCS,1)-1):LOCS(size(LOCS,1))-1,1:size(NUMT,2));

%CONI{7}(end,:)=[];

StackCT = cat(3,CONT{1});
for i = 1:size(LOCS,1)
StackCT = cat(3,StackCT,CONT{i});
end

StackCT(StackCT == 0) = NaN;
MMCT = nanmean(StackCT,3);
SDMCT = std(StackCT,[],3);

XCT = [MMCT(:,2)];
YCT = [MMCT(:,3)];
ZCT = [MMCT(:,4)];

FrameCT = linspace(0,Avg_TCT,length(XCT)); 
    
else
    Fs = 100;
    [PKS,LOCS]=findpeaks(NUMT(:,5), Fs);
    [DPS,DLOCS]=findpeaks(-NUMT(:,5), Fs);
    
        FrCT = [LOCS(1)];
    for i = 2:size(LOCS,1)
        FrCT = [FrCT LOCS(i)-LOCS(i-1)];
        Avg_TCT = mean(FrCT)*(10/3);
        DimCT = round(mean(FrCT));
    end
    
CONT{1} = zeros((round(max(FrCT))),7); 
CONT{1}(1:LOCS(1),1:size(NUMT,2)) = NUMT(1:LOCS(1),1:size(NUMT,2));
for i = 2:size(LOCS,1)-1
    CONT{i} = zeros((round(max(FrCT))),7); 
    CONT{i}(1:LOCS(i)-LOCS(i-1)+1,1:size(NUMT,2)) = NUMT(DLOCS(i-1):LOCS(i),1:size(NUMT,2));
end
CONT{size(LOCS,1)} = zeros(max(FrCT),7); 
CONT{size(LOCS,1)}(1:LOCS(size(LOCS,1))-LOCS(size(LOCS,1)-1),1:size(NUMT,2)) = NUMT(DLOCS(size(LOCS,1)-1):LOCS(size(LOCS,1))-1,1:size(NUMT,2));

%CONI{7}(end,:)=[];

StackCT = cat(3,CONT{1});
for i = 1:size(LOCS,1)
StackCT = cat(3,StackCT,CONT{i});
end

StackCT(StackCT == 0) = NaN;
MMCT = nanmean(StackCT,3);
SDMCT = std(StackCT,[],3);

XCT = [MMCT(:,5)];
YCT = [MMCT(:,6)];
ZCT = [MMCT(:,7)];

FrameCT = linspace(0,Avg_TCT,length(XCT)); 
end

clear PKS LOCS DPS DLOCS
%% Procrustes Method

%PIH vs. Univ Control to show no similarities in kinematics
%PIH vs. SRT to show altered or dysfunctional movement

%Univ Control vs. SRT to show preserved movement
%Indiv Control vs. SRT to show Indiv preserved movement
%Univ Control vs Indiv Control to show unappreciable difference

%% Paper 2
%Demonstrate tiredness effects of two initial and two final curves with procrustes overlay
%Demonstrate effects of arm dominance
%Demonstrate emergence of synergies and compensation

