% hMultiUserPDSCH generates the precoded PDSCH symbols for MU-MIMO 
% transmission with block diagonalization precoding.
% pdschSymbols = hMultiUserPDSCH(ENB,muPDSCH,CODEDTRBLK,HARQPROCESSES,HARQID)
% generates the precoded PDSCH symbols for a given ENB structure, PDSCH 
% transmission configuration cell array muPDSCH, coded transport block size
% array CODEDTRBLK, HARQPROCESSES and HARQ process ID HARQID for the 
% subframe. The steps followed are:
%  * Generate separate PDSCH symbols for each UE based on the data from
%    HARQ process and calculated precoding matrix for that UE.
%  * The PDSCH symbols corresponding to the UEs are combined.

% Copyright 2018 The MathWorks, Inc.

function pdschSymbols = hMultiUserPDSCH(enb,muPDSCH,CodedTrBlk,harqProcesses,harqID)

    numUsers = size(muPDSCH,1); % Number of active users
    pdschSymbols = 0;
    for userIdx = 1:numUsers
        muPDSCH{userIdx}.RVSeq = harqProcesses{userIdx}(harqID).txConfig.RVSeq;
        muPDSCH{userIdx}.RV = harqProcesses{userIdx}(harqID).txConfig.RV;
        dlschTransportBlk = harqProcesses{userIdx}(harqID).data;
        ncw = length(muPDSCH{userIdx}.Modulation);
        % Generating DL-SCH coded bits by performing complete channel coding
        % including CRC calculation, code block segmentation and CRC attachment,
        % turbo coding, rate matching and code block concatenation.
        dlsch = lteDLSCH(enb,muPDSCH{userIdx},CodedTrBlk(userIdx,1:ncw),...
            dlschTransportBlk);
        % Complex-valued modulated symbol generation for PDSCH in each UE. This
        % involves scrambling, modulation, layering and precoding processes.
        uePDSCH = ltePDSCH(enb,muPDSCH{userIdx},dlsch);
        % PDSCH symbols corresponding to users are combined
        pdschSymbols = pdschSymbols+uePDSCH;
    end
    
end

