function Focus = getFreqFocus(wspec,Ts,FSCALE)
% Infers user-specified focus from WSPEC input to BODE, SIGMA,...
%
% FOCUS is 2x2 where 
%   * The first row is the linear-scale focus [FMIN,FMAX] with
%     0<=FMIN<FMAX<Inf
%   * The second row is the log-scale focus [FMIN,FMAX] with
%     0<FMIN<FMAX<Inf
% NaN values indicate unspecified lower or upper limits.

%   Copyright 1986-2021 The MathWorks, Inc.

% Note: In FOCUS, use NaN to denote unspecified WMIN or WMAX frequency to 
%       distinguish bode(sys,[0 logspace(...) Inf]) vs. bode(sys,{0,Inf})
Focus = NaN(2,2);
if iscell(wspec)
   % User-defined range {WMIN,WMAX} with 0<=WMIN<WMAX<=Inf
   if wspec{1}>=0
      Focus(1,1) = wspec{1};
   end
   if wspec{1}>0
      Focus(2,1) = wspec{1};
   end
   if wspec{2}<Inf
      Focus(:,2) = wspec{2};
   end
   if Ts~=0
      % FOCUS should be inside [0,pi/Ts]
      nf = pi/abs(Ts);
      if Focus(1,2)>nf && (Focus(1,1)<nf || isnan(Focus(1,1)))
         Focus(:,2) = nf;
      end
   end
elseif ~isempty(wspec)
   % Frequency grid WSPEC
   w = sort(abs(wspec));
   w = w(w<Inf);
   % Linear scale
   if numel(w)>1
      Focus(1,:) = [w(1),w(end)];
   elseif isscalar(w)
      Focus(1,:) = w * [.5 2] + [0 (w==0)];
   end
   % Log scale
   w = w(w>0);
   if numel(w)>1
      Focus(2,:) = [w(1),w(end)];
   elseif isscalar(w)
      Focus(2,:) = w * [.5 2];
   end
end

% Return requested focus if specialized
if nargin>2
   if strcmp(FSCALE,'log')
      Focus = Focus(2,:);
   else
      Focus = Focus(1,:);
   end
end
