function varargout = drawanalysisplot(fun,varargin)
%DRAWANALYSISPLOT Draw analysis plot.
%  DRAWANALYSISPLOT(FUN,...) is a gateway to the analysis plot drawing
%  functions, where FUN is a local function name.

%   Thomas A. Bryan
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.11 $  $Date: 2000/04/19 17:28:43 $

if nargout
  [varargout{1:nargout}] = feval(fun,varargin{:});
else
  feval(fun,varargin{:});
end
%----------------------------------------------------------------------
function magresp(ax,W,H,S,xunits,yunits)
%MAGRESP Magnitude response
%   MAGRESP(AX,W,H,S,XUNITS,YUNITS) is the magnitude response, where AX
%   is a vector of axes handles, W is the frequency vector, H is the complex
%   frequency response, S is the structure of options for FREQZPLOT, YUNITS
%   are the magnitude units, and XUNITS are the frequency units.
axes(ax(1));
S.plot='mag';
S.yunits=yunits;
S.xunits=xunits;
S.ax = ax(1);
freqzplot(H,W,S);
set(ax(1),'visible','on')

%----------------------------------------------------------------------
function phaseresp(ax,W,H,S,xunits,yunits)
%PHASERESP Phase response
%   PHASERESP(AX,W,H,S,XUNITS,YUNITS) is the phase response, where AX
%   is a vector of axes handles, W is the frequency vector, H is the complex
%   frequency response, S is the structure of options for FREQZPLOT, YUNITS
%   are the magnitude units, and XUNITS are the frequency units.
axes(ax(1));
S.plot='phase';
S.yunits=yunits;
S.xunits=xunits;
S.ax = ax(1);
freqzplot(H,W,S);
set(ax(1),'visible','on')

%----------------------------------------------------------------------
function hline=magnphaseresp(ax,W,H,S,xunits,yunits,xlbl)
%MAGNPHASERESP Magnitude and phase response
%   MAGNPHASERESP(AX,W,H,S,XUNITS,YUNITS) is the magnitude and phase
%   response, where AX is a vector of axes handles, W is the frequency vector, H
%   is the complex frequency response, S is the structure of options for
%   FREQZPLOT, YUNITS are the magnitude units, and XUNITS are the frequency
%   units.

switch S.xunits
  case 'rad/sample'
    W = W/pi;
    W(end)=1;
end
switch yunits
  case 'db'
    mag = 20*log10(abs(H));
    maglabel = 'Magnitude (dB)';
  case 'squared'
    mag = abs(H).^2;
    maglabel = 'Magnitude squared'; 
  otherwise
    mag = abs(H);
    maglabel = 'Magnitude';
end
ph  = unwrap(angle(H))*180/pi;

% Magnitude plot
axes(ax(1));
cla
% Color order
hFig = get(ax(1),'Parent');
co = get(hFig,'defaultaxescolororder');
nco = 1;

for k=1:size(mag,2)
  hline(nco) = line(W,mag(:,k),'color',co(nco,:));
  nco = nco+1;
end
myylabel(ax(1),maglabel);
grid on

% Phase plot
axes(ax(2));
cla
for k=1:size(mag,2)
  hline(nco) = line(W,ph(:,k),'color',co(nco,:));
  nco = nco+1;
end
set(ax(2),'YAxisLocation','right','color','none', ...
          'xgrid','off','ygrid','off','box','off');
set(ax,'xlim',[W(1),W(end)])
myxlabel(ax(2),xlbl,'interpreter','tex');

myylabel(ax(2),'Phase (degrees)');
set(ax,'visible','on','box','on');

%----------------------------------------------------------------------
function hline = groupdelay(ax,W,Gd,xlbl)
%GROUPDELAY Group delay
%   GROUPDELAY(AX,W,Gd,XUNITS) is the group delay, where AX is a vector of
%   axes handles, W is the frequency vector, Gd is the group delay, and
%   XUNITS are the frequency units.
axes(ax(1));
hline = line(W,Gd);
set(ax(1),'xlim',[W(1),W(end)])
grid on
myylabel(ax(1),'Group delay (in samples)');
myxlabel(ax(1),xlbl,'interpreter','tex');
set(ax(1),'visible','on','box','on');

%----------------------------------------------------------------------
function hline = impresp(ax,T,H,xunits)
%IMPRESP Impulse response
%   IMPRESP(AX,T,H,XUNITS) is the impulse response, where AX is a vector of
%   axes handles, T is the time vector, H is the impulse response, and
%   XUNITS are the time units.
axes(ax(1));
hline = line(T,real(H));
set(hline(1),'linestyle','-','marker','o')
if length(hline)>1
  set(hline(2),'linestyle','-','marker','s')
end
set(ax(1),'xlim',[T(1),T(end)])
grid on
myxlabel(ax(1),'Samples')
myylabel(ax(1),'Impulse response')
set(ax(1),'visible','on','box','on');

%----------------------------------------------------------------------
function hline = stepresp(ax,T,H,xunits)
%STEPRESP Step response
%   STEPRESP(AX,T,H,XUNITS) is the step response, where AX is a vector of
%   axes handles, T is the time vector, H is the step response, and
%   XUNITS are the time units.
axes(ax(1));
hline = line(T,real(H));
set(hline(1),'linestyle','-','marker','o')
if length(hline)>1
  set(hline(2),'linestyle','-','marker','s')
else
end
set(ax(1),'xlim',[T(1),T(end)])
grid on
myxlabel(ax(1),'Samples')
set(ax(1),'visible','on','box','on');

%----------------------------------------------------------------------
function hline = pzplot(ax,varargin)
%PZPLOT  Pole-zero plot
%    PZPLOT(AX,P1,Z1) or PZPLOT(AX,P1,Z1,P2,Z2) plots the poles and zeros
%    found in P1, Z1, ..., respectively.
axes(ax(1));
hline=[];
for k=1:3:length(varargin)
  r = varargin{k};
  marker = varargin{k+1};
  color = varargin{k+2};
  hline1 = ...
      line(real(r),imag(r),'linestyle','none','marker',marker,'color',color);
  hline = [hline hline1];
end
circle = exp(i*2*pi*linspace(0,1,100));
hline2 = line(real(circle),imag(circle),'linestyle',':','color','b');
axis equal
lim = axis;
hline3 = line(1.1*lim(1:2),[0 0],'linestyle',':','color','b');
hline4 = line([0 0],1.1*lim(3:4),'linestyle',':','color','b');
axis equal
set(ax(1),'visible','on','box','on');

%----------------------------------------------------------------------
function myxlabel(ax,str,varargin)
%MYXLABEL Set the XLABEL on specified axis. 
%   MYXLABEL(AX,STR) sets the xlabel on axis AX to STR.  The difference
%   between MYXLABEL and XLABEL is that XLABEL calls GCA, which we need to
%   avoid in a GUI.
%
%   MYXLABEL(AX,STR,PN,PV,...) passes through optional parameter-value pairs
%   to setting the axis.
set(get(ax,'xlabel'),'string',str,varargin{:})

%----------------------------------------------------------------------
function myylabel(ax,str,varargin)
%MYYLABEL Set the YLABEL on specified axis.
%   MYYLABEL(AX,STR) sets the ylabel on axis AX to STR.  The difference
%   between MYYLABEL and YLABEL is that YLABEL calls GCA, which we need to
%   avoid in a GUI.
%
%   MYYLABEL(AX,STR,PN,PV,...) passes through optional parameter-value pairs
%   to setting the axis.
set(get(ax,'ylabel'),'string',str,varargin{:})
