Skip to content

Commit 64471ec

Browse files
author
grigory
committed
Reorganize code of RC SCAN Polar codec
1 parent 89d26a5 commit 64471ec

File tree

13 files changed

+171
-137
lines changed

13 files changed

+171
-137
lines changed

python_polar_coding/polar_codes/rc_scan.py

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .codec import RCSCANPolarCodec
2+
from .decoder import RCSCANDecoder
3+
from .functions import *
4+
from .node import INFINITY, RCSCANNode
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from typing import Union
2+
3+
from python_polar_coding.polar_codes.base import BasePolarCodec
4+
5+
from .decoder import RCSCANDecoder
6+
7+
8+
class RCSCANPolarCodec(BasePolarCodec):
9+
"""Polar code with RC-SCAN decoding algorithm.
10+
11+
Based on: https://arxiv.org/pdf/1510.06495.pdf,
12+
DOI: 10.1109/DASIP.2015.7367252
13+
14+
"""
15+
decoder_class = RCSCANDecoder
16+
17+
def __init__(
18+
self,
19+
N: int,
20+
K: int,
21+
design_snr: float = 0.0,
22+
mask: Union[str, None] = None,
23+
pcc_method: str = BasePolarCodec.BHATTACHARYYA,
24+
I: int = 1,
25+
* args, **kwargs,
26+
):
27+
self.I = I
28+
super().__init__(N=N, K=K,
29+
is_systematic=True,
30+
design_snr=design_snr,
31+
mask=mask,
32+
pcc_method=pcc_method)
33+
34+
def init_decoder(self):
35+
return self.decoder_class(n=self.n, mask=self.mask, I=self.I)
36+
37+
def to_dict(self):
38+
d = super().to_dict()
39+
d.update({'I': self.I})
40+
return d

python_polar_coding/polar_codes/decoders/rc_scan_decoder.py renamed to python_polar_coding/polar_codes/rc_scan/decoder.py

Lines changed: 12 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,11 @@
1-
import numba
21
import numpy as np
32
from anytree import PreOrderIter
43

5-
from ..base.functions import function_1, function_2, make_hard_decision
6-
from .fast_ssc_decoder import FastSSCDecoder, FastSSCNode
4+
from python_polar_coding.polar_codes.fast_ssc import FastSSCDecoder
75

8-
# LLR = 1000 is high enough to be considered as +∞ for RC-SCAN decoding
9-
INFINITY = 1000
10-
11-
12-
class RCSCANNode(FastSSCNode):
13-
14-
def compute_leaf_beta(self):
15-
"""Do nothing.
16-
17-
Unlike SC-based decoders SCAN decoders does not make decisions
18-
in leaves.
19-
20-
"""
21-
22-
def initialize_leaf_beta(self):
23-
"""Initialize BETA values on tree building.
24-
25-
Initialize Leaves following to Section III doi:10.1109/jsac.2014.140515
26-
27-
"""
28-
if not self.is_leaf:
29-
return
30-
31-
if self._node_type == RCSCANNode.ZERO_NODE:
32-
self._beta = self._compute_zero_node_beta(self.alpha)
33-
if self._node_type == RCSCANNode.ONE_NODE:
34-
self._beta = self._compute_one_node_beta(self.alpha)
35-
36-
def get_node_type(self):
37-
"""Get the type of RC SCAN Node.
38-
39-
* Zero node - [0, 0, 0, 0, 0, 0, 0, 0];
40-
* One node - [1, 1, 1, 1, 1, 1, 1, 1];
41-
42-
Or other type.
43-
44-
"""
45-
if np.all(self._mask == 0):
46-
return RCSCANNode.ZERO_NODE
47-
if np.all(self._mask == 1):
48-
return RCSCANNode.ONE_NODE
49-
return RCSCANNode.OTHER
50-
51-
@staticmethod
52-
@numba.njit
53-
def _compute_zero_node_beta(llr):
54-
"""Compute beta values for ZERO node.
55-
56-
https://arxiv.org/pdf/1510.06495.pdf Section III.C.
57-
58-
"""
59-
return np.ones(llr.size, dtype=np.double) * INFINITY
60-
61-
@staticmethod
62-
@numba.njit
63-
def _compute_one_node_beta(llr):
64-
"""Compute beta values for ONE node.
65-
66-
https://arxiv.org/pdf/1510.06495.pdf Section III.C.
67-
68-
"""
69-
return np.zeros(llr.size, dtype=np.double)
6+
from ..base import make_hard_decision
7+
from .functions import function_1, function_2
8+
from .node import RCSCANNode
709

