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

makeListing;

for i = 1:length(subjects)
  for j = 1:length(methods)
    for k = 1:length(speeds)
      
      path1   = ['~/Documents/MATLAB/data/' subjects{i} '/processed/EMG/avg_act_thresh'];
      eval(['load ''' path1 '/' methods{j} '/walk' speeds{k} '.mat'';']); 

      avg_file = ['~/Documents/MATLAB/data/avgEMG/' methods{j} '/walk' speeds{k} '.mat'];
      eval(['load ' avg_file ';']);
            
      eval(['num_muscles = length(' subjects{i} '.bad_act_walk' speeds{k} ');']); 
      for mm = 1:num_muscles
         eval(['mus = ' subjects{i} '.bad_act_walk' speeds{k} '{mm};']); 
         eval(['m_a' mus '= j_m_a' mus ';']);
         eval(['m_s' mus '= j_m_a' mus ';']);
         eval(['sd_a' mus '= j_sd_a' mus ';']);
         eval(['sd_s' mus '= j_sd_s' mus ';']);
      end;
      if strcmp(subjects{i},'AD052611') == 1
        m_sHAM  = (m_sBFLH + m_sSMEM)/2;
        m_aHAM  = (m_aBFLH + m_aSMEM)/2;
        sd_aHAM = 0.5*sqrt(sd_aBFLH.^2+sd_aSMEM.^2);
        sd_sHAM = 0.5*sqrt(sd_sBFLH.^2+sd_sSMEM.^2);
        m_aHAB   = m_aGLUU;
        sd_aHAB  = sd_aGLUU;        
        m_sHAB   = m_sGLUU;
        sd_sHAB  = sd_sGLUU;        
      elseif strcmp(subjects{i},'AM062411') == 1
        m_aHAB   = m_aGLUU;
        sd_aHAB  = sd_aGLUU;        
        m_sHAB   = m_sGLUU;
        sd_sHAB  = sd_sGLUU;        
      elseif strcmp(subjects{i},'EM072611') == 1      
        m_aHAB   = m_aGLUU;
        sd_aHAB  = sd_aGLUU;        
        m_sHAB   = m_sGLUU;
        sd_sHAB  = sd_sGLUU;        
      elseif strcmp(subjects{i},'DS100611') == 1
        m_aHAB   = m_aGLUU;
        sd_aHAB  = sd_aGLUU;        
        m_sHAB   = m_sGLUU;
        sd_sHAB  = sd_sGLUU;        
      elseif strcmp(subjects{i},'DH121412') == 1        
        m_sHAM  = (m_sBFLH + m_sSMEM)/2;      
        m_aHAM  = (m_aBFLH + m_aSMEM)/2;         
        sd_aHAM = 0.5*sqrt(sd_aBFLH.^2+sd_aSMEM.^2);
        sd_sHAM = 0.5*sqrt(sd_sBFLH.^2+sd_sSMEM.^2);        
      elseif strcmp(subjects{i},'MC121812') == 1
        m_sGLU  = m_sGLUU;
        sd_sGLU = sd_sGLUU;
        m_aGLU  = m_aGLUU;
        sd_aGLU = sd_aGLUU;
      elseif strcmp(subjects{i},'JB011613') == 1
      elseif strcmp(subjects{i},'BC011113') == 1
        m_sHAM  = (m_sBFLH + m_sSMEM)/2;      
        m_aHAM  = (m_aBFLH + m_aSMEM)/2;                 
        sd_aHAM = 0.5*sqrt(sd_aBFLH.^2+sd_aSMEM.^2);
        sd_sHAM = 0.5*sqrt(sd_sBFLH.^2+sd_sSMEM.^2);        
      elseif strcmp(subjects{i},'DC011413') == 1
        m_sHAM  = m_sBFSH;
        m_aHAM  = m_sBFSH;
        sd_sHAM = sd_sBFSH;
        sd_aHAM = sd_sBFSH;       
      end;
      
      % Try first without this!
      %m_aHAB = (m_aHAB + j_m_aGMED)/2;
      %m_sHAB = (m_sHAB + j_m_sGMED)/2;      
      %sd_aHAB = 0.5*sqrt(m_aHAB.^2+j_m_aGMED.^2);
      %sd_sHAB = 0.5*sqrt(m_sHAB.^2+j_m_sGMED.^2);      
      
      % Could put filtering of GLU mid-stance feature in here if I
      % decide to do that.
      
      eval(['path2 = ''~/Documents/MATLAB/data/' subjects{i} '/processed/EMG/avg_act_final/' methods{j} ''';']);      
      if exist(path2) == 0
         eval(['mkdir ' path2 ]);
      end;  
      eval(['outfile = ''' path2 '/walk' speeds{k} '.mat'';']);
      save(outfile,'m_*','sd_*');   

    end;
  end;  
