%% Connect UL-SCH Encoder and Decoder Back to Back
% Generate a random sequence of binary values corresponding to one transport block of length 5120.

trBlkLen = 5120;
trBlk = randi([0 1],trBlkLen,1,'int8');

% Create and configure an UL-SCH encoder System object with the specified target code rate.

targetCodeRate = 567/1024;
encUL = nrULSCH;
encUL.TargetCodeRate = targetCodeRate;

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

setTransportBlock(encUL,trBlk);

% Call the encoder with 64-QAM modulation scheme, 1 transmission layer, an output length of 10,240 bits, and redundancy version 0. The encoder applies the UL-SCH processing chain to the transport block loaded into the object.

mod = '64QAM';
nLayers = 1;
outlen = 10240;
rv = 0;
codedTrBlock = encUL(mod,nLayers,outlen,rv);

% Create and configure an UL-SCH decoder System object.

decUL = nrULSCHDecoder;
decUL.TargetCodeRate = targetCodeRate;
decUL.TransportBlockLength = trBlkLen;

% Call the UL-SCH decoder on the soft bits representing the encoded transport block. Use the configuration parameters specified for the encoder. The error flag in the output indicates that the block decoding does not have errors.

rxSoftBits = 1.0 - 2.0*double(codedTrBlock);
[decbits,blkerr] = decUL(rxSoftBits,mod,nLayers,rv)

% Verify that the transmitted and received message bits are identical.

isequal(decbits,trBlk)

% Copyright 2018-2019 The MathWorks, Inc.