function [phir_resp,phir_ref] = cpx2real(phic_resp,phic_ref,ic2rmeth)
%
%  Name: cpx2real
% 
%  Usage: cpx2real
% 
%   Description:
%    Converts complex modes to real modes
%        ic2rmeth=1 - simply uses real part
%	 ic2rmeth=2 - rounds phases to +/- 180
%	 ic2rmeth=3 - uses Mcmillan transformation
%

%
%  Version SWD970520
%
%  Author(s): Scott W. Doebling, doebling@lanl.gov
%
%  Institution: Los Alamos National Laboratory,
%		ESA-EA
%
%  Copyright (c) 1996, The Regents of the University
%     	of California. (See top level of code for
%	full copyright statement.)


%%
%% Compute phir_resp
%%

%
%  Method 1: Use real part of mode shape
%

if ic2rmeth ==1,
   
   phir_resp = real(phic_resp);

%
%  Method 2: Phase Rounding
%

elseif ic2rmeth==2,

   phir_resp = zeros(size(phic_resp));
   
   for j = 1:size(phic_resp,2),
      
      r = abs(phic_resp(:,j));
      a = angle(phic_resp(:,j));
      pc = polyfit(r.*cos(a),r.*sin(a),1);
      
      amod = a - pc(1);
      
      for i = 1:size(phic_resp,1),

        if abs(amod(i)) < pi/2,
             angsign = 1;
        else
             angsign = -1;
        end

        phir_resp(i,j) = abs(phic_resp(i,j)) * angsign;
       
      end

   end

end


%%
%%  Define phir_ref such that mode participation factors are preserved
%%

for j = 1:size(phic_resp,2)
   
   for i = 1:size(phic_ref,1),
   
      phir_ref(i,j) = real(phir_resp(:,j) \ (phic_resp(:,j)*phic_ref(i,j)));
      
   end
   
end

return

%%
%%  Method 3: McMillan Transformation (Defunct)
%%

%if ic2rmeth==3,

%
%  Construct state-space matrices
%

%if exist('Freq') == 1, 
%  freqmc = Freq;
%  dampmc = Damp;
%  p_resp = phic_resp;
%  p_ref  = phic_ref;
%  suff   = '';
%elseif exist('freq_t') ==1,
%  freqmc = freq_t;
%  dampmc = damp_t;
%  p_resp = phic_resp_t;
%  p_ref  = phic_ref_t;
%  suff   = '_t';
%end


%nmodmc = length(freqmc);
%j = sqrt(-1);

%     Define complex eigenvalues

%Adr = zeros(nmodmc*2,nmodmc*2);
%Bdr = zeros(nmod*2,size(p_ref,1));
%Cdr = zeros(size(p_resp,1),nmod*2);
%ddcr = zeros(nmodmc,1);

%for i = 1:nmodmc,

%   sig = -dampmc(i) * 2 * pi * freqmc(i);

%   omeg = sqrt( (2*pi*freqmc(i))^2 - sig^2);

%   Adr( (i-1)*2+1 : i*2 , (i-1)*2+1 : i*2 ) = [sig + j*omeg, 0; 0, sig - j*omeg];

%   Bdr( (i-1)*2 + 1 : i*2 , :) =  [ p_ref(:,i) conj(p_ref(:,i))]';

%   Cdr( : , (i-1)*2 + 1 : i*2 ) = [ p_resp(:,i) conj(p_resp(:,i))];

 %  ddcr( (i-1)*2+1 : i*2 ) = [ sig + j*omeg; sig - j*omeg];

%end

%
%  Run McMillan Transformation
%

%[Am,Bm,Cm,V] = mcmillan(Adr,Bdr,Cdr,ddcr);

%
%  Create appropriate Real mode shape vectors
%

%  Extract the frequencies

%dvec = [1:2:length(ddcr)];

%nm = length(dvec);

%freq_unsort = zeros(nm,1);
%phi_unsort = zeros(size(Cm,1),nm);

%for i = 1:nm,

%  freq_unsort(i) = sqrt(-Am((dvec(i)+1),dvec(i)))/2/pi;

%  phir_resp_unsort(:,i) = Cm(:,(dvec(i)+1));
 
%  phir_ref_unsort(:,i) = Bm((dvec(i)+1),:)';

%end

%[temp, isort] = sort(freq_unsort);

%eval(['phir_resp',suff,'= phir_resp_unsort(:,isort);'])
%eval(['phir_ref',suff,'= phir_ref_unsort(:,isort);'])

%end

return