7110

7211
class RCSCANDecoder(FastSSCDecoder):
@@ -79,12 +18,14 @@ class RCSCANDecoder(FastSSCDecoder):
7918
"""
8019
node_class = RCSCANNode
8120

82-
def __init__(self, n: int,
83-
mask: np.array,
84-
is_systematic: bool = True,
85-
code_min_size: int = 0,
86-
I: int = 1):
87-
super().__init__(n=n, mask=mask, is_systematic=is_systematic,
21+
def __init__(
22+
self,
23+
n: int,
24+
mask: np.array,
25+
code_min_size: int = 0,
26+
I: int = 1,
27+
):
28+
super().__init__(n=n, mask=mask, is_systematic=True,
8829
code_min_size=code_min_size)
8930
self.I = I
9031

@@ -105,8 +46,6 @@ def clean_before_decoding(self):
10546
10647
Run this before calling `__call__` method.
10748
108-
TODO: Add JIT
109-
11049
"""
11150
for node in PreOrderIter(self._decoding_tree):
11251
if not node.is_leaf:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import numba
2+
3+
from ..base import compute_alpha
4+
5+
6+
@numba.njit
7+
def function_1(a, b, c):
8+
"""Function 1.
9+
10+
Source: doi:10.1007/s12243-018-0634-7, formula 1.
11+
12+
"""
13+
return compute_alpha(a, b + c)
14+
15+
16+
@numba.njit
17+
def function_2(a, b, c):
18+
"""Function 2.
19+
20+
Source: doi:10.1007/s12243-018-0634-7, formula 2.
21+
22+
"""
23+
return compute_alpha(a, b) + c
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import numba
2+
import numpy as np
3+
4+
from python_polar_coding.polar_codes.fast_ssc import FastSSCNode
5+
6+
# LLR = 1000 is high enough to be considered as +∞ for RC-SCAN decoding
7+
INFINITY = 1000
8+
9+
10+
class RCSCANNode(FastSSCNode):
11+
12+
def compute_leaf_beta(self):
13+
"""Do nothing.
14+
15+
Unlike SC-based decoders SCAN decoders does not make decisions
16+
in leaves.
17+
18+
"""
19+
20+
def initialize_leaf_beta(self):
21+
"""Initialize BETA values on tree building.
22+
23+
Initialize Leaves following to Section III doi:10.1109/jsac.2014.140515
24+
25+
"""
26+
if not self.is_leaf:
27+
return
28+
29+
if self._node_type == RCSCANNode.ZERO_NODE:
30+
self._beta = self._compute_zero_node_beta(self.alpha)
31+
if self._node_type == RCSCANNode.ONE_NODE:
32+
self._beta = self._compute_one_node_beta(self.alpha)
33+
34+
def get_node_type(self):
35+
"""Get the type of RC SCAN Node.
36+
37+
* Zero node - [0, 0, 0, 0, 0, 0, 0, 0];
38+
* One node - [1, 1, 1, 1, 1, 1, 1, 1];
39+
40+
Or other type.
41+
42+
"""
43+
if np.all(self._mask == 0):
44+
return RCSCANNode.ZERO_NODE
45+
if np.all(self._mask == 1):
46+
return RCSCANNode.ONE_NODE
47+
return RCSCANNode.OTHER
48+
49+
@staticmethod
50+
@numba.njit
51+
def _compute_zero_node_beta(llr):
52+
"""Compute beta values for ZERO node.
53+
54+
https://arxiv.org/pdf/1510.06495.pdf Section III.C.
55+
56+
"""
57+
return np.ones(llr.size, dtype=np.double) * INFINITY
58+
59+
@staticmethod
60+
@numba.njit
61+
def _compute_one_node_beta(llr):
62+
"""Compute beta values for ONE node.
63+
64+
https://arxiv.org/pdf/1510.06495.pdf Section III.C.
65+
66+
"""
67+
return np.zeros(llr.size, dtype=np.double)
File renamed without changes.

python_polar_coding/tests/test_rc_scan/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)