function q = dcm2q(dcm)
% function q = dcm2q(dcm)
% Conversion from Discrete Cosine Matrix to q-parameter
%     input dcm:Discrete Cosine Matrix
%     output q:q-parameter
%
%     Copyright Tatsuaki Hashimoto  2008.12.5
%
%

check = det(dcm) ;
if abs(check-1.0) > 1e-6
   fprintf('input matrix error %e \n', check)
end

qq4 = 0.5*sqrt( 1.0 + trace(dcm) );
qq1 = ( dcm(2,3) - dcm(3,2) )/4/qq4;
qq2 = ( dcm(3,1) - dcm(1,3) )/4/qq4;
qq3 = ( dcm(1,2) - dcm(2,1) )/4/qq4;

q = [qq1 qq2 qq3 qq4];

check = norm(q) ;
if abs(check-1.0) > 1e-6
   fprintf('output vector error %e \n', check)
end
