|
| 1 | +"""Common functions for polar coding.""" |
| 2 | +import numba |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from .node_types import NodeTypes |
| 6 | + |
| 7 | +# ----------------------------------------------------------------------------- |
| 8 | +# Making hard decisions during the decoding |
| 9 | +# ----------------------------------------------------------------------------- |
| 10 | + |
| 11 | + |
| 12 | +@numba.njit |
| 13 | +def zero( |
| 14 | + llr: np.array, |
| 15 | + mask_steps: int = 0, |
| 16 | + last_chunk_type: int = 0, |
| 17 | +) -> np.array: |
| 18 | + """Makes hard decision based on soft input values (LLR).""" |
| 19 | + return np.zeros(llr.size, dtype=np.int8) |
| 20 | + |
| 21 | + |
| 22 | +@numba.njit |
| 23 | +def make_hard_decision( |
| 24 | + llr: np.array, |
| 25 | + mask_steps: int = 0, |
| 26 | + last_chunk_type: int = 0, |
| 27 | +) -> np.array: |
| 28 | + """Makes hard decision based on soft input values (LLR).""" |
| 29 | + return np.array([s < 0 for s in llr], dtype=np.int8) |
| 30 | + |
| 31 | + |
| 32 | +@numba.njit |
| 33 | +def single_parity_check( |
| 34 | + llr: np.array, |
| 35 | + mask_steps: int = 0, |
| 36 | + last_chunk_type: int = 0, |
| 37 | +) -> np.array: |
| 38 | + """Compute bits for Single Parity Check node. |
| 39 | +
|
| 40 | + Based on: https://arxiv.org/pdf/1307.7154.pdf, Section IV, A. |
| 41 | +
|
| 42 | + """ |
| 43 | + bits = make_hard_decision(llr) |
| 44 | + parity = np.sum(bits) % 2 |
| 45 | + arg_min = np.abs(llr).argmin() |
| 46 | + bits[arg_min] = (bits[arg_min] + parity) % 2 |
| 47 | + return bits |
| 48 | + |
| 49 | + |
| 50 | +@numba.njit |
| 51 | +def repetition( |
| 52 | + llr: np.array, |
| 53 | + mask_steps: int = 0, |
| 54 | + last_chunk_type: int = 0, |
| 55 | +) -> np.array: |
| 56 | + """Compute bits for Repetition node. |
| 57 | +
|
| 58 | + Based on: https://arxiv.org/pdf/1307.7154.pdf, Section IV, B. |
| 59 | +
|
| 60 | + """ |
| 61 | + return ( |
| 62 | + np.zeros(llr.size, dtype=np.int8) if np.sum(llr) >= 0 |
| 63 | + else np.ones(llr.size, dtype=np.int8) |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +@numba.njit |
| 68 | +def g_repetition( |
| 69 | + llr: np.array, |
| 70 | + mask_steps: int, |
| 71 | + last_chunk_type: int, |
| 72 | +) -> np.array: |
| 73 | + """Compute bits for Generalized Repetition node. |
| 74 | +
|
| 75 | + Based on: https://arxiv.org/pdf/1804.09508.pdf, Section III, A. |
| 76 | +
|
| 77 | + """ |
| 78 | + N = llr.size |
| 79 | + step = N // mask_steps # step is equal to a chunk size |
| 80 | + |
| 81 | + last_alpha = np.zeros(step) |
| 82 | + for i in range(step): |
| 83 | + last_alpha[i] = np.sum(np.array([ |
| 84 | + llr[i + j * step] for j in range(mask_steps) |
| 85 | + ])) |
| 86 | + |
| 87 | + last_beta = ( |
| 88 | + make_hard_decision(last_alpha) if last_chunk_type == 1 |
| 89 | + else single_parity_check(last_alpha) |
| 90 | + ) |
| 91 | + |
| 92 | + result = np.zeros(N) |
| 93 | + for i in range(0, N, step): |
| 94 | + result[i: i + step] = last_beta |
| 95 | + |
| 96 | + return result |
| 97 | + |
| 98 | + |
| 99 | +@numba.njit |
| 100 | +def rg_parity( |
| 101 | + llr: np.array, |
| 102 | + mask_steps: int, |
| 103 | + last_chunk_type: int = 0, |
| 104 | +) -> np.array: |
| 105 | + """Compute bits for Relaxed Generalized Parity Check node. |
| 106 | +
|
| 107 | + Based on: https://arxiv.org/pdf/1804.09508.pdf, Section III, B. |
| 108 | +
|
| 109 | + """ |
| 110 | + N = llr.size |
| 111 | + step = N // mask_steps # step is equal to a chunk size |
| 112 | + result = np.zeros(N) |
| 113 | + |
| 114 | + for i in range(step): |
| 115 | + alpha = np.zeros(mask_steps) |
| 116 | + for j in range(mask_steps): |
| 117 | + alpha[j] = llr[i + j * step] |
| 118 | + |
| 119 | + beta = single_parity_check(alpha) |
| 120 | + result[i:N:step] = beta |
| 121 | + |
| 122 | + return result |
| 123 | + |
| 124 | + |
| 125 | +# Mapping between decoding node types and corresponding decoding methods |
| 126 | +_methods_map = { |
| 127 | + NodeTypes.ZERO: zero, |
| 128 | + NodeTypes.ONE: make_hard_decision, |
| 129 | + NodeTypes.SINGLE_PARITY_CHECK: single_parity_check, |
| 130 | + NodeTypes.REPETITION: repetition, |
| 131 | + NodeTypes.RG_PARITY: rg_parity, |
| 132 | + NodeTypes.G_REPETITION: g_repetition, |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +def compute_beta_hard( |
| 137 | + node_type: str, |
| 138 | + llr: np.array, |
| 139 | + mask_steps: int = 0, |
| 140 | + last_chunk_type: int = 0, |
| 141 | + *args, **kwargs, |
| 142 | +) -> np.array: |
| 143 | + """Unites functions for making hard decisions during decoding.""" |
| 144 | + method = _methods_map[node_type] |
| 145 | + return method(llr, mask_steps, last_chunk_type, *args, **kwargs) |
| 146 | + |
| 147 | + |
| 148 | +@numba.njit |
| 149 | +def compute_parent_beta_hard(left: np.array, right: np.array) -> np.array: |
| 150 | + """Compute Beta values for parent Node.""" |
| 151 | + N = left.size |
| 152 | + result = np.zeros(N * 2, dtype=np.int8) |
| 153 | + result[:N] = (left + right) % 2 |
| 154 | + result[N:] = right |
| 155 | + |
| 156 | + return result |
0 commit comments