%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all; close all; clc;
addpath '~/Dropbox/harvard/processMOT/';  
addpath '~/Desktop/export_fig';

save_it = 1;

% List all subjects, muscles:
subjects = {'AD052611';'AM062411';'EM072611';'DS100611';'DH121412';'MC121812';'BC011113';'DC011413';'JB011613'};

%subjects = {'DH121412';'MC121812';'BC011113';'DC011413';'JB011613'};
speeds   = {'075';'100';'125';'150';'175';'200'};
muscles  = {'TA';'SOL';'GAS';'VAS';'BFSH';'RF';'SMEM';'BFLH';'ILL';'GLU';'GLUU';'GMED';'ADDL';'ADDM'};
methods  = {'low100_mvc_lo';'sang_mvc_lo'};

% List muscles that are "unreasonable" for each participant:
makeListing;

colors = [1 .6 0; 0 .5 .2; 0 0 1; 1 0 0; .5 0 1; 0 .5 1];% 0 0 0; .1 .6 0;1 .9 0; 1 0 1;.4 .4 .4];
numpts = 101;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

% What about normalization?  Don't think renormalization is appropriate.
% Can probably get away with just putting in mean trajectories
% where re-normalization would be wanted.

for i = 1:length(methods)
  for j = 1:length(speeds)
    for m = 1:length(muscles)
      eval(['om_a' muscles{m} ' = [];']);
      eval(['om_s' muscles{m} ' = [];']);
      eval(['osd_a' muscles{m} ' = [];']);  
      eval(['osd_s' muscles{m} ' = [];']);  
    end;
    clear m_a* m_s* sd_a* sd_s*
    for k = 1:length(subjects)
      eval(['num_muscles = length(' subjects{k} '.good_act_walk' speeds{j} ');']); 
      eval(['load ''~/Documents/MATLAB/data/' subjects{k} '/processed/EMG/avg_act_thresh/' methods{i} '/walk' speeds{j} '.mat'';']);  
      for mm = 1:num_muscles
         eval(['mus = ' subjects{k} '.good_act_walk' speeds{j} '{mm};']); 
         eval(['om_a' mus ' = cat(1,om_a' mus  ',m_a' mus ');']);
         eval(['om_s' mus ' = cat(1,om_s' mus  ',m_s' mus ');']);
         eval(['osd_a' mus ' = cat(1,osd_a' mus  ',sd_a' mus ');']);
         eval(['osd_s' mus ' = cat(1,osd_s' mus  ',sd_s' mus ');']);
      end;
    end;
    clear m_a* m_s* sd_a* sd_s*
    for mmm = 1:length(muscles)
      mus = muscles{mmm};
      eval(['j_m_a' mus ' = mean(om_a' mus ',1);']);
      eval(['j_m_s' mus ' = mean(om_s' mus ',1);']);
      %eval(['j_sd_a' mus ' = std(om_a' mus ',0,1);']);
      %eval(['j_sd_s' mus ' = std(om_s' mus ',0,1);']);
      eval(['j_sd_a' mus ' = sqrt(sum(osd_a' mus '.^2,1))/size(osd_a' mus ',1);']);      
      eval(['j_sd_s' mus ' = sqrt(sum(osd_s' mus '.^2,1))/size(osd_s' mus ',1);']);            
      %eval(['figure(' num2str(mmm+(i-1)*40) ');']);
      eval(['figure(' num2str(1+(i-1)*2) ');']);
      eval(['subplot(3,5,' num2str(mmm) ');']);
      eval(['niceBars(0:(numpts-1),j_m_a' mus ',j_sd_a' mus ',[' num2str(colors(j,:)) '],0.5);']);
      hold on;  axis tight;
      %xlabel('Pct. GC','fontsize',12);
      %ylabel('Act.','fontsize',12);
      eval(['title(''' mus ''',''fontsize'',12);']);
      %xlabel('Percent GC','fontsize',14);
      %ylabel('Activation','fontsize',14);
      %eval(['title(''' mus ' Average Activation (' methods{i}(1:end-7) ')'',''fontsize'',14);']);
      %eval(['figure(' num2str(mmm+20+(i-1)*40) ');']);
      eval(['figure(' num2str(2+(i-1)*2) ');']);
      eval(['subplot(3,5,' num2str(mmm) ');']);
      eval(['niceBars(0:(numpts-1),j_m_s' mus ',j_sd_s' mus ',[' num2str(colors(j,:)) '],0.5);']);
      hold on;  axis tight;
      %xlabel('Pct GC','fontsize',12);
      %ylabel('Exc','fontsize',12);
      eval(['title(''' mus ''',''fontsize'',12);']);
      %xlabel('Percent GC','fontsize',14);
      %ylabel('Excitation','fontsize',14);
      %eval(['title(''' mus ' Average Excitation (' methods{i}(1:end-7) ')'',''fontsize'',14);']);
    end;
    if save_it == 1
      path_out = ['~/Documents/MATLAB/data/avgEMG/' methods{i}];
      if exist(path_out) == 0
        eval(['mkdir ' path_out ]);
      end;      
      outfile = [path_out '/walk' speeds{j} '.mat']; 
      save(outfile,'o*','j_m_a*','j_m_s*','j_sd_a*','j_sd_s*');
    end;  
  end;
