function [num,den,tau,sos] = iirgrpdelay(order,f,edges,des,varargin)
%IIRGRPDELAY Optimal IIR filter design with prescribed group-delay.

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

%   Copyright 1999-2023 The MathWorks, Inc.

if nargin > 4
        [varargin{:}] = convertStringsToChars(varargin{:});
end

if rem(order,2) ~= 0
   error(message('dsp:iirgrpdelay:MustBeEven'));
end

% Parse input
s = iirparser(3,4,order,f,edges,des,varargin{:});

% Catch case when desired group delay is zero
if all(des == 0)
    error(message('dsp:iirgrpdelay:InvalidRange'));
end
    
% Design filter
[as,tau] = filterdesign.internal.iirgrpdelaymex(s.denOrd,s.edges,s.f,s.des,s.wt,...
    s.maxRadius,s.P,s.density,s.Ho,s.AS); %s.Ho is actually group delay estimate in this case

sections = fix((s.denOrd + 1) / 2);

% Get handle to toSos function
toSoshndl = computetfandsos('gettososhndl');

% Convert numerator and denominator into second-order sections format
a = feval(toSoshndl,as, s.denOrd, sections);
sos = [fliplr(a) a];

[num,den] = sos2tf(sos,1);



