%% Descramble PSBCH Codeword
% Descramble a received PSBCH codeword.
%% Scramble PSBCH Codeword
% Create a UE configuration structure and SL-BCH codeword.
% Generate the required length of the PRBS and scramble the PSBCH codeword with the PRBS sequence using xor.
% Modulate the logical scrambled data.

ue = struct('NSLID',2);
codeword = lteSLBCH(ue,ones(40,1));

psbchPrbs = ltePSBCHPRBS(ue,length(codeword));
scrambled = xor(psbchPrbs,codeword);

txsym = lteSymbolModulate(scrambled,'QPSK');

%
%% Descramble Recovered Codeword
% Add noise to transmitted symbols and demodulate received soft data.
% Generate the PSBCH PRBS in signed form.
% Descramble a vector of noisy demodulated symbols representing a sequence of soft bits. To do so, perform a pointwise multiplication between the PRBS sequence and the recovered data.
% Compare the transmitted codeword to the recovered codeword.

rxsym = awgn(double(txsym),30,'measured');
softdata = lteSymbolDemodulate(rxsym,'QPSK');

scramblingSeq = ltePSBCHPRBS(ue,length(softdata),'signed');

descrambled = softdata.*scramblingSeq;

isequal(codeword,descrambled > 0)

% The transmitted codeword matches the hard decision on the descrambled data.
% Copyright 2012 The MathWorks, Inc.