Skip to content

Commit d67abac

Browse files
committed
Hamming code deduplicated routines into HammingCommon class
1 parent 3fd7c9e commit d67abac

File tree

11 files changed

+104
-286
lines changed

11 files changed

+104
-286
lines changed
Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import numpy
2-
from bitarray import bitarray
32

43
from okdmr.dmrlib.etsi.fec.fec_utils import (
54
derive_parity_check_matrix_from_generator,
6-
get_syndrome_for_word,
75
)
6+
from okdmr.dmrlib.etsi.fec.hamming_common import HammingCommon
87

98

10-
class Hamming1393:
9+
class Hamming1393(HammingCommon):
1110
"""
1211
ETSI TS 102 361-1 V2.5.1 (2017-10) - B.3.4 Hamming (13,9,3), Hamming (15,11,3), and Hamming (16,11,4)
1312
"""
1413

15-
GENERATOR_MATRIX: numpy.ndarray = numpy.array(
14+
GENERATOR_MATRIX = numpy.array(
1615
[
1716
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
1817
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0],
@@ -26,57 +25,10 @@ class Hamming1393:
2625
]
2726
)
2827

29-
PARITY_CHECK_MATRIX: numpy.ndarray = derive_parity_check_matrix_from_generator(
30-
GENERATOR_MATRIX
31-
)
32-
33-
CORRECT_SYNDROME: numpy.ndarray = numpy.array([0, 0, 0, 0])
34-
35-
@staticmethod
36-
def check(bits: bitarray) -> bool:
37-
"""
38-
Verifies Hamming FEC in 4 end bits of input bitarray
39-
:param bits:
40-
:return: check result
41-
"""
42-
assert (
43-
len(bits) == 13
44-
), f"Hamming (13,9,3) expects exactly 13 bits, got {len(bits)}"
45-
return numpy.array_equal(
46-
get_syndrome_for_word(
47-
numpy.array(bits.tolist()), Hamming1393.PARITY_CHECK_MATRIX
48-
),
49-
Hamming1393.CORRECT_SYNDROME,
50-
)
51-
52-
@staticmethod
53-
def check_and_correct(bits: bitarray) -> (bool, bitarray):
54-
"""
55-
Will check if parity matches, if not, tries to correct one bit-error
56-
:param bits:
57-
:return: (parity matches, corrected message)
58-
"""
59-
check = Hamming1393.check(bits)
60-
61-
if not check:
62-
# if check does not pass, find index of flipped bit, and correct it
63-
syndrome = get_syndrome_for_word(
64-
numpy.array(bits.tolist()), Hamming1393.PARITY_CHECK_MATRIX
65-
).tolist()
66-
bits.invert(Hamming1393.PARITY_CHECK_MATRIX.T.tolist().index(syndrome))
28+
PARITY_CHECK_MATRIX = derive_parity_check_matrix_from_generator(GENERATOR_MATRIX)
6729

68-
return check, bits
30+
CORRECT_SYNDROME = numpy.array([0, 0, 0, 0])
6931

70-
@staticmethod
71-
def generate(bits: bitarray) -> numpy.ndarray:
72-
"""
73-
Returns 9 bits (input) with 4 bits of Hamming FEC
74-
:param bits:
75-
:return: 13 bits (9 data bits + 4 FEC bits)
76-
"""
77-
assert (
78-
len(bits) == 9
79-
), f"Hamming (13,9,3) expects 9 bits of data to add 4 bits of parity, got {len(bits)}"
80-
return divmod(
81-
numpy.dot(Hamming1393.GENERATOR_MATRIX.T, numpy.array(bits.tolist())), 2
82-
)[1]
32+
CODEWORD_LENGTH = 13
33+
CODE_DIMENSION = 9
34+
MINIMUM_HAMMING_DISTANCE = 3
Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import numpy
2-
from bitarray import bitarray
32

43
from okdmr.dmrlib.etsi.fec.fec_utils import (
54
derive_parity_check_matrix_from_generator,
6-
get_syndrome_for_word,
75
)
6+
from okdmr.dmrlib.etsi.fec.hamming_common import HammingCommon
87

98

10-
class Hamming15113:
9+
class Hamming15113(HammingCommon):
1110
"""
1211
ETSI TS 102 361-1 V2.5.1 (2017-10) - B.3.4 Hamming (13,9,3), Hamming (15,11,3), and Hamming (16,11,4)
1312
"""
1413

15-
GENERATOR_MATRIX: numpy.ndarray = numpy.array(
14+
GENERATOR_MATRIX = numpy.array(
1615
[
1716
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1],
1817
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
@@ -28,57 +27,10 @@ class Hamming15113:
2827
]
2928
)
3029

