function varargout = plotSID(sid, figno, colors)
% plotSID         Plot Shift Invariant Dictionary 
%
% use:   plotSID(sid);
%        h = plotSID(sid, figno, colors);
% -------------------------------------------------------------------------
%   sid    The Shift Invariant Dictionary, a struct as in initSID
%   figno  Figure number to make plot in, if omitted a new figure is made.
%   colors A string that gives the colors to use for the atoms, default 'bgrcm'
% -------------------------------------------------------------------------
% ex:   Qm = [8,8,16,32,64]; Pm = [1,1,2,4,8]; sid = initSID(Qm,Pm); 
%       plotSID(sid, 1, 'k');

%--------------------------------------------------------------------------
% Made by Karl Skretting, University of Stavanger (UiS), 
% Department of Electrical Engineering and Computer Science (IDE)
% Mail:  karl.skretting@uis.no   Homepage:  http://www.ux.uis.no/~karlsk/
% 
% HISTORY:  dd.mm.yyyy
% Ver. 1.0  28.10.2015  KS: made file 
% Ver. 1.1  24.11.2015  KS: minor modifications 
% Ver. 1.2  14.04.2016  KS: minor modifications; argument bw added
%--------------------------------------------------------------------------

if ~isstruct(sid) || ~isfield(sid,'Type') || ~strcmp(sid.Type, 'SID')
    disp([mfilename,': First argument not a SID structure.']);
    h = [];
    
else
    if (nargin < 3) || ~ischar(colors)
        colors = 'bgrcm';
    end
    if (nargin < 2) || isempty(figno) || (figno <= 0)
        figno = figure;
    else
        figure(figno);
    end
    clf;
        
    subplot(121); hold on;
    for m = 1:sid.M
        atom = sid.freeVar(sid.i1(m):sid.i2(m));
        c = colors(rem(m-1,length(colors))+1);
        plot(1:sid.Qm(m), m + atom, [c,'-']);
        if (sid.M < 10)
            text(max(sid.Qm)-1, m+0.15, ['Q_',int2str(m),'= ',int2str(sid.Qm(m))], ...
                'Color','k', 'HorizontalAlignment','right' );
        end
    end
    title(['Atoms in SID,  M = ',int2str(sid.M)]);
    % ylabel(['Atom number, M = ',int2str(sid.M)]);
    xlabel('Sample number.');
    text(max(sid.Qm)-1, 0.2, ['Sum atom lengths  Q = ',int2str(sid.Q)], ...
                'Color','k', 'HorizontalAlignment','right' );
    axis([-1,max(sid.Qm),0,sid.M+1]);
    subplot(122); hold on; 
    top = max(sid.Qm + 6*sid.Pm);
    for m = 1:sid.M
        c = colors(rem(m-1,length(colors))+1);
        for i = 1:5;
            plot((m+0.1*i)*[1,1], [top-sid.Qm(m),top]-sid.Pm(m)*(i-1), [c,'-']);
        end
        for i = 6:8;
            plot((m+0.1*i), top-sid.Pm(m)*(i-1), [c,'.']);
        end
        text(m+0.2, 1.04*top, int2str(sid.Pm(m)), 'Color','k');
    end
    plot([1,1,sid.M+1,sid.M+1,1],[0,top,top,0,0], 'k-');
    text(1.2, 1.08*top, 'Shift (Pm) for each atom is:', 'Color','k');
    title('Structure in SID');
    axis([1,sid.M+1,0,1.1*top]);
    Ns = thisLCM(sid.Pm);
    Ksm = Ns./sid.Pm;
    Ks = sum(Ksm);
    text(1.2,-0.05*top,['N --> \infty.  K/N = ',int2str(Ks),'/',int2str(Ns),' = ',num2str(Ks/Ns),'.']);
    axis off;
    % locate figure in upper right part of screen
    a = get(0,'ScreenSize'); sw = a(3); sh = a(4);
    w = 850; h = 450; 
    set(gcf, 'Position',[sw-w-10,sh-h-70-20*min(figno,12),w,h]);
    % saveas(gcf, ['plotSIDfig',int2str(figno),'.png'], 'png');
end
    
if nargout >= 1
    varargout{1} = h;
end

end

%% subfunctions
function y = thisLCM(x)
% x may be a vector of several elements
% matlab lcm function allows only two input arguments, i.e it works
% elementwise on vectors and matrix inputs!
M = numel(x);
if M == 1
    y = x(1);
else
    y = lcm(x(1), x(2));
    for m = 3:M
        y = lcm(y, x(m));
    end
end
end
