function Gms=finn_Gms(G)
%=========================================================================\
% Finds Gms (minimum-phase and stable version of G)
%=========================================================================/
s=tf('s');
[num_G, den_G]=tfdata(G,'v');
zeros_G=roots(num_G);
poles_G=roots(den_G);

    for i=1:length(zeros_G)
        if(zeros_G(i)>0)
            zeros_Gms(i)=-zeros_G(i);
        else
            zeros_Gms(i)=zeros_G(i);
        end
    end

    for i=1:length(poles_G)
        if(poles_G(i)>0)
            poles_Gms(i)=-poles_G(i);
        else
            poles_Gms(i)=poles_G(i);
        end
    end

    if(length(zeros_G)==0) %If the system has no zeros...
        Gms=tf(1,poly(poles_Gms));
    else
        Gms=tf(poly(zeros_Gms),poly(poles_Gms));
    end
    
    
% Gs=1;
% Gm=1;
% 
% for ii=1:length(poles_G)
%     Gs=Gs*(s-poles_G(ii))/(s+poles_G(ii));
% end
% 
% for jj=1:length(zeros_G)
%     Gm=Gm*(s+zeros_G(jj))/(s-zeros_G(jj));
% end
% 
% Gms=Gs*G*Gm;
% 
% end

end















