Code covered by the BSD License

### Daniel Auger (view profile)

```function C = phlead(varargin)
%   C = PHLEAD(W, LEADDEG) returns a state-space representation of a phase
%   unity gain at W rad/sec.

% Check input arguments and calculate constants.

num = [alpha w];
den = [1 w*alpha];
C = tf(num, den);

% Convert to state-space.
C = ss(C);
C = prescale(C);

% Check we've calculated reasonably well - give warnings if not

end

%   to give the specified phase lead.
%
%   A phase lead compensator has the following structure:
%
%             a*s + w
%      G(s) = -------
%             s + w*a
%
%   When s = j*w, this becomes
%
%               j*a*w + w   1+j*a      1+j*a
%      G(jw) =  --------- = ----- =  ---------
%               j*w + w*a    a+j     j*(1-j*a)
%
%   This has unit magnitude.  The phase is given by
%
%      arg G(jw) = arg(1+j*a) - arg(1-j*a) - arg(j)
%                = 2*arg(1+j*a) - arg(j)
%                = 2*atan(a) - 90 deg
%
%   This is the peak phase lead.  (At high or low frequencies, the phase
end

error(nargchk(2, 2, nargin, 'struct')); %#ok<NCHKN>
w = varargin{1};
'Infeasible w value: w must be a real scalar in {x: 0 < x < inf}.');
['Infeasible leadDeg value: must be a real scalar in ' ...
'{x: 0 <= x < inf}.']);
end

function success = isValidW(x)
if ~isa(x, 'double')
success = false;
else
success = isscalar(x) & isreal(x) & all(x(:) > 0) & all(x(:) < inf);
end
end

if ~isa(x, 'double')
success = false;
else
success = isscalar(x) & isreal(x) & all(x(:) >= 0) & all(x(:) < 90);
end
end

M = freqresp(C, w);
phaseTol = 1e-2;
if abs(phaseErr) > phaseTol
'Achieved phase lead is %0.2f but demand was %0.2f.', ...
end
achievedGain = 20*log10(abs(M));
gainErr = achievedGain;
gainTol = 1e-2;
if abs(gainErr) > gainTol