%% Generate and Decode NB-IoT DL-SCH Transmissions
% This example shows how to transmit a bundle carrying the same transport block twice. The LLR soft bits from repeated subframes in a bundle are combined in structure dstate, the LLR soft bits from two bundles are combined in structure state. Note that dstate is reset before the re-transmission of the bundle.
%
% Specify the cell-wide settings and channel transmission configuration in parameter structures enb and chs.

enb.NNCellID = 0;
enb.NBRefP = 1;
enb.NFrame = 1;
chs.NSF = 3;
chs.NRep = 4;
chs.RNTI = 0;
chs.NPDSCHDataType = 'NotBCCH';

%
% Set the transport block length to 208 and the output codeword length to 960. Initialize the decoder states for the first HARQ transmission. Generate transport block data.

trblklen = 208;
outlen = 960;
estate = []; 
dstate = []; 
state = [];
trblkin = randi([0 1],trblklen,1);

%
% Generate the 960 NB-Iot DL-SCH codeword bits.

cw = lteNDLSCH(outlen,trblkin);

%
% Perform the initial transmission of a bundle containing the transport block. Verify if there are errors in the transmission.

for subframeIdx = 0:(chs.NSF*chs.NRep-1)
    enb.NSubframe = subframeIdx;
    [sym,estate] = lteNPDSCH(enb,chs,cw,estate);
    [rxcw, dstate] = lteNPDSCHDecode(enb,chs,sym,dstate);
end
[trblkout1,blkerr1,state] = lteNDLSCHDecode(trblklen,rxcw,state);
blkerr1

%
% Perform the re-transmission of the bundle containing the same transport block. The information obtained from the initial bundle transmission is saved in state and used as an input to lteNDLSCHDecode. Verify if there are errors in the re-transmission.

for subframeIdx = chs.NSF*chs.NRep:(2*chs.NSF*chs.NRep-1)
    enb.NSubframe = subframeIdx;
    [sym,estate] = lteNPDSCH(enb,chs,cw,estate);
    [rxcw,dstate] = lteNPDSCHDecode(enb,chs,sym,dstate);
end
[trblkout2,blkerr2,state] = lteNDLSCHDecode(trblklen,rxcw,state);
blkerr2

% Copyright 2012 The MathWorks, Inc.