%% Connect DL-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 a DL-SCH encoder System object with the specified target code rate.

targetCodeRate = 567/1024;
encDL = nrDLSCH;
encDL.TargetCodeRate = targetCodeRate;

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

setTransportBlock(encDL,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 DL-SCH processing chain to the transport block loaded into the object.

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

% Create and configure a DL-SCH decoder System object.

decDL = nrDLSCHDecoder;
decDL.TargetCodeRate = targetCodeRate;
decDL.TransportBlockLength = trBlkLen;

% Call the DL-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] = decDL(rxSoftBits,mod,nLayers,rv)

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

isequal(decbits,trBlk)

% Copyright 2018-2019 The MathWorks, Inc.