% Open and close loop responses.
% Input in Hz, plot vs. radians/sec.
% Type 1, order 4 (zero and 3 poles in filter)
%   Like Eq. (12.1) in FS by PL but one more pole
% Modify gains, N, and singularities

% MODIFY PARAMETERS AND GO
clear
Kp = 1;
KLF = 1;
Kv = 2e8;
Fp1 = 1;
Fz = 100;
Fp2 = 2000;
Fp3 = 3000;
N = 1000;
op = 1; 
%********

twopi = 2*pi;
Wp1 = twopi*Fp1;
Wp2 = twopi*Fp2;
Wp3 = twopi*Fp3;
p = [0;-Wp1;-Wp2;-Wp3]; % pole locations
Wz = twopi*Fz;
z = [-Wz]; % zero location
K = Kp*KLF*Kv*Wp1*Wp2*Wp3/(N*Wz);
[num,den] = zp2tf(z,p,K); % conversion to open-loop transfer function

clf;
while op
	op = input('Enter 0 = quit; 1 = open-loop; 2 = error; 3 = output responses: ');
	if op == 3, % output modulation response to ref unit step divided by N
		num3 = num;
		den3 = num + den;
		bode(num3,den3); % G/(1+G) = num/(num + den)
		title('Closed-Loop Output Modulation Response to Ref/N')
	elseif op==2 % output modulation response to unit step of VCO
		num2 = den;
		den2 = num + den;
		bode(num2,den2); % 1/(1+G) = den/(num + den)
		title('Closed-Loop Error Modulation Response')
	elseif op~=0 % open loop resposne, Bode plot
		bode(num,den); % G = num/den
		title('Open-Loop Modulation Response')
	end % if
end % while

