function [sv,w,FocusInfo] = sigmaresp(D,type,wspec,isPlotted)
% Generates singular value response data for MIMO LTI models. 
% Used by SIGMA.
%
%  [SV,W,FOCUSINFO] = SIGMARESP(D,TYPE,WSPEC,ISPLOTTED) 
%  computes the frequency response of a single MIMO model D
%  over some user-defined or auto-generated frequency grid W
%  and returns the singular values (principal gains) of the
%  response. SV is of size min(Ny,Nu)-by-Nf.
%
%  TYPE is 0 to compute the singular values of H, 1 for inv(H), 
%  2 for I+H, and 3 for I+inv(H).
%
%  WSPEC specifies the frequency grid or range as follows:
%             [] :  none (auto-selected)
%    {fmin,fmax} :  user-defined frequency range (grid spans 
%                   this range)
%         vector :  user-defined frequency grid
%
%  ISPLOTTED is true when the data is used for plotting and false 
%  otherwise (interpolation is used only when ISPLOTTED=true).
%  See FREQFOCUS for details on the contents of FOCUSINFO.

%  Author(s): P. Gahinet
%   Copyright 1986-2008 The MathWorks, Inc.
Ts = abs(D.Ts);
RealFlag = isreal(D);
[w,Focus] = parseFreqSpec(D,wspec,RealFlag && isPlotted); % Focus always nonempty
AutoGrid = isempty(w);   
AutoFocus = any(isnan(Focus));

% Generate frequency grid if none specified
if AutoGrid
   % Use model dynamics to generate grid
   [z,p] = iodynamics(D);
   OPTS = struct('Focus',Focus,'Negative',~RealFlag,...
      'Decades',AutoFocus && ~isPlotted);
   w = freqgrid(z,p,Ts,4,OPTS);
else
   z = [];  p = [];
end

% Compute response and singular values
h = fresp(D,w);
sv = ltipack.getSV(h,type);

% Pick frequency focus
FocusInfo = freqfocus(Focus,4,w,permute(h,[3 1 2]),z,p,Ts,0,[]);

% Sort frequencies if response is plotted
if isPlotted
   [w,is] = sort(w);  sv = sv(:,is);
end
