function [b,err] = firlpnorm(varargin)
%FIRLPNORM Least P-norm optimal FIR filter design.

%   References:
%     [1] A. Antoniou, Digital Filters:  Analysis, Design, and Applications,
%         2nd ed., McGraw-Hill, 1993.

%   Copyright 1999-2023 The MathWorks, Inc.

if nargin > 4 && dsp.util.isChar(varargin{end}) && ...
		strncmpi(char(varargin{end}), 'linphase', length(char(varargin{end})))
    
    error(message('dsp:firlpnorm:NotSupported'));
else
	% Design a nonlinear phase filter
	[b,err] = firlpnorm_nlp(varargin{:});
end

%-----------------------------------------------------------------------
function [b,err] = firlpnorm_nlp(numOrd,f,edges,des,varargin)
%FIRLPNORM_NLP Nonlinear phase FIRLPNORM function.


if ~isempty(varargin) && ~iscell(varargin{end}) && strcmpi('minphase', varargin{end})
    minPhase = 1;
    if length(varargin) == 1
        myArgin = {};
    else
    	myArgin = varargin(1:end-1);
    end
else
    minPhase = 0;
	myArgin = varargin;
end
if nargin > 5
   myArgin = {myArgin{1} 0.9 myArgin{2:end}};  % To satisfy iirparser.
end

% Parse the input
s = iirparser(4+minPhase,numOrd,0,f,edges,des,myArgin{:});

% Design the filter
[b,err] = filterdesign.internal.firlpnormmex(s.numOrd,s.edges,s.f,...
    s.des,s.wt,s.P,s.density,s.BS,minPhase);

% Compute the transfer function and the sos matrix. b(1) is sos gain
if minPhase == 1
	b = computetfandsos(s.numOrd,0,b(2:end),[],b(1));
end

% EOF

