%% Decode Scrambled BCH Transport Block
% Generate a random sequence of binary values corresponding to a BCH transport block of 24 bits.

trblk = randi([0 1],24,1,'int8');  

% Specify the physical layer cell identity number as 321, the system frame number as 10, and the second half frame.

nid = 321;  
sfn = 10;   
hrf = 1;     

% Specify the number of candidate SS/PBCH blocks as 8. When you specify the number of candidate SS/PBCH blocks as 4 or 8, you can specify the subcarrier offset kssb as an input argument to the BCH encoder.

lssb = 8;                     
kssb = 18;               

% Encode the BCH transport block using the specified arguments.

bch = nrBCH(trblk,sfn,hrf,lssb,kssb,nid);

% Decode the encoded transport block and recover information by using a polar decoding list length of 8 bits.

listLen = 8;
[~,errFlag,rxtrblk,rxSFN4lsb,rxHRF,rxKssb] = nrBCHDecode( ...
   double(1-2*bch),listLen,lssb,nid);

% Verify that the decoding has no errors.

errFlag
isequal(trblk,rxtrblk)
isequal(bi2de(rxSFN4lsb','left-msb'),mod(sfn,16))
[isequal(hrf,rxHRF) isequal(de2bi(floor(kssb/16),1),rxKssb)]

% Copyright 2019 The MathWorks, Inc.