function sys = fselect(sys,varargin)
%FSELECT  Selects frequency points or range in FRD model.
%
%   SUBSYS = FSELECT(SYS,FMIN,FMAX) takes an FRD model SYS and selects
%   the portion of the frequency response between the frequencies
%   FMIN and FMAX.  The selected range [FMIN,FMAX] should be expressed
%   in the FRD model units (see "FrequencyUnit" property).
%
%   SUBSYS = FSELECT(SYS,INDEX) selects the frequency points specified
%   by the vector of indices INDEX. The resulting frequency grid is
%   SYS.Frequency(INDEX).
%
%   See also FRD/INTERP, FCAT, FRD.

%   Copyright 1986-2015 The MathWorks, Inc.
ni = nargin;
if ni<2
   error(message('Control:general:TwoOrMoreInputsRequired','fselect','fselect'))
elseif ni==2
   % Validate INDEX
   index = varargin{1};
   if islogical(index)
      index = find(index);
      varargin{1} = index;
   end
   if any(index<1 | index~=round(index)) || any(diff(index)<=0)
      error(message('Control:transformation:fselect2'))
   end
else
   % Validate FMIN, FMAX
   fmin = varargin{1}; fmax = varargin{2};
   if ~(isnumeric(fmin) && isreal(fmin) && isscalar(fmin) && ...
         isnumeric(fmax) && isreal(fmax) && isscalar(fmax))
      error(message('Control:transformation:fselect3'))
   end
   cf = funitconv(sys.FrequencyUnit,'rad/TimeUnit',sys.TimeUnit);
   varargin{1} = cf * fmin;
   varargin{2} = cf * fmax;
end

% Extract requested portion of the data
sys = fselect_(sys,varargin{:});