%% Transmit and Decode Polar Encoded Data
% Transmit polar-encoded block of data and decode it using successive-cancellation list decoder.
%% Initial Setup
% Create a channel that adds white Gaussian noise (WGN) to an input signal. Set the noise variance to 1.5.

nVar = 1.5; 
chan = comm.AWGNChannel('NoiseMethod','Variance','Variance',nVar);

% Create a binary phase shift keying (BSPK) modulator and demodulator.

bpskMod = comm.BPSKModulator;
bpskDemod = comm.BPSKDemodulator('DecisionMethod', ...
    'Approximate log-likelihood ratio','Variance',nVar);

%% Simulate a Frame
% Perform polar encoding of a random message of length K. The rate-matched output is of length E.

K = 132;
E = 256;
msg = randi([0 1],K,1,'int8');
enc = nrPolarEncode(msg,E);

% Modulate the polar encoded data using BSPK modulation, add WGN, and demodulate.

mod = bpskMod(enc);
rSig = chan(mod);
rxLLR = bpskDemod(rSig); 

% Perform polar decoding using successive-cancellation list decoder of length L.

L = 8;
rxBits = nrPolarDecode(rxLLR,K,E,L);

% Determine the number of bit errors.

numBitErrs = biterr(rxBits,msg);
disp(['Number of bit errors: ' num2str(numBitErrs)])

% The transmitted and received messages are identical.
% Copyright 2018 The MathWorks, Inc.