function [C1,C2,R2] = secondOrderPassiveFilterDesign(Fc,Phi,Icp,Kvco,Nmax)

% This function computes the 2nd order passive loop filter componet values 
% for the 2 capacitors (C1 & C2) and 1 resistor (R2).    
%
% Input:
% Fc   - Loop bandwidth(ideally start at 1/10th of the reference frequency)
% Phi  - Desired phase margin (ideally it should be 40< phi <60)
% Icp  - Charge-pump current (follow the design spec sheet value)
% Kvco - VCO voltage sensitivity value (in Hz/V) mentioned in design spec.
% Nmax - Maximum divider ratio the PLL is supposed to work on.
%
% Ouput:
% C1, C2 & R2 : Component values of two capacitors, C1 & C2, and one 
%               resistor, R2, forming the 2nd order passive loop filter.
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The output that this function dumps out is the respective R and C values 
% of the passive 2nd order loop filter constituents as well as the 
% respective poles, zeros and the gain values of the configuration.
% The transfer function of the 2nd order filter is represented as:
%                     1+s.T2
%            Z(s)= ------------
%                  s.A0(1+s.T1)
% where, T2 = R2*C2
%        T1 = R2*C2*C1/A0
%        A0 = C1+C2
% There is another paramter named gamma optimization factor which is used
% to optimize the performance of the PLL. But for now we will use it as a
% constant and set it at it's default value of 1.

% Copyright 2018, The MathWorks, Inc.

if(nargin ~= 5 )
    error(message('msblks:msblksMessages:InvalidNumberOfParameters'));
elseif (length(Fc) ~= 1 || Fc <= 0 || isreal(Fc) ~= 1)
    error(message('msblks:msblksMessages:InvalidLoopBandwidth'));
elseif (length(Phi) ~= 1 || Phi <= 0 || isreal(Phi) ~= 1)
    error(message('msblks:msblksMessages:InvalidPhaseMargin'));
elseif(length(Icp) ~= 1 || Icp <= 0 || isreal(Icp) ~= 1)
    error(message('msblks:msblksMessages:InvalidChargePumpCurrent','Icp'));
elseif(length(Kvco) ~= 1 || Kvco <= 0 || isreal(Kvco) ~= 1)
    error(message('msblks:msblksMessages:InvalidVoltageSensitivity','Kvco'));
elseif(length(Nmax) ~= 1 || Nmax <= 0 || isreal(Nmax) ~= 1)
    error(message('msblks:msblksMessages:InvalidDividerRatio','Nmax'));
% elseif(nargin == 5)
%     warning(message('msblks:msblksMessages:MissingPlotOption','Plot'));
%     Plot=0;
% elseif((Plot ~= 0 && Plot ~= 1))
%     warning(message('msblks:msblksMessages:InvalidPlotOption','Plot'));
%     Plot=0;
end

Kd=Icp;
N=Nmax;
% converting Fc (loop bandwidth) to rad/s
OmegaC=2*pi*Fc; 
%computing pole T1 using approximate method
T1=(sec((pi/180)*Phi)-tan((pi/180)*Phi))/OmegaC; 
%using the Gamma optimization factor as 1
Gamma=1; 
%computing the zero (T2) of loop filter which is independent of the loop filter order.
T2=Gamma/(OmegaC^2*T1); 
% Computing the filter coefficients A0 and A1 for the 2nd order filter
A0=(Kd*Kvco/(OmegaC^2*N))*sqrt((1+(OmegaC^2*T2^2))/(1+(OmegaC^2*T1^2)));
A1=A0*T1;
% Computing the values of the resistors and Capacitors using the knowledge
% of filter coefficients
C1=A1/T2;
C2=(A0*T2-A1)/T2;
R2=T2/C2;

if(C1<0 || isreal(C1) ~= 1 || isnan(C1) == 1 || C2<0 || isreal(C2) ~= 1 || ...
   isnan(C2) == 1 ||  R2<0 || isreal(R2) ~= 1 || isnan(R2) == 1)
    error(message('msblks:msblksMessages:InvalidLoopFilterComponents'));
end

% Num=[T2 1]; % alternatively, Num=[R2*C2 1];
% Den=[A1 A0 0]; % alternatively, Den=[C1*C2*R2 (C1+C2) 0];
% LoopFilterTf=tf(Num,Den); % Loop-filter transfer function
% NumVco=Kvco;
% DenVco=[1 0];
% VcoTf=tf(NumVco,DenVco); % VCO transfer function
% ForwardGain=(Kd*VcoTf*LoopFilterTf); % Forward Gain = Phase-detector gain * Loop-filter tf * VCO gain
% BackwardGain=1/N; % Backward gain comprises of the divider value
% OpenLoopTf=ForwardGain*BackwardGain;% open-loop transfer function
% if(Plot==1)
%     PlotTitle=message('msblks:msblksMessages:LoopDynamicPlotTitle').getString;
%     figure('Name',PlotTitle,'IntegerHandle','off','NextPlot','replace','Tag','SecondOrderLoopDynamicPlot');
%     subplot(2,2,1);
%     margin(OpenLoopTf); 
%     closed_sys=feedback(ForwardGain,BackwardGain);
%     subplot(2,2,2);
%     step(closed_sys);
%     subplot(2,2,4);
%     impulse(closed_sys);
%     subplot(2,2,3);
%     iopzmap(closed_sys);
% end