Skip to content

Commit

Permalink
Fix nrPolarDecode() and add more tests (#71)
Browse files Browse the repository at this point in the history
* fix minsum function in nrPolarDecode()
* fix wrong bpsk mod
* add test_nrPolarDecode.py and test_nrPolarEncode_nrPolarDecode.py
* bump version to 0.3.6
  • Loading branch information
catkira committed Mar 11, 2024
1 parent d6ffce1 commit 4d65406
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 11 deletions.
65 changes: 55 additions & 10 deletions py3gpp/nrPolarDecode.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import numpy as np
from .helper import generate_5g_ranking
from .helper import polar_precode_interleave
from py3gpp.helper import generate_5g_ranking
from py3gpp.helper import polar_precode_interleave

# very simple successive cancellation decoder
def Polar_SC_decoder(N, frozen_pos, r):
n = np.log2(N).astype("int")
n = np.log2(N).astype(int)
L = np.zeros((n + 1, N))
ucap = np.zeros((n + 1, N))
ns = np.zeros(2 * N - 1, "int")
ns = np.zeros(2 * N - 1, int) # node state vector
L[0, :] = r
node = depth = done = 0
while done == 0:
# print(f'd = {depth}, node = {node}')
if depth == n:
ucap[n, node] = 0 if (L[n, node] >= 0 or node in frozen_pos) else 1
ucap[n, node] = 0 if ((L[n, node] >= 0) or (node in frozen_pos)) else 1
if node == N - 1:
done = 1
else:
Expand All @@ -31,21 +32,24 @@ def Polar_SC_decoder(N, frozen_pos, r):
node = lnode # go to left node
depth += 1
L[depth, ctemp * node : ctemp * (node + 1)] = (
(1 - 2 * (a > 0)) * (1 - 2 * (b > 0)) * np.max((np.abs(a), np.abs(b)))
# cannot use np.sign() because np.sign(0) = 0 !!
(1 - 2*(a < 0)) * (1 - 2*(b < 0)) * np.minimum(np.abs(a), np.abs(b)) # minsum-function
)
elif ns[npos] == 1:
ucapn = ucap[depth + 1, ctemp * lnode : ctemp * (lnode + 1)] # incoming decisions from left child
node = rnode # go to right node
depth += 1
L[depth, ctemp * node : ctemp * (node + 1)] = b + (1 - 2 * ucapn) * a
else:
L[depth, ctemp * node : ctemp * (node + 1)] = b + (1 - 2 * ucapn) * a # g-function
elif ns[npos] == 2:
ucapl = ucap[depth + 1, ctemp * lnode : ctemp * (lnode + 1)]
ucapr = ucap[depth + 1, ctemp * rnode : ctemp * (rnode + 1)]
ucap[depth, temp * node : temp * (node + 1)] = np.append(np.mod(ucapl + ucapr, 2), ucapr) # combine
node = node // 2 # go to parent node
depth -= 1
else:
assert False
ns[npos] += 1
return ucap.astype("int")[n, :]
return ucap.astype(int)[n, :]


# rec: Rate-recovered input, LLRs, length must be power of two
Expand All @@ -69,9 +73,50 @@ def nrPolarDecode(rec, K, E, L, padCRC=False, nmax=9, iil=True, CRClen=24):
if iil:
# deinterleave
p_IL = polar_precode_interleave(K)
decoded2 = np.empty(decoded.shape, "int")
decoded2 = np.empty(decoded.shape, int)
np.put(decoded2, p_IL, decoded)
else:
decoded2 = decoded

return decoded2

if __name__ == '__main__':
import py3gpp
import sionna
nmax = 9
K = 100

# --- TESTCASE1 ---
payload = np.ones(K).astype(int)
cw = py3gpp.nrPolarEncode(payload, 512, nmax=nmax, iil=False)
cw_desired = np.array([0,1,0,0,0,1,1,0,1,1,0,1,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,1,0,0,0,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,0,1,1,0,1,0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,0,1,0,1,1,0,0,0,0,0,0,1,1,0,1,0,1,0,0,0,0,1,0,1,1,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,1,0,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,1,1,0,0,0,0,1,0,1,1,0,1,0,1,1,0,1,1,1,1,1,1,0,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,0,1,1,0,1,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,1,0,0,1,1,0,0,0,0,0,0,1,1,0,1,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1,0,1,0,0,1,1,1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,1,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,1,1,1,1,1,1,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1])
assert np.array_equal(cw, cw_desired)

cw_llr = -2*cw.astype(np.float64) + 1
cw_llr[100:300] = 0 # introduce some errors

frozen_pos, info_pos = sionna.fec.polar.utils.generate_5g_ranking(K, 2**nmax)
decoder = sionna.fec.polar.decoding.PolarSCLDecoder(frozen_pos, 2**nmax, list_size=8, crc_degree='CRC24C', cpu_only=True, use_fast_scl=False)
payload_decoded_sionna = np.array(decoder(np.expand_dims(-cw_llr, 0))[0], 'int')
print(payload_decoded_sionna)
assert np.array_equal(payload, payload_decoded_sionna)

payload_decoded = py3gpp.nrPolarDecode(cw_llr, K, 512, 10, nmax = nmax, iil = False)
assert payload_decoded.shape[0] == K
print(payload_decoded)
assert np.array_equal(payload, payload_decoded)

# --- TESTCASE2 ---
payload = np.array([1,0,1,1,0,0,1,0,1,1,0,0,1,1,1,1,1,0,1,0,1,1,0,1,0,1,1,1,1,1,0,0,0,1,1,1,1,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1,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,0,1,1,1,0,1,1,1,1,0,0,1,0,1,0,1,0,1,1,1,1,1,0,0,0,0,1,1,1,1,0,1,0,1,0,0,0,0,0,0,0,1,1,1,0,1])
cw_llr = -2*cw.astype(np.float64) + 1
cw_llr[100:300] = 0 # introduce some errors

payload_decoded_sionna = np.array(decoder(np.expand_dims(-cw_llr, 0))[0], 'int')
print(payload_decoded_sionna)
assert np.array_equal(payload_decoded_sionna, payload)

payload_decoded = py3gpp.nrPolarDecode(cw_llr, K, 512, 10, nmax = nmax, iil = False)
assert payload_decoded.shape[0] == K
print(payload_decoded)
assert np.array_equal(payload_decoded, payload)
1 change: 1 addition & 0 deletions py3gpp/nrPolarEncode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

def nrPolarEncode(payload, E, nmax = 9 , iil = False):
K = payload.shape[0]
assert not (iil and K > 164), "iil is not supported for K > 164"
_, info_pos = generate_5g_ranking(k = K, n = 2 ** nmax)
N = 2 ** nmax
u = np.zeros(N).astype(int)
Expand Down
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.5"
version = "0.3.6"
authors = [
{name = "Benjamin Menküc", email = "benjamin@menkuec.de"},
]
Expand Down
31 changes: 31 additions & 0 deletions tests/test_nrPolarDecode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import numpy as np
import pytest
from py3gpp.nrPolarDecode import nrPolarDecode

def test_nrPolarDecode():
nmax = 9
K = 100

# --- TESTCASE1 ---
payload = np.ones(K).astype(int)
cw = np.array([0,1,0,0,0,1,1,0,1,1,0,1,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,1,1,1,0,1,1,1,0,1,1,0,1,1,1,1,1,1,0,0,0,1,1,0,1,0,1,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,0,1,1,0,1,0,0,1,1,0,0,1,1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,1,0,1,0,1,1,0,0,0,0,0,0,1,1,0,1,0,1,0,0,0,0,1,0,1,1,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,1,0,1,0,1,1,1,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1,0,1,0,0,1,1,0,0,0,0,1,0,1,1,0,1,0,1,1,0,1,1,1,1,1,1,0,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,0,1,1,0,1,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,1,0,1,0,0,1,1,0,0,0,0,0,0,1,1,0,1,0,0,0,0,1,1,1,1,1,1,0,0,0,0,1,0,0,0,1,1,1,1,0,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1,0,1,0,0,1,1,1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,1,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,1,1,1,1,1,1,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1])
cw_llr = -2*cw.astype(np.float64) + 1
cw_llr[100:300] = 0 # introduce some errors

payload_decoded = nrPolarDecode(cw_llr, K, 512, 10, nmax = nmax, iil = False)
assert payload_decoded.shape[0] == K
assert np.array_equal(payload, payload_decoded)

# --- T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,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,0,1,1,1,0,0,0,1,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,0,1,0,1,1,1,0,1,1,1,1,0,0,1,0,1,0,1,0,1,1,1,1,1,0,0,0,0,1,1,1,1,0,1,0,1,0,0,0,0,0,0,0,1,1,1,0,1])
cw_llr = -2*cw.astype(np.float64) + 1
cw_llr[100:300] = 0 # introduce some errors

payload_decoded = nrPolarDecode(cw_llr, K, 512, 10, nmax = nmax, iil = False)
assert payload_decoded.shape[0] == K
assert np.array_equal(payload_decoded, payload)


if __name__ == '__main__':
test_nrPolarDecode()
69 changes: 69 additions & 0 deletions tests/test_nrPolarEncode_nrPolarDecode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import numpy as np
import pytest
from py3gpp.nrPolarEncode import nrPolarEncode
from py3gpp.nrPolarDecode import nrPolarDecode

@pytest.mark.parametrize('K', [100, 151, 200])
@pytest.mark.parametrize('nmax', [9, 10])
@pytest.mark.parametrize('iil', [True, False])
def test_nrPolarEncode_nrPolarDecode_generic(K, nmax, iil, repetitions = 100):
for _ in np.arange(repetitions):
if iil and K > 164:
# print("skipping test, because iil is not supported for K > 164")
continue
payload = np.random.randint(2, size=K)
cw = nrPolarEncode(payload, 2**nmax, nmax=nmax, iil=iil)

cw_llr = -2*cw.astype(np.float64) + 1
if K < 150:
cw_llr[100:300] = 0 # introduce some errors
else:
cw_llr[100:200] = 0 # introduce some errors

payload_decoded = nrPolarDecode(cw_llr, K, 2**nmax, 10, nmax = nmax, iil = iil)
assert payload_decoded.shape[0] == K
assert np.array_equal(payload, payload_decoded)

def test_nrPolarEncode_nrPolarDecode_fixed():
nmax = 9
K = 100
payload = np.ones(K).astype(int)

# --- TESTCASE 1 ---
for iil in [True, False]:
cw = nrPolarEncode(payload, 512, nmax=nmax, iil=iil)
if iil:
cw_desired = np.array([0,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0,0,0,0,0,1])
assert np.array_equal(cw, cw_desired)

cw_llr = -2*cw.astype(np.float64) + 1
cw_llr[100:300] = 0 # introduce some errors

payload_decoded = nrPolarDecode(cw_llr, K, 512, 10, nmax = nmax, iil = iil)
assert payload_decoded.shape[0] == K
assert np.array_equal(payload, payload_decoded)

payload = np.ones(100).astype(int)
payload[:10] = 0

# --- TESTCASE 2 ---
for iil in [True, False]:
cw = nrPolarEncode(payload, 512, nmax=nmax, iil=iil)
if iil:
cw_desired = np.array([0,1,0,1,1,1,0,0,0,1,1,0,0,0,1,0,1,1,0,0,1,1,0,1,0,1,1,1,0,0,1,0,1,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,0,0,0,1,1,1,1,0,1,1,1,1,0,1,0,0,1,0,1,1,0,0,0,1,1,1,0,0,0,1,0,0,1,1,0,0,0,1,1,0,0,1,0,1,1,1,1,0,0,0,0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,0,0,1,1,0,0,1,0,0,0,1,1,0,0,0,0,1,1,1,1,1,0,1,0,0,0,0,1,0,0,0,1,1,1,1,0,1,0,1,1,0,0,1,0,0,0,1,0,0,0,0,1,1,1,1,1,0,0,1,0,0,0,0,0,1,1,0,1,0,0,1,0,0,0,1,0,1,1,0,1,1,1,1,0,1,0,1,0,0,0,1,1,1,1,1,0,0,0,1,1,0,1,1,0,0,0,1,1,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,1,1,0,1,0,1,1,1,0,0,1,1,1,1,0,1,1,1,0,0,0,1,1,0,0,0,1,1,1,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,1,0,1,1,0,1,0,0,0,0,0,1,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,0,0,1,1,1,1,0,0,0,1,0,0,0,1,1,0,0,0,1,1,1,0,1,0,0,0,1,0,0,0,0,0,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0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,1,1,0,1,0,0,0,0,1,1,1,1,1,1,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,1,1,0,1,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1])
assert np.array_equal(cw, cw_desired)

cw_llr = -2*cw.astype(np.float64) + 1
cw_llr[100:300] = 0 # introduce some errors

payload_decoded = nrPolarDecode(cw_llr, K, 512, 10, nmax = nmax, iil = iil)
assert payload_decoded.shape[0] == K
assert np.array_equal(payload, payload_decoded)

if __name__ == '__main__':
# test_nrPolarEncode_nrPolarDecode_fixed()
test_nrPolarEncode_nrPolarDecode_generic(K = 200, nmax = 9, iil = False, repetitions = 100)

0 comments on commit 4d65406

Please sign in to comment.