function all_factors = getScaleFactors(path1,methods,minspd,allspds)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% getScaleFactors.m finds the scaling factors between stimulation and
% activation peaks across speed.  It returns a structure with these
% numbers and also saves the numbers to the respective method directory.
%
% Inputs:
% path1   - subject identification.
% methods - cell of stimulation, activation estimation methods to be processed.
% minspd  - walking speed where MCOT is minimal, in cm/s. 
% allspd  - array of walking speeds in cm/s.
%
% Outputs:
% all_factors - strucure containing scaling factors for all queried data.
%
% Example Usage
% factors = getScaleFactors('JB011613',{'low100_mvc_sh'},125,[075 100 110 125 150 175 200]);
% 
% Jared Markowitz
% Spring 2013
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% For each subject I can pick only speeds, muscles where scaling is
% reasonable.  Can also either extrapolate or just make monotonic.

muscles = {'TA';'SOL';'GAS';'VAS';'BFSH';'RF';'HAM';'ILL';'GLU';'HAB';'ADDL';'ADDM'};

if length(find(allspds == minspd)) == 0
  allspds = [allspds minspd];
  fprintf('Appending minimum MCOT speed to allspds.\n');
end;

for j = 1:size(methods,1)

  clear factors;
  factors.speeds = allspds;

  for k = 1:size(muscles,1)    
    eval(['max_stim.' muscles{k} '= [];']);
    eval(['max_act.' muscles{k} '= [];']);
  end;
  
  for i = 1:length(allspds)
    speedc = num2str(allspds(i));
    if allspds(i) < 100
      speedc = ['0' speedc];
    end;
    if ischar(methods) == 0
      method1 = char(methods(j));
    else
      method1 = methods;
    end;
    pathf = ['~/Documents/MATLAB/data/' path1 '/processed/EMG/avg_act_thresh'];
    infile1 = [pathf '/' method1 '/walk' speedc '.mat']; 
    eval(['load ' infile1]);
    for l = 1:size(muscles,1)    
      eval(['max_stim.' muscles{l} '= cat(1,max_stim.' muscles{l} ',max(m_s' muscles{l} '));']);
      eval(['max_act.' muscles{l} '= cat(1,max_act.' muscles{l} ',max(m_a' muscles{l} '));']);
    end;
  end;
  
  ind = find(allspds == minspd);
  for m = 1:size(muscles,1)
    eval(['factors.stim.' muscles{m} '=max_stim.' muscles{m} '/max_stim.' muscles{m} '(ind);']);
    eval(['factors.act.' muscles{m} '=max_act.' muscles{m} '/max_act.' muscles{m} '(ind);']);  
  end;

  outfile = [pathf '/' method1 '/scaleFactors.mat'];
  save(outfile,'factors');

  eval(['all_factors.' methods{j} '= factors;']);
end;