end;    

clear all;
muscles  = {'TA';'SOL';'GAS';'VAS';'BFSH';'RF';'SMEM';'BFLH';'ILL';'GLU';'GLUU';'GMED';'ADDL';'ADDM'};
methods  = {'low100_mvc_lo';'sang_mvc_lo'};

for jj = 1:length(methods)
 
  % DH121412 walk133
  for mmm = 1:length(muscles)
    eval(['clear *' muscles{mmm} ';']);
  end;
  clear *HAM; clear *HAB;
  eval(['load ''~/Documents/MATLAB/data/DH121412/processed/EMG/avg_act_final/' methods{jj} '/walk125.mat'';']);
  tempfile = ['DH121412_' methods{jj} '_walk125.mat'];
  save(tempfile,'*TA','*ADDL','*GMED');
  eval(['load ''~/Documents/MATLAB/data/DH121412/processed/EMG/avg_act_thresh/' methods{jj} '/walk133.mat'';']);  
  eval(['load ' tempfile]);
  m_sHAM  = (m_sBFLH + m_sSMEM)/2;      
  m_aHAM  = (m_aBFLH + m_aSMEM)/2;         
  sd_aHAM = 0.5*sqrt(sd_aBFLH.^2+sd_aSMEM.^2);
  sd_sHAM = 0.5*sqrt(sd_sBFLH.^2+sd_sSMEM.^2);
  outfile = ['~/Documents/MATLAB/data/DH121412/processed/EMG/avg_act_final/' methods{jj} '/walk133.mat'];
  save(outfile,'m_*','sd_*');
   
  % BC011113 walk133
  for mmm = 1:length(muscles)
    eval(['clear *' muscles{mmm} ';']);
  end;
  clear *HAM; clear *HAB;  
  eval(['load ''~/Documents/MATLAB/data/BC011113/processed/EMG/avg_act_final/' methods{jj} '/walk125.mat'';']);
  tempfile = ['BC011113_' methods{jj} '_walk125.mat'];
  save(tempfile,'*BFLH','*ILL','*GLU','*ADDL','*GMED');
  eval(['load ''~/Documents/MATLAB/data/BC011113/processed/EMG/avg_act_thresh/' methods{jj} '/walk133.mat'';']);  
  eval(['load ' tempfile]);
  m_sHAM  = (m_sBFLH + m_sSMEM)/2;      
  m_aHAM  = (m_aBFLH + m_aSMEM)/2;         
  sd_aHAM = 0.5*sqrt(sd_aBFLH.^2+sd_aSMEM.^2);
  sd_sHAM = 0.5*sqrt(sd_sBFLH.^2+sd_sSMEM.^2);   
  outfile = ['~/Documents/MATLAB/data/BC011113/processed/EMG/avg_act_final/' methods{jj} '/walk133.mat'];
  save(outfile,'m_*','sd_*');
    
  % MC121812 walk200
  for mmm = 1:length(muscles)
    eval(['clear *' muscles{mmm} ';']);
  end;
  clear *HAM; clear *HAB;
  eval(['load ''~/Documents/MATLAB/data/MC121812/processed/EMG/avg_act_final/' methods{jj} '/walk175.mat'';']);
  tempfile = ['MC121812_' methods{jj} '_walk175.mat'];
  save(tempfile,'*SOL');
  eval(['load ''~/Documents/MATLAB/data/MC121812/processed/EMG/avg_act_final/' methods{jj} '/walk200.mat'';']);
  eval(['load ' tempfile]);
  outfile = ['~/Documents/MATLAB/data/MC121812/processed/EMG/avg_act_final/' methods{jj} '/walk200.mat'];
  save(outfile,'m_*','sd_*');
  
  % JB011613 walk200
  for mmm = 1:length(muscles)
    eval(['clear *' muscles{mmm} ';']);
  end;
  clear *HAM; clear *HAB;
  eval(['load ''~/Documents/MATLAB/data/JB011613/processed/EMG/avg_act_final/' methods{jj} '/walk175.mat'';']);
  tempfile = ['JB011613_' methods{jj} '_walk175.mat'];
  save(tempfile,'*SOL');
  eval(['load ''~/Documents/MATLAB/data/JB011613/processed/EMG/avg_act_final/' methods{jj} '/walk200.mat'';']);
  eval(['load ' tempfile]);
  outfile = ['~/Documents/MATLAB/data/JB011613/processed/EMG/avg_act_final/' methods{jj} '/walk200.mat'];
  save(outfile,'m_*','sd_*');

end;