%% Generate Codeword with Coded UL-SCH Data and Coded UCI
% Create a PUSCH configuration object with a pi/2-BPSK modulation scheme and no frequency hopping. Set the beta offset factor for HARQ-ACK to 20, and the beta offset factor for CSI part 1 and CSI part 2 to 6.25 each. Specify the scaling factor as 0.8, which limits the number of resource elements (REs) assigned for the UCI.

pusch = nrPUSCHConfig;
pusch.Modulation = 'pi/2-BPSK';
pusch.FrequencyHopping = 'neither';
pusch.BetaOffsetACK = 20;
pusch.BetaOffsetCSI1 = 6.25;
pusch.BetaOffsetCSI2 = 6.25;
pusch.UCIScaling = 0.8;

% Set the target code rate, payload lengths of the UL-SCH data, HARQ-ACK, CSI part 1, and CSI part 2.

tcr = 0.5;   % Target code rate
tbs = 1032;  % Payload length of UL-SCH data (transport block size)
oack = 8;    % Payload length of HARQ-ACK
ocsi1 = 88;  % Payload length of CSI part 1
ocsi2 = 720; % Payload length of CSI part 2

% Get the rate matched lengths of the data, HARQ-ACK, CSI part 1, and CSI part 2.

rmInfo = nrULSCHInfo(pusch,tcr,tbs,oack,ocsi1,ocsi2);

% Create the random payload bits for the UL-SCH data, HARQ-ACK, CSI part 1, and CSI part 2.

data = randi([0 1],tbs,1);
ack  = randi([0 1],oack,1);
csi1 = randi([0 1],ocsi1,1);
csi2 = randi([0 1],ocsi2,1);

% Create a UL-SCH encoder System object.

encUL = nrULSCH;

% Load the transport block into the UL-SCH encoder.

setTransportBlock(encUL,data);

% Get the coded bits of length rmInfo.GULSCH by calling the encoder.

rv = 0; % Redundancy version is 0
culsch = encUL(pusch.Modulation,pusch.NumLayers,rmInfo.GULSCH,rv);

% Encode the random payload of the HARQ-ACK, CSI part 1, and CSI part 2 for the rate-matched output lengths obtained from the rmInfo structure.

cack  = nrUCIEncode(ack,rmInfo.GACK,pusch.Modulation);
ccsi1 = nrUCIEncode(csi1,rmInfo.GCSI1,pusch.Modulation);
ccsi2 = nrUCIEncode(csi2,rmInfo.GCSI2,pusch.Modulation);

% Get the codeword from the coded bits of the UL-SCH and the coded bits of UCI types.

[cw,info] = nrULSCHMultiplex(pusch,tcr,tbs,culsch,cack,ccsi1,ccsi2)

% Copyright 2020 The MathWorks, Inc.