%% Release 10 PUSCH Multiple Codeword Transmit and Receive Modeling
% This example demonstrates the multicodeword transmission and reception
% in the uplink.

% Copyright 2012-2018 The MathWorks, Inc.

%% Introduction
% This example shows how to implement multicodeword transmission and
% reception using LTE Toolbox(TM). This is done using Fixed Reference
% Channel (FRC) A3-2 as specified in TS36.104, Annex A3 [ <#7 1> ]. The
% configuration is then amended to transmit two identically configured
% codewords.

%% Setup
% This section sets the User Equipment (UE) configuration structure
% associated to FRC A3-2 and modifies it to use 2 codewords. The
% configuration for the two codewords is identical.

% Generate configuration for FRC A3-2
frc = lteRMCUL('A3-2');

% UE configuration
frc.TotSubframes = 1;   % Total number of subframes
frc.NTxAnts = 2;        % Number of transmit antennas

% Update Physical Uplink Shared Channel (PUSCH) configuration for 2
% identically configured codewords
frc.PUSCH.NLayers = 2;
frc.PUSCH.Modulation = repmat({frc.PUSCH.Modulation},1,2);
frc.PUSCH.RV = repmat(frc.PUSCH.RV,1,2);
frc.PUSCH.TrBlkSizes = repmat(frc.PUSCH.TrBlkSizes,2,1);

%% Encoding
% This section sets up the transport blocks and the Uplink Control
% Information (UCI). This is then coded to generate the Uplink Shared
% Channel (UL-SCH). The diagram below shows the operations performed
% internally by <matlab:doc('lteULSCH') lteULSCH>.
%
% <<../ULSCHProcessingChain.png>>
%
% PUSCH modulation is applied to the generated codewords. 

% Set up the transport block sizes and data for both codewords
TBSs = frc.PUSCH.TrBlkSizes(:,frc.NSubframe+1); % transport block sizes
trBlks = {(randi([0 1], TBSs(1), 1)) (randi([0 1], TBSs(2), 1))}; % data

% Set up UCI contents
CQI = [1 0 1 0 0 0 1 1 1 0 0 0 1 1].';
RI  = [0 1 1 0].';
ACK = [1 0].';

% UL-SCH coding including UCI coding
cws = lteULSCH(frc,frc.PUSCH,trBlks,CQI,RI,ACK);

% PUSCH modulation
puschSymbols = ltePUSCH(frc,frc.PUSCH,cws);

%% Decoding
% This section demodulates the PUSCH and applies channel decoding. The
% resulting UCI is then decoded to produce the received Channel Quality
% Indicator (CQI), Rank Indication (RI) and Acknowledgment (ACK).

% PUSCH demodulation
ulschInfo = lteULSCHInfo(frc,frc.PUSCH,TBSs,length(CQI),length(RI),...
            length(ACK),'chsconcat');    % Get UL-SCH information
llrs = ltePUSCHDecode(frc,ulschInfo,puschSymbols); % Decode PUSCH

% UL-SCH decoding
softBuffer = [];
[rxtrblks,crc,softBuffer] = lteULSCHDecode(frc,ulschInfo,TBSs,llrs,...
                            softBuffer);

% UCI decoding
[llrsData,llrsCQI,llrsRI,llrsACK] = lteULSCHDeinterleave(frc,ulschInfo,...
                                    llrs);
rxCQI = lteCQIDecode(ulschInfo,llrsCQI);    % Decode CQI
rxRI = lteRIDecode(ulschInfo,llrsRI);       % Decode RI
rxACK = lteACKDecode(ulschInfo,llrsACK);    % Decode ACK

%% Results
% The decoded CRC for both codewords is displayed. The transmitted and
% received CQI, RI and ACK bits are also shown.

hULMulticodewordTxRxDisplayResults(crc,CQI,RI,ACK,rxCQI,rxRI,rxACK);

%% Appendix 
% This example uses the following helper function:
%
% * <matlab:edit('hULMulticodewordTxRxDisplayResults.m') hULMulticodewordTxRxDisplayResults.m>

%% Selected Bibliography
% # 3GPP TS 36.104 "Base Station (BS) radio transmission and reception"
