function features = makeMSFProj(trials,CV,command)
%%%  features = makeMSFProj(trials,CV)
%%%   trials: ntasks x ntrials cell array of data
%%%   CV.parms contains
%%%           .nlags
%%%           .winSize
%%%           .winShift
%%%   features: ntasks x ntrials cell array of featurized data

if nargin > 2 && strcmp(command,'make')
  lagged = lagize(trials,CV.parms.nlags);
  allData = [];
  [r,c] = size(trials);
  for ri = 1:r
    for ci = 1:c
      allData = [allData lagged{ri,ci}];
    end
  end

  if CV.parms.meanSubtract
    means = mean(allData,2);
    allData = allData - repmat(means,1,size(allData,2));
    CV.parms.means = means;
  else
    CV.parms.means = [];
  end    

  [u,s] = mnf2(allData);
  u = s';
  CV.parms.basis = u;

  features = CV;

else

  nlags = CV.parms.nlags;
  basis = CV.parms.basis;
  lagged = lagize(trials,nlags);
  
  [r,c] = size(trials);
  for ri = 1:r
    for ci = 1:c
      if CV.parms.meanSubtract
	d = lagged{ri,ci} - repmat(CV.parms.means,1,size(lagged{ri,ci},2));
      else
	d = lagged{ri,ci};
      end
      features{ri,ci} = basis' * d;
    end
  end

end
