1;

function parms=fitgaussians(x,y,inparms)
  parms=nonlin_curvefit(@gaussians,inparms,x,y);
  plot_results(x,y,parms);
endfunction

function y=gaussians(parms,x)
  y=x*0;
  for i=0:numel(parms)/3-1
    y=y+exp(-((x-parms(i*3+2)) ./ (0.6005615*parms(i*3+3))) .^2) *parms(i*3+1);
  endfor
endfunction

function plot_results(x,y,parms)
  close
  plot(x,y);
  hold all;
  nsets=numel(parms)/3;
  lcol=zeros(nsets,3);
  lcol(:,2)=1;
  for i=1:nsets
    lcol(i,1)=i/nsets;
    lcol(i,2)=(nsets-i+1)/nsets;
  endfor
  for i=0:numel(parms)/3-1
    plpr=sprintf("--;a=%f x=%f w=%f;",parms(i*3+1),parms(i*3+2),parms(i*3+3)); 
    plot(x,gaussians(parms(i*3+1:i*3+3),x),plpr,"color",lcol(i+1,:));
  endfor
  plot(x,gaussians(parms,x));
  hold off;
  lcol
endfunction

