function [Model,freq,unit] = parseFRDInputs(FRDfcn,InputList)
% Helper function for xFRD(MODEL,...). 
% 
% Parses input list and consolidates the signatures xFRD(MODEL,W), 
% xFRD(MODEL,W,UNIT), and xFRD(MODEL,W,'*Unit*',U). Also checks 
% compatibility of the frequency vectors if MODEL is already an
% FRD model.

%   Copyright 1986-2011 The MathWorks, Inc.
Model = InputList{1};
ni = numel(InputList);
if ni<2 || ni>4
   error(message('Control:transformation:frd1',FRDfcn,FRDfcn))
elseif ni==4 && ~contains(InputList{3},'unit','IgnoreCase',true)
   % Four-input call is deprecated and should be of the form
   % xFRD(Model,freq,'*unit*',unit)
   error(message('Control:general:InvalidSyntaxForCommand',FRDfcn,FRDfcn))
end
   
% Sort frequency vector, remove duplicates, and validate
freq = InputList{2};
if isnumeric(freq)
   freq = unique(freq(:));
end
freq = FRDModel.checkFrequencyVector(freq);

% Resolve frequency units
if ni>2
   unit = ltipack.matchKey(InputList{end},ltipack.getValidFrequencyUnits());
   if isempty(unit)
      error(message('Control:ltiobject:setFRD2'))
   end
else
   unit = 'rad/TimeUnit';
end

% Warn about Ts=-1, see frd/checkDataConsistency for rationale
if isa(Model,'DynamicSystem') && Model.Ts==-1
   warning(message('Control:ltiobject:frdAmbiguousRate2'))
   Model.Ts = 1;
end
