% hRSMeasurementsExamplePlot plot for RSMeasurementsExample

% Copyright 2015-2016 The MathWorks, Inc.

function hRSMeasurementsExamplePlot(txRSRPs,rxRSRPs,detectedCells)
    
    nTimePeriods = size(txRSRPs,1);
    if (nTimePeriods)
        h = figure;
        hold on;
        plot(repmat((1:nTimePeriods).',size(rxRSRPs,2),1),rxRSRPs(:),'o','LineWidth',2);
        OSR = 10;
        cell1RSRPs = repmat(txRSRPs(:,1).',OSR,1);
        xtx = arrayfun(@(x)x-0.5+(1/(OSR+1):1/(OSR+1):1-(1/(OSR+1))),1:nTimePeriods,'UniformOutput',false);
        xtx = [xtx{:}];
        hcell1 = plot(xtx,cell1RSRPs(:),':','LineWidth',2);
        cell1color = hcell1.Color;
        cell2RSRPs = repmat(txRSRPs(:,2).',OSR,1);
        hcell2 = plot(xtx,cell2RSRPs(:),':','LineWidth',2);
        cell2color = hcell2.Color;
        txRSRPsFinite = txRSRPs(isfinite(txRSRPs(:)));
        plotRSRPs = unique([txRSRPsFinite; rxRSRPs(rxRSRPs>(min(txRSRPsFinite)-5.0))]);
        yMin = min(plotRSRPs)-1.5;
        yMax = max(plotRSRPs)+1.5;
        axis([0.5 nTimePeriods+0.5 yMin yMax]);
        title('TDD intra-frequency cell reselection');
        xlabel('Time period');
        ylabel('RSRP (dBm)');
        cellcolor = [cell1color; cell2color];
        for T = 1:nTimePeriods
            th = text(T,min(plotRSRPs)-0.75,sprintf('Selected:\nCell %d',detectedCells(T)));
            th.FontWeight = 'bold';
            th.Color = cellcolor(detectedCells(T),:)*3/4;
            th.HorizontalAlignment = 'center';
        end
        for t = 1.5:1:(nTimePeriods-0.5)
            plot([t t],[yMin yMax],'k:');
        end
        h.CurrentAxes.XTick = 1:nTimePeriods;
        h.CurrentAxes.XTickLabelMode = 'manual';
        h.CurrentAxes.XTickLabel = arrayfun(@(x)sprintf('T%d',x),1:nTimePeriods,'UniformOutput',false);
        legend('Measured RSRP','Ideal RSRP Cell 1','Ideal RSRP Cell 2');
    end

end
