Comparing the Polar Coding results with NeoRadium

% To save a new set of files, you need to set the saveFiles to true.
saveFiles = true;
outPath = fileparts(matlab.desktop.editor.getActiveFilename);
disp(outPath);
/Users/shahab/code/Fireball/OpenSource/onGithub/Tests/Wireless/NewProject/OnGit/neoradium/Playground/CompareWithMatlab/Polar/MatlabFiles
 
s = rng(100); % Seed the RNG for repeatability
 
% Code parameters
K = 54; % Message length in bits, including CRC, K > 30
E = 120; % Rate matched output length, E <= 8192
 
EbNo = 0.8; % EbNo in dB
L = 8; % List length, a power of two, [1 2 4 8]
numFrames = 1000; % Number of frames to simulate
linkDir = 'DL'; % Link direction: downlink ('DL') OR uplink ('UL')
 
if strcmpi(linkDir,'DL')
% Downlink scenario (K >= 36, including CRC bits)
crcLen = 24; % Number of CRC bits for DL, Section 5.1, [6]
poly = '24C'; % CRC polynomial
nPC = 0; % Number of parity check bits, Section 5.3.1.2, [6]
nMax = 9; % Maximum value of n, for 2^n, Section 7.3.3, [6]
iIL = true; % Interleave input, Section 5.3.1.1, [6]
iBIL = false; % Interleave coded bits, Section 5.4.1.3, [6]
else
% Uplink scenario (K > 30, including CRC bits)
crcLen = 11;
poly = '11';
nPC = 0;
nMax = 10;
iIL = false;
iBIL = true;
end
if saveFiles
% Generate a random message
msg = randi([0 1],K-crcLen,1);
save(strcat(outPath,'/msg.mat'),'msg');
else
msg = load(strcat(outPath,'/msg.mat')).msg;
end
size(msg)
ans = 1×2
30 1
 
% Attach CRC
msgcrc = nrCRCEncode(msg,poly);
if saveFiles
save(strcat(outPath,'/msgcrc.mat'),'msgcrc');
end
size(msgcrc)
ans = 1×2
54 1
 
% Polar encode
encOut = nrPolarEncode(msgcrc,E,nMax,iIL);
if saveFiles
save(strcat(outPath,'/encOut.mat'),'encOut');
end
N = length(encOut)
N = 128
% Rate match
modIn = nrRateMatchPolar(encOut,K,E,iBIL);
if saveFiles
save(strcat(outPath,'/modIn.mat'),'modIn');
end
size(modIn)
ans = 1×2
120 1
% Modulate
modOut = nrSymbolModulate(modIn,'QPSK');
if saveFiles
save(strcat(outPath,'/modOut.mat'),'modOut');
end
size(modOut)
ans = 1×2
60 1
R = K/E; % Effective code rate
bps = 2; % bits per symbol, 1 for BPSK, 2 for QPSK
EsNo = EbNo + 10*log10(bps);
snrdB = EsNo + 10*log10(R) % in dB
snrdB = 0.3424
noiseVar = 1./(10.^(snrdB/10))
noiseVar = 0.9242
% Using AWGNChannel Channel
% chan = comm.AWGNChannel('NoiseMethod','Variance','Variance',noiseVar);
% rxSig = chan(modOut);
% size(rxSig)
 
if saveFiles
% The channel definition above does exactly the following. We may use the following code
% instead to be able save the noise file.
% Do the following to make random noise and save it to file to be used by python.
randDataI = randn(size(modOut), class(modOut));
randDataQ = randn(size(modOut), class(modOut));
chanNoise = noiseVar*(randDataI + 1i*randDataQ) / sqrt(2);
save(strcat(outPath,'/chanNoise.mat'),'chanNoise');
else
% Or just read the previously random noise to be consistent.
chanNoise = load(strcat(outPath,'/chanNoise.mat')).chanNoise;
end
rxSig = modOut + chanNoise;
size(rxSig)
ans = 1×2
60 1
% Or no noise at all:
% rxSig = modOut;
% size(rxSig)
 
% Soft demodulate
rxLLR = nrSymbolDemodulate(rxSig,'QPSK',noiseVar);
if saveFiles
save(strcat(outPath,'/rxLLR.mat'),'rxLLR');
end
size(rxLLR)
ans = 1×2
120 1
% Rate recover
decIn = nrRateRecoverPolar(rxLLR,K,N,iBIL);
if saveFiles
save(strcat(outPath,'/decIn.mat'),'decIn');
end
size(decIn)
ans = 1×2
128 1
% Polar decode
decBits = nrPolarDecode(decIn,K,E,L,nMax,iIL,crcLen);
if saveFiles
save(strcat(outPath,'/decBits.mat'),'decBits');
end
size(decBits)
ans = 1×2
54 1
strrep(num2str(reshape(decBits.',1,[])),' ','')
ans = '100100110110000110100110000010111100001010001110100010'