Skip to content

Commit

Permalink
add nrRateMatchPolar() (#72)
Browse files Browse the repository at this point in the history
* add nrRateMatchPolar(), until now only repetition is supported and ibil has to be disabled
* bump version to 0.4.0
  • Loading branch information
catkira committed Mar 15, 2024
1 parent 4d65406 commit 5ae6f5f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions py3gpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .nrPBCHPRBS import nrPBCHPRBS
from .nrEqualizeMMSE import nrEqualizeMMSE
from .nrRateRecoverPolar import nrRateRecoverPolar
from .nrRateMatchPolar import nrRateMatchPolar
from .nrPolarDecode import nrPolarDecode
from .nrPolarEncode import nrPolarEncode
from .nrCRCEncode import nrCRCEncode
Expand Down
48 changes: 48 additions & 0 deletions py3gpp/nrRateMatchPolar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import numpy as np

def subblock_interleaving(u):
N = u.shape[-1]
assert np.mod(N,32)==0, \
"length for sub-block interleaving must be a multiple of 32."
y = np.zeros_like(u)
# Permutation according to Tab 5.4.1.1-1 in 38.212
perm = np.array([0, 1, 2, 4, 3, 5, 6, 7, 8, 16, 9, 17, 10, 18, 11, 19,
12, 20, 13, 21, 14, 22, 15, 23, 24, 25, 26, 28, 27,
29, 30, 31])

for n in range(N):
i = int(32*n/N)
j = int(perm[i] * N/32 + np.mod(n, N/32))
y[n] = u[j]

return y

def nrRateMatchPolar(enc, K, E, ibil=False):
# TODO: for what is the parameter K??
N = enc.shape[0]
d = np.zeros(N, int)
d = enc[subblock_interleaving(np.arange(N))]
y = np.zeros(E, int)
if N <= E:
# repetition
y[:N] = d[:N]
for k in range(N, E):
y[k] += d[k%N]
else:
# TODO: implement shortening und puncturing
print("Error!")
exit()
if ibil == True:
print("Error: interleaving of coded bits is not implemented!")
exit()
return y

if __name__ == '__main__':
import py3gpp
# input = np.zeros(512, int)
# input[0:3] = [1 , 1, 1]
input = np.array([1,1,0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0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,1,0,1,0,1,1,1,1,0,0,0,0,1,1,0,1,0,0,0,1,1,0,0,1,0,1,0,1,0,0,1,0,1,0,0,1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,0,1,0,0,1,1,1,1,0,0,1,0,1,1,1,1,1,1,0,0,0,0,1,1,0,0,0,0,1,1,0,1,1,0,1,1,0,1,1,1,0,1,1,0,1,0,0,1,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,1,0,1,0,1,1,0,1,0,0,0,1,0,1,0,0,1,1,0,0,1,0,0,0,1,0,0,0,1,1,0,1,0,1,1,1,1,0,1,1,0,1,1,1,0,1,1,1,1,1,0,0,0,1,0,0,0,0,1,1,0,1,0,1,1,1,1,1,0,1,1,1,1,1,1,0]).astype(int)
out = nrRateMatchPolar(input, 0, 863, False)
assert np.array_equal(out, out_desired)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ build-backend = "setuptools.build_meta"
py3gpp = ["codes/*.csv"]

[project]
version = "0.3.6"
version = "0.4.0"
authors = [
{name = "Benjamin Menküc", email = "benjamin@menkuec.de"},
]
Expand Down
8 changes: 8 additions & 0 deletions tests/test_nrRateMatchPolar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import numpy as np
from py3gpp.nrRateMatchPolar import nrRateMatchPolar

def test_nrRateMatchPolar():
input = np.array([1,1,0,0,0,0,1,1,0,0,1,1,1,0,1,0,0,1,1,1,0,1,0,1,1,1,0,0,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,0,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0,1,1,1,1,0,1,1,0,0,1,1,1,0,0,1,0,0,0,0,0,1,1,0,0,1,1,0,1,1,1,1,0,0,1,1,1,0,0,0,0,0,1,1,1,1,0,1,1,1,1,0,0,1,0,1,0,1,1,0,0,1,0,1,1,1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