
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'};


for i = 1:length(methods)
  for j = 1:length(speeds)
    path_in = ['~/Documents/MATLAB/data/avgEMG/' methods{i}];      
    infile = [path_in '/walk' speeds{j} '.mat']; 
    load(infile)
    for m = 1:length(muscles)
      musc = muscles{m};
      var_a = [methods{i} '.act.' musc '.walk' speeds{j} ];
      eval([var_a ' = [];']);
      var_s = [methods{i} '.stim.' musc '.walk' speeds{j} ];
      eval([var_s ' = [];']);
      eval(['len1 = size(om_a' musc ',1);']);
      if len1 > 1
        % Perform LOOCV:
        for n = 1:len1
          eval(['tr_a = om_a' musc ';']);
          te_a = tr_a(n,:);
          tr_a(n,:) = [];
          mtr_a = mean(tr_a,1);
          %r2_a = 1-sum((te_a-mtr_a).^2)./sum((te_a-mean(te_a)).^2);
          r_a = corr(mtr_a',te_a');
          eval([var_a ' = cat(1,' var_a ',r_a);']);
          eval(['tr_s = om_s' musc ';']);
          te_s = tr_s(n,:);
          tr_s(n,:) = [];
          mtr_s = mean(tr_s,1);
          %r2_s = 1-sum((te_s-mtr_s).^2)./sum((te_s-mean(te_s)).^2);
          r_s = corr(mtr_s',te_s');
          eval([var_s ' = cat(1,' var_s ',r_s);']);
        end;
      else
        eval([var_a ' = 0;']); 
        eval([var_s ' = 0;']);
      end;
    end;
 end;  
end;      
    
for i = 1:length(methods)
  for j = 1:length(speeds)
    for m = 1:length(muscles)
      musc = muscles{m};
      var_a = [methods{i} '.act.' musc '.walk' speeds{j} ];
      eval(['mean_' var_a ' = mean(' var_a ');']);
      eval(['sd_' var_a ' = std(' var_a ',0);']);
      var_s = [methods{i} '.stim.' musc '.walk' speeds{j} ];
      eval(['mean_' var_s ' = mean(' var_s ');']);
      eval(['sd_' var_s ' = std(' var_s ',0);']);    
    end;
  end;     
end;
      
clear m_* o* t* v* i* r*

whos *low*
whos *sang*

save('emg_cross_validation.mat','*low*','*sang*');