%% Reset Soft Buffer in DL-SCH Decoder with Multiple HARQ Processes
% Generate a random sequence of binary values corresponding to one transport block of length 5120.

trBlkLen1 = 5120;
trBlk1 = randi([0 1],trBlkLen1,1,'int8');

% Create and configure a DL-SCH encoder System object with multiple HARQ processes.

encDL = nrDLSCH('MultipleHARQProcesses',true);

% Load the transport block into the DL-SCH encoder for HARQ process number 1 and codeword index 1.

harqID = 1;
cwID = 0;
setTransportBlock(encDL,trBlk1,cwID,harqID);

% Call the encoder with QPSK modulation scheme, 1 transmission layer, an output length of 10,240 bits, redundancy version 0, and HARQ process number 1. The encoder applies the DL-SCH processing chain to the transport block loaded into the object for HARQ process number 1.

rv = 0;
codedTrBlock1 = encDL('QPSK',1,10240,rv,harqID);

% Create and configure a DL-SCH decoder System object with multiple HARQ processes.

decDL = nrDLSCHDecoder('MultipleHARQProcesses',true);

% Configure the decoder for the encoded transport block.

decDL.TransportBlockLength = trBlkLen1;

% Add noise to the soft bits representing the encoded transport block. Call the DL-SCH decoder on the modified soft bits for HARQ process number 1.

rxSoftBits1 = awgn(1-2*double(codedTrBlock1),5);
[decBits1,blkErr1] = decDL(rxSoftBits1,'QPSK',1,rv,harqID);

% The added noise results in an error during decoding.

blkErr1

% Repeat the encoding operation for a new transport block of length 4400 and HARQ process number 1.

trBlkLen2 = 4400;
trBlk2 = randi([0 1],trBlkLen2,1,'int8');
setTransportBlock(encDL,trBlk2,cwID,harqID); 
codedTrBlock2 = encDL('QPSK',1,8800,rv,harqID);

% Configure the decoder for the second transport block.

decDL.TransportBlockLength = trBlkLen2;

% If an error occurred during the previous decoding with HARQ process number 1, you must reset the soft buffer of the HARQ process before decoding the second transport block.

if blkErr1   
    resetSoftBuffer(decDL,harqID);
end

% Call the decoder on the soft bits representing the second encoded transport block using HARQ process number 1.

rxBits2 = 1-2*double(codedTrBlock2);
[decBits2,blkErr2] = decDL(rxBits2,'QPSK',1,rv,harqID);
blkErr2

% Verify that the second transmitted and decoded message bits are identical.

isequal(decBits2,trBlk2)

% Copyright 2018-2019 The MathWorks, Inc.