%% Decode SL-SCH Using HARQ Soft Combining
% Use soft combining while decoding the sequence of four transmissions used to send every transport block on the SL-SCH. The rate matching and noise level are set so that successful decoding of the block requires multiple transmissions.
%% Initialize parameters
% Create a UE settings structure for the SL-SCH.
% Generate a transport block of 100 random bits.
% Create a local variable specifying an SL-SCH bit capacity of 288.
% Define the fixed redundancy version sequence used by the HARQ process.
% Clear the HARQ process decoding state.

ue = struct('CyclicPrefixSL','Normal','Modulation','QPSK');
trblk = randi([0 1],100,1);
bitcapacity = 288;
rvseq = [0 2 3 1];
decstate = [];

%
%% Transmit and recover the SL-SCH transport block
% Send the transport block four times.
% Display result of decoding successive transmissions.

for i = 1:4
    % Encode information bits with the next RV value.
    ue.RV = rvseq(i);
    cw = lteSLSCH(ue,bitcapacity,trblk);
    
    % Modulate the codeword and add noise.
    sym = awgn(lteSymbolModulate(cw,ue.Modulation),-4,'measured');
    softdata = lteSymbolDemodulate(sym,ue.Modulation);
    
    % Decode the current transmission and combine with decoding state.
    [rxtrblk,err,decstate] = lteSLSCHDecode(ue,length(trblk), ...
        softdata,decstate);
    X = ['Decoding error ', num2str(err), ' for transmission #', ...
        num2str(i), ' with RV ', num2str(ue.RV)];
    disp(X)
end

% The soft-combined data is recovered without error on the third transmission.
% Copyright 2012 The MathWorks, Inc.