end;

figure(1)
h1 = figure(1)
subplot(3,5,1);
set(gca,'linewidth',2);
ylabel('Act.','fontsize',12);
subplot(3,5,2);
set(gca,'linewidth',2);
subplot(3,5,3);
set(gca,'linewidth',2);
subplot(3,5,4);
set(gca,'linewidth',2);
subplot(3,5,5);
set(gca,'linewidth',2);
subplot(3,5,6);
ylabel('Act.','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,7);
set(gca,'linewidth',2);
subplot(3,5,8);
set(gca,'linewidth',2);
subplot(3,5,9);
set(gca,'linewidth',2);
subplot(3,5,10);
set(gca,'linewidth',2);
subplot(3,5,11);
xlabel('Pct. GC','fontsize',12);
ylabel('Act.','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,12);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,13);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,14);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
legend('0.75 m/s','1.00 m/s','1.25 m/s','1.50 m/s','1.75 m/s','2.00 m/s','location','EastOutside');
set(gca,'linewidth',2);
set(gcf,'Color',[1,1,1])
saveas(h1,'low100_act.fig');
export_fig low100_act -png;

figure(3)
h3 = figure(3)
subplot(3,5,1);
set(gca,'linewidth',2);
ylabel('Act.','fontsize',12);
subplot(3,5,2);
set(gca,'linewidth',2);
subplot(3,5,3);
set(gca,'linewidth',2);
subplot(3,5,4);
set(gca,'linewidth',2);
subplot(3,5,5);
set(gca,'linewidth',2);
subplot(3,5,6);
ylabel('Act.','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,7);
set(gca,'linewidth',2);
subplot(3,5,8);
set(gca,'linewidth',2);
subplot(3,5,9);
set(gca,'linewidth',2);
subplot(3,5,10);
set(gca,'linewidth',2);
subplot(3,5,11);
xlabel('Pct. GC','fontsize',12);
ylabel('Act.','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,12);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,13);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,14);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
legend('0.75 m/s','1.00 m/s','1.25 m/s','1.50 m/s','1.75 m/s','2.00 m/s','location','EastOutside');
set(gca,'linewidth',2);
set(gcf,'Color',[1,1,1])
saveas(h1,'sang_act.fig');
export_fig sang_act -png;

load ~/Documents/MATLAB/data/Perry_muscles/stim_act;
figure(2)
h2 = figure(2);
subplot(3,5,1)
plot(0:(numpts-1),p_sTA,'k','linewidth',3); axis tight;
subplot(3,5,2)
plot(0:(numpts-1),p_sSOL,'k','linewidth',3); axis tight;
subplot(3,5,3)
plot(0:(numpts-1),p_sGAS,'k','linewidth',3); axis tight;
subplot(3,5,4)
plot(0:(numpts-1),p_sVLAT,'k','linewidth',3); axis tight;
subplot(3,5,5)
plot(0:(numpts-1),p_sBFSH,'k','linewidth',3); axis tight;
subplot(3,5,6)
plot(0:(numpts-1),p_sRF,'k','linewidth',3); axis tight;
subplot(3,5,7)
plot(0:(numpts-1),p_sSMEM,'k','linewidth',3); axis tight;  
subplot(3,5,8)
plot(0:(numpts-1),p_sBFLH,'k','linewidth',3); axis tight;
subplot(3,5,9)
plot(0:(numpts-1),p_sILL,'k','linewidth',3); axis tight;
subplot(3,5,10)
plot(0:(numpts-1),p_sGLU_low,'k','linewidth',3); axis tight;
subplot(3,5,11)
plot(0:(numpts-1),p_sGLU_upp,'k','linewidth',3); axis tight;
subplot(3,5,12)
plot(0:(numpts-1),p_sGMED,'k','linewidth',3); axis tight;
subplot(3,5,13)
plot(0:(numpts-1),p_sAL,'k','linewidth',3); axis tight;
subplot(3,5,14)
plot(0:(numpts-1),p_sAM,'k','linewidth',3); axis tight;
subplot(3,5,1);
set(gca,'linewidth',2);
ylabel('Exc.','fontsize',12);
subplot(3,5,2);
set(gca,'linewidth',2);
subplot(3,5,3);
set(gca,'linewidth',2);
subplot(3,5,4);
set(gca,'linewidth',2);
subplot(3,5,5);
set(gca,'linewidth',2);
subplot(3,5,6);
ylabel('Exc.','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,7);
set(gca,'linewidth',2);
subplot(3,5,8);
set(gca,'linewidth',2);
subplot(3,5,9);
set(gca,'linewidth',2);
subplot(3,5,10);
set(gca,'linewidth',2);
subplot(3,5,11);
xlabel('Pct. GC','fontsize',12);
ylabel('Exc.','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,12);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,13);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,14);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
legend('0.75 m/s','1.00 m/s','1.25 m/s','1.50 m/s','1.75 m/s','2.00 m/s','Perry','location','EastOutside');
set(gca,'linewidth',2);
set(gcf,'Color',[1,1,1])
saveas(h2,'low100_exc.fig');
export_fig low100_exc -png;

figure(4)
h4 = figure(4);
subplot(3,5,1)
plot(0:(numpts-1),p_sTA,'k','linewidth',3); axis tight;
subplot(3,5,2)
plot(0:(numpts-1),p_sSOL,'k','linewidth',3); axis tight;
subplot(3,5,3)
plot(0:(numpts-1),p_sGAS,'k','linewidth',3); axis tight;
subplot(3,5,4)
plot(0:(numpts-1),p_sVLAT,'k','linewidth',3); axis tight;
subplot(3,5,5)
plot(0:(numpts-1),p_sBFSH,'k','linewidth',3); axis tight;
subplot(3,5,6)
plot(0:(numpts-1),p_sRF,'k','linewidth',3); axis tight;
subplot(3,5,7)
plot(0:(numpts-1),p_sSMEM,'k','linewidth',3); axis tight;
subplot(3,5,8)
plot(0:(numpts-1),p_sBFLH,'k','linewidth',3); axis tight;
subplot(3,5,9)
plot(0:(numpts-1),p_sILL,'k','linewidth',3); axis tight;
subplot(3,5,10)
plot(0:(numpts-1),p_sGLU_low,'k','linewidth',3); axis tight;
subplot(3,5,11)
plot(0:(numpts-1),p_sGLU_upp,'k','linewidth',3); axis tight;
subplot(3,5,12)
plot(0:(numpts-1),p_sGMED,'k','linewidth',3); axis tight;
subplot(3,5,13)
plot(0:(numpts-1),p_sAL,'k','linewidth',3); axis tight;
subplot(3,5,14)
plot(0:(numpts-1),p_sAM,'k','linewidth',3); axis tight;
subplot(3,5,1);
set(gca,'linewidth',2);
ylabel('Exc.','fontsize',12);
subplot(3,5,2);
set(gca,'linewidth',2);
subplot(3,5,3);
set(gca,'linewidth',2);
subplot(3,5,4);
set(gca,'linewidth',2);
subplot(3,5,5);
set(gca,'linewidth',2);
subplot(3,5,6);
ylabel('Exc.','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,7);
set(gca,'linewidth',2);
subplot(3,5,8);
set(gca,'linewidth',2);
subplot(3,5,9);
set(gca,'linewidth',2);
subplot(3,5,10);
set(gca,'linewidth',2);
subplot(3,5,11);
xlabel('Pct. GC','fontsize',12);
ylabel('Exc.','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,12);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,13);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
subplot(3,5,14);
xlabel('Pct. GC','fontsize',12);
set(gca,'linewidth',2);
legend('0.75 m/s','1.00 m/s','1.25 m/s','1.50 m/s','1.75 m/s','2.00 m/s','Perry','location','EastOutside');
set(gca,'linewidth',2);
set(gcf,'Color',[1,1,1])
saveas(h4,'sang_exc.fig');
export_fig sang_exc -png;