31-
PARITY_CHECK_MATRIX: numpy.ndarray = derive_parity_check_matrix_from_generator(
32-
GENERATOR_MATRIX
33-
)
34-
35-
CORRECT_SYNDROME: numpy.ndarray = numpy.array([0, 0, 0, 0])
36-
37-
@staticmethod
38-
def check(bits: bitarray) -> bool:
39-
"""
40-
Verifies Hamming FEC in 4 end bits of input bitarray
41-
:param bits:
42-
:return: check result
43-
"""
44-
assert (
45-
len(bits) == 15
46-
), f"Hamming (15,11,3) expects exactly 15 bits, got {len(bits)}"
47-
return numpy.array_equal(
48-
get_syndrome_for_word(
49-
numpy.array(bits.tolist()), Hamming15113.PARITY_CHECK_MATRIX
50-
),
51-
Hamming15113.CORRECT_SYNDROME,
52-
)
53-
54-
@staticmethod
55-
def check_and_correct(bits: bitarray) -> (bool, bitarray):
56-
"""
57-
Will check if parity matches, if not, tries to correct one bit-error
58-
:param bits:
59-
:return: (parity matches, corrected message)
60-
"""
61-
check = Hamming15113.check(bits)
62-
63-
if not check:
64-
# if check does not pass, find index of flipped bit, and correct it
65-
syndrome = get_syndrome_for_word(
66-
numpy.array(bits.tolist()), Hamming15113.PARITY_CHECK_MATRIX
67-
).tolist()
68-
bits.invert(Hamming15113.PARITY_CHECK_MATRIX.T.tolist().index(syndrome))
30+
PARITY_CHECK_MATRIX = derive_parity_check_matrix_from_generator(GENERATOR_MATRIX)
6931

70-
return check, bits
32+
CORRECT_SYNDROME = numpy.array([0, 0, 0, 0])
7133

72-
@staticmethod
73-
def generate(bits: bitarray) -> numpy.ndarray:
74-
"""
75-
Returns 11 bits (input) with 4 bits of Hamming FEC
76-
:param bits:
77-
:return: 15 bits (11 data bits + 4 FEC bits)
78-
"""
79-
assert (
80-
len(bits) == 11
81-
), f"Hamming (15,11,3) expects 11 bits of data to add 4 bits of parity, got {len(bits)}"
82-
return divmod(
83-
numpy.dot(Hamming15113.GENERATOR_MATRIX.T, numpy.array(bits.tolist())), 2
84-
)[1]
34+
CODEWORD_LENGTH = 15
35+
CODE_DIMENSION = 11
36+
MINIMUM_HAMMING_DISTANCE = 3
Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import numpy
2-
from bitarray import bitarray
32

43
from okdmr.dmrlib.etsi.fec.fec_utils import (
54
derive_parity_check_matrix_from_generator,
6-
get_syndrome_for_word,
75
)
6+
from okdmr.dmrlib.etsi.fec.hamming_common import HammingCommon
87

98

10-
class Hamming16114:
9+
class Hamming16114(HammingCommon):
1110
"""
1211
ETSI TS 102 361-1 V2.5.1 (2017-10) - B.3.4 Hamming (13,9,3), Hamming (15,11,3), and Hamming (16,11,4)
1312
"""
1413

15-
GENERATOR_MATRIX: numpy.ndarray = numpy.array(
14+
GENERATOR_MATRIX = numpy.array(
1615
[
1716
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1],
1817
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0],
@@ -28,57 +27,10 @@ class Hamming16114:
2827
]
2928
)
3029

31-
PARITY_CHECK_MATRIX: numpy.ndarray = derive_parity_check_matrix_from_generator(
32-
GENERATOR_MATRIX
33-
)
34-
35-
CORRECT_SYNDROME: numpy.ndarray = numpy.array([0, 0, 0, 0, 0])
36-
37-
@staticmethod
38-
def check(bits: bitarray) -> bool:
39-
"""
40-
Verifies Hamming FEC in 5 end bits of input bitarray
41-
:param bits:
42-
:return: check result
43-
"""
44-
assert (
45-
len(bits) == 16
46-
), f"Hamming (15,11,3) expects exactly 16 bits, got {len(bits)}"
47-
return numpy.array_equal(
48-
get_syndrome_for_word(
49-
numpy.array(bits.tolist()), Hamming16114.PARITY_CHECK_MATRIX
50-
),
51-
Hamming16114.CORRECT_SYNDROME,
52-
)
53-
54-
@staticmethod
55-
def check_and_correct(bits: bitarray) -> (bool, bitarray):
56-
"""
57-
Will check if parity matches, if not, tries to correct one bit-error
58-
:param bits:
59-
:return: (parity matches, corrected message)
60-
"""
61-
check = Hamming16114.check(bits)
62-
63-
if not check:
64-
# if check does not pass, find index of flipped bit, and correct it
65-
syndrome = get_syndrome_for_word(
66-
numpy.array(bits.tolist()), Hamming16114.PARITY_CHECK_MATRIX
67-
).tolist()
68-
bits.invert(Hamming16114.PARITY_CHECK_MATRIX.T.tolist().index(syndrome))
30+
PARITY_CHECK_MATRIX = derive_parity_check_matrix_from_generator(GENERATOR_MATRIX)
6931

