function sos = fotonToSOS(fotonCoeffs)
% sos = fotonToSOS(fotonCoeffs)
%
% fotonToSOS converts filter coefficients used by LIGO CDS
%   and produced by Foton to Matlab SOS format.
%
% Example:
% foc = [0.0032898    -1.5263       0.6024      -1.4132      0.99859 ...
%                     -1.5731       0.7543      -1.1196      0.98454 ...
%                     -1.6560       0.9293      0.26583      0.99777];
% fvtool(fotonToSOS(foc))
%
% this is essentially the inverse of writeSOS
% (see also writeSOS, getSOS)


  % number of second order sections
  numSOS = (numel(fotonCoeffs) - 1) / 4;
  
  if numSOS ~= round(numSOS)
    error('foton coefficients must be a vector of length 4 * N + 1')
  end
  
  % index of coeffs in SOSs
  nn = 2:4:numel(fotonCoeffs);
  
  % setup SOS
  sos = ones(numSOS, 6);
  
  % numerator
  sos(:, 2) = fotonCoeffs(nn + 2);
  sos(:, 3) = fotonCoeffs(nn + 3);
  
  % denominator
  sos(:, 5) = fotonCoeffs(nn + 0);
  sos(:, 6) = fotonCoeffs(nn + 1);

  % gain
  g = fotonCoeffs(1);
  sos(1, 1:3) = g * sos(1, 1:3);
end
