function class08()
%CLASS02  Displays plots for ECE 3500 class
%   CLASS02()  creates all the plots for class lecture notes through the
%   use of Dr. Harley's plotforclass MATLAB function. To get the full
%   results from the lecture notes, this function requires the "export_fig"
%   and "oaxes" functions from the MathWorks File Exchange. 
%
%   INPUTS: none
%
%   OUTPUTS: none
%
%   see also: plot, stem, subplot, figure

close all;

addpath('../matlab/altmany-export_fig-8016f6a')
addpath('../matlab/')
namebase = ['./' ];

Ts = 0.01; Fs = 1/Ts;

N = 20;
n = 1;

% STABLE 
z{n} = [1 nan];
p{n} = [0.5 2 ];
roca{n} = [ 1 -1]; 
K{n} = 1;
n = n + 1;

% UNSTABLE 
z{n} = [1 nan];
p{n} = [0.5 2 ];
roca{n} = [ -1 1]; 
K{n} = 1;
n = n + 1;

% UNSTABLE 
z{n} = [1 nan];
p{n} = [0.5 2 ];
roca{n} = [ 1 1]; 
K{n} = 1;
n = n + 1;

% UNSTABLE 
z{n} = [1 nan];
p{n} = [0.5 2 ];
roca{n} = [ -1 -1]; 
K{n} = 1;
n = n + 1;

% STABLE 
z{n} = [0 nan];
p{n} = [0.2  nan ];
roca{n} = [1 1]; 
K{n} = 1;
n = n + 1;

z{n} = [0 nan];
p{n} = [0.4  nan ];
roca{n} = [1 1]; 
K{n} = 1;
n = n + 1;

z{n} = [0 nan];
p{n} = [0.6  nan ];
roca{n} = [1 1]; 
K{n} = 1;
n = n + 1;

z{n} = [0 nan];
p{n} = [0.8  nan ];
roca{n} = [1 1]; 
K{n} = 1;
n = n + 1;

z{n} = [0 nan];
p{n} = [1  nan ];
roca{n} = [1 1]; 
K{n} = 1;
n = n + 1;

z{n} = [0 nan];
p{n} = [1.2  nan ];
roca{n} = [1 1]; 
K{n} = 1;
n = n + 1;



M = n-1;

n = 1;


for m = 1:M
    sys = zpk(z{m}(~isnan(z{m})),p{m}(~isnan(p{m})),K{m},1); 
    [num,den] = tfdata(sys);

    fnc{n} = @plot; x{n} = [real(p{m}).' real(z{m}).'];    y{n} = [imag(p{m}).' imag(z{m}).'];
    xlbl{n} = 'Re'; ylbl{n} = 'Im'; ynum{n} = false;  
    markersize{n} = 10; roc{n} = roca{m};
    axis{n} = [-max([max(abs((x{n}))) max(abs((x{n}))) 1]) max([max(abs(real(x{n}))) max(abs((x{n}))) 1]) -max([max(abs((x{n}))) max(abs((x{n}))) 1]) max([max(abs((x{n}))) max(abs((x{n}))) 1])];
    pzzplot{n} = true; figwidth{n} = 3.33; figheight{n} = 3.0;
    %textloc{n} = {[-1 1.5]}; textcont{n} = {['\bf System ' num2str(m)]};
    if (sum(p{m} == 0) > 1)
        textloc{n} = [0.1 0.1]; textcont{n} = ['\times ' num2str((sum(p{m} == 0)))];
    end
    caption{n} = 'Pole-zero plot';
    n = n + 1;
    
    sys = zpk(z{m}(~isnan(z{m})),p{m}(~isnan(p{m})),K{m},1); 
    [num,den] = tfdata(sys);
    
    index = find(den{1} ~= 0, 1, 'first');
    den{1} = den{1}(index : end);
    
    [r0,p0,k0] = residue(num{1},den{1});
    [~,indx0] = sort(p0); p0 = p0(indx0); r0 = r0(indx0);
    [~,indx1] = sort(p{m}); roc1 = roca{m}(indx1); p1 = p{m}(indx1);
    
    x0 = [zeros(N,1); 1; zeros(N,1)];
    x1 = [zeros(N+1,1); 1; zeros(N-1,1)];   % SHIFTED BY 1
    y{n} = zeros(2*N+1,1);
    for ii = 1:length(r0)
        if roc1(ii) == 1 || p0(ii)==0
            y{n} = y{n} + filter([r0(ii)],[1, -p0(ii)], x1);
        else
            y{n} = y{n} + flipud(filter([-r0(ii)/p0(ii)],[1, -1/p0(ii)], x0));
        end
    end
    if ~isempty(k0)
        y{n} = y{n} + filter(k0,1, x0);
    end
    
    fnc{n} = @stem; x{n} = (-20+1-index):(21-index);
    xlbl{n} = 'n'; ylbl{n} = 'h[n]'; figwidth{n} = 2.4; 
    hidexticks{n}  = true; ynum{n} = false; hideyticks{n}  = false; 
    axis{n} = [-20 20 -max(abs(y{n})) max(abs(y{n}))]; yTick{n} = [-max(abs(y{n})) max(abs(y{n}))];
    caption{n} = 'Impulse Response';
    n = n + 1;
    
end


pltopt{n} = [];
figheight{n} = [];
axis{n} = [];
figwidth{n} = [];
hidexticks{n}  = [];
hideyticks{n}  = [];
marker{n} = [];
markersize{n} = [];
xTickPrefix{n} = [];
xTickSuffix{n} = [];
yTickPrefix{n} = [];
yTickSuffix{n} = [];
LineStyleOrder{n} = [];
ynum{n} = [];
pzplot{n} = [];
roc{n} = [];
myColorMap{n} = [];
xTick{n} = []; 
yTick{n} = [];
pzzplot{n} = [];
textloc{n} = [];
textcont{n} = '';
caption{n} = [];

for n = 1:length(x)
    plotforclass( 'fnc', fnc{n}, 'x', x{n}, 'y', y{n}, ...
                  'flname', [namebase 'fig' num2str(n, '%03u')], ...
                  'xlbl', xlbl{n}, 'ylbl', ylbl{n}, 'figwidth', figwidth{n}, ...
                  'hidexticks', hidexticks{n}, 'hideyticks', hideyticks{n}, 'marker', marker{n}, ...
                  'markersize', markersize{n}, 'axis', axis{n}, ...
                  'xTickPrefix', xTickPrefix{n}, 'yTickPrefix', yTickPrefix{n}, ...
                  'yTickSuffix', yTickSuffix{n}, 'figheight', figheight{n}, ...
                  'xTickSuffix', xTickSuffix{n}, 'LineStyleOrder', LineStyleOrder{n}, ...
                  'pltopt', pltopt{n}, 'ynum', ynum{n}, 'pzplot', pzplot{n}, 'roc', roc{n}, ...
                  'myColorMap', myColorMap{n}, 'xTick', xTick{n}, 'yTick', yTick{n}, ...
                  'pzzplot', pzzplot{n}, 'textloc', textloc{n}, 'textcont', textcont{n}, ...
                  'caption', caption{n}, 'captionFile', [namebase 'caption' num2str(n, '%03u') '.html']);
end