70-
return check, bits
32+
CORRECT_SYNDROME = numpy.array([0, 0, 0, 0, 0])
7133

72-
@staticmethod
73-
def generate(bits: bitarray) -> numpy.ndarray:
74-
"""
75-
Returns 11 bits (input) with 5 bits of Hamming FEC
76-
:param bits:
77-
:return: 16 bits (11 data bits + 5 FEC bits)
78-
"""
79-
assert (
80-
len(bits) == 11
81-
), f"Hamming (15,11,3) expects 9 bits of data to add 5 bits of parity, got {len(bits)}"
82-
return divmod(
83-
numpy.dot(Hamming16114.GENERATOR_MATRIX.T, numpy.array(bits.tolist())), 2
84-
)[1]
34+
CODEWORD_LENGTH = 16
35+
CODE_DIMENSION = 11
36+
MINIMUM_HAMMING_DISTANCE = 4
Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import numpy
2-
from bitarray import bitarray
32

43
from okdmr.dmrlib.etsi.fec.fec_utils import (
54
derive_parity_check_matrix_from_generator,
6-
get_syndrome_for_word,
75
)
6+
from okdmr.dmrlib.etsi.fec.hamming_common import HammingCommon
87

98

10-
class Hamming17123:
9+
class Hamming17123(HammingCommon):
1110
"""
1211
ETSI TS 102 361-1 V2.5.1 (2017-10) - B.3.3 Hamming (17,12,3)
1312
"""
1413

15-
GENERATOR_MATRIX: numpy.ndarray = numpy.array(
14+
GENERATOR_MATRIX = numpy.array(
1615
[
1716
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1],
1817
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
@@ -29,57 +28,10 @@ class Hamming17123:
2928
]
3029
)
3130

32-
PARITY_CHECK_MATRIX: numpy.ndarray = derive_parity_check_matrix_from_generator(
33-
GENERATOR_MATRIX
34-
)
35-
36-
CORRECT_SYNDROME: numpy.ndarray = numpy.array([0, 0, 0, 0, 0])
37-
38-
@staticmethod
39-
def check(bits: bitarray) -> bool:
40-
"""
41-
Verifies Hamming FEC in 5 end bits of input bitarray
42-
:param bits:
43-
:return: check result
44-
"""
45-
assert (
46-
len(bits) == 17
47-
), f"Hamming (17,12,3) expects exactly 17 bits, got {len(bits)}"
48-
return numpy.array_equal(
49-
get_syndrome_for_word(
50-
numpy.array(bits.tolist()), Hamming17123.PARITY_CHECK_MATRIX
51-
),
52-
Hamming17123.CORRECT_SYNDROME,
53-
)
54-
55-
@staticmethod
56-
def check_and_correct(bits: bitarray) -> (bool, bitarray):
57-
"""
58-
Will check if parity matches, if not, tries to correct one bit-error
59-
:param bits:
60-
:return: (parity matches, corrected message)
61-
"""
62-
check = Hamming17123.check(bits)
63-
64-
if not check:
65-
# if check does not pass, find index of flipped bit, and correct it
66-
syndrome = get_syndrome_for_word(
67-
numpy.array(bits.tolist()), Hamming17123.PARITY_CHECK_MATRIX
68-
).tolist()
69-
bits.invert(Hamming17123.PARITY_CHECK_MATRIX.T.tolist().index(syndrome))
31+
PARITY_CHECK_MATRIX = derive_parity_check_matrix_from_generator(GENERATOR_MATRIX)
7032

71-
return check, bits
33+
CORRECT_SYNDROME = numpy.array([0, 0, 0, 0, 0])
7234

73-
@staticmethod
74-
def generate(bits: bitarray) -> numpy.ndarray:
75-
"""
76-
Returns 11 bits (input) with 5 bits of Hamming FEC
77-
:param bits:
78-
:return: 16 bits (11 data bits + 5 FEC bits)
79-
"""
80-
assert (
81-
len(bits) == 12
82-
), f"Hamming (17,12,3) expects 12 bits of data to add 5 bits of parity, got {len(bits)}"
83-
return divmod(
84-
numpy.dot(Hamming17123.GENERATOR_MATRIX.T, numpy.array(bits.tolist())), 2
85-
)[1]
35+
CODEWORD_LENGTH = 17
36+
CODE_DIMENSION = 12
37+
MINIMUM_HAMMING_DISTANCE = 3

0 commit comments

Comments
 (0)