%hEPDCCHGenerationPlot resource grid plot for EPDCCH generation example
%   hEPDCCHGenerationPlot(ENB,CHS) plots resource element assignments for
%   EPDCCH, EPDCCH DMRS, CRS, CSI-RS and the legacy control region given
%   cell-wide configuration structure ENB and EPDCCH-specific channel
%   configuration structure CHS.

%   Copyright 2014 The MathWorks, Inc.

function hEPDCCHGenerationPlot(enb,chs)
   
    % create an empty resource grid for all 4 EPDCCH antenna ports
    grid = lteDLResourceGrid(enb,4);
    K = size(grid,1);
    L = size(grid,2);
    
    % create and map colors for the EPDCCH, its DMRS and the CRS
    epdcchIndices = lteEPDCCHIndices(enb,chs);
    grid(epdcchIndices) = 1;
    epdcchDmrsIndices = lteEPDCCHDMRSIndices(enb,chs);
    grid(epdcchDmrsIndices) = 2;
    grid(lteCellRSIndices(enb)) = 3;
    legends = {'unused','EPDCCH','EPDCCH DMRS','CRS'}; 
    patches = 0:3;
    
    % If appropriate, create and map color for the legacy control region,
    % the region used by PDCCH, PCIFCH and PHICH
    if (isfield(chs,'EPDCCHStart'))
        if (chs.EPDCCHStart>0)
            if (enb.NDLRB<=10)
                enb.CFI = min(chs.EPDCCHStart-1,3);
            else
                enb.CFI = min(chs.EPDCCHStart,3);
            end
        else
            if (isfield(enb,'CFI'))
                enb = rmfield(enb,'CFI');
            end
        end
    end
    if (isfield(enb,'CFI'))
        duplexingInfo = lteDuplexingInfo(enb);
        if (duplexingInfo.NSymbolsDL)
            grid(ltePDCCHIndices(enb,[])) = 4;
        end
        legends = [legends ,'Legacy Control'];
        patches = [patches 4];
    end
    
    % create and map color for the CSI-RS
    grid(lteCSIRSIndices(enb)) = 5;
    legends = [legends 'CSI-RS'];
    patches = [patches 5];    
    
    % reduce multi-antenna array to a single plane and mark out any cases
    % where channels/signals are co-located
    grid = sum(grid,3)./sum(grid~=0,3);
    grid(isnan(grid)) = 0;
    grid(rem(grid,1)~=0) = 6;
    
    % establish set of resource blocks 'rbs' in use for EPDCCH
    [resubs,~,~] = ind2sub([K L 4],[epdcchIndices; epdcchDmrsIndices]);
    rbsubs = floor((resubs-1)/12)+1;
    rbs = unique(rbsubs);

    % set up axis ranges for the set of resource blocks 'rbs' and one
    % subframe
    if (~isempty(rbs))
        y = min(rbs-1)*12:max(rbs)*12-1;
    else
        y = 0:11;
    end
    x = 0:L-1;
    
    % plot grid and set up colormap, plot axes and patches of appropriate
    % colors outside the axes to facilitate adding the legend
    figure;
    hold on;
    for i = patches
        patch([-2 -3 -3 -2],[-2 -2 -3 -3],i);
    end 
    image(x,y,grid(y+1,x+1)+1);
    colormap([0.94 0.94 0.94; 0.93 0.69 0.13; 0.47 0.67 0.19; ...
              0.88 0.35 0.13; 0.66 0.35 0.73; 0.13 0.57 0.86; ...
              0.25 0.25 0.25]);    
    caxis([0 6]);
    axis([x(1)-1 x(end)+1 y(1)-1 y(end)+1]);
    view([0 -90]);
    
    % set up axis ticks and add gridlines
    a = gca;
    a.XTick = x;
    a.YTick = y;
    for i=[x x(end)+1]
        h = line([i i]-0.5, ...
               [y(1)-0.5-(y(1)~=0)*0.25 y(end)+0.5+(y(end)~=(K-1))*0.25]);
        h.Color = [0.25 0.25 0.25];
    end
    for i=[y y(end)+1]
        h = line([x(1)-0.5 x(end)+0.5],[i i]-0.5);
        h.Color = [0.25 0.25 0.25];
    end
    
    % Add plot labels and legend
    xlabel('OFDM symbol index (0-based)');
    ylabel('Subcarrier index (0-based)');    
    box on;
    legend(legends,'Location','NorthEast');
    title('Resource grid RE assignment');

end
