feat(framing): framing done

refactor(framing): re-name hamming codec to 7,4 to reflect algorithm used
This commit is contained in:
2025-11-30 13:44:41 +01:00
parent 0489d8fc31
commit 5bf5bfec5d
4 changed files with 77 additions and 75 deletions

View File

@@ -19,7 +19,7 @@ DECODER_MATRIX = np.asarray([(0, 0, 1, 0, 0, 0, 0,),
def number_to_list(number: int, N: int) -> npt.NDArray:
"""Return the last N bits of a number as an MSB-first array"""
return np.array([(number >> (N-1-i)) % 2 for i in range(0, N)])
return np.array([(number >> (N-1-i)) % 2 for i in range(0, N)],dtype=int)
def list_to_number(array: npt.NDArray, N: int) -> int:
@@ -54,7 +54,8 @@ def decode_nibble(data: int) -> int:
syndrome = (PARITY_MATRIX @ data_asarr) % 2
error = list_to_number(syndrome, 3)
if error == 0:
return list_to_number(data_asarr, 4)
data_decoded = (DECODER_MATRIX @ data_asarr) % 2
return list_to_number(data_decoded, 4)
data_asarr[error - 1] ^= 1
data_decoded = (DECODER_MATRIX @ data_asarr) % 2
return list_to_number(data_decoded, 4)
@@ -86,12 +87,10 @@ if __name__ == "__main__":
data_corrected_2bit = hamming_7_4_decode(data_corrupted_2bit)
data_corrected_2bit_str = [int.to_bytes(int(i), 1, "big").decode(
encoding="utf-8", errors="ignore") for i in data_corrected_2bit]
print(f"Recovered data from 1-bit errors: {data_corrected_2bit_str}")
print(f"Recovered data from 2-bit errors: {data_corrected_2bit_str}")
data_corrupted_3bit = [data ^ (7 << (idx % 7))
for idx, data in enumerate(data_encoded)]
# print(f"Corrupt data with 3-bit errors: {data_corrupted_3bit}")
data_corrected_3bit = hamming_7_4_decode(data_corrupted_3bit)
data_corrected_3bit_str = [int.to_bytes(int(i), 1, "big").decode(
encoding="utf-8", errors="ignore") for i in data_corrected_3bit]
print(f"Recovered data from 1-bit errors: {data_corrected_3bit_str}")
data_corrected_noerr = hamming_7_4_decode(data_encoded)
data_corrected_noerr_str = [int.to_bytes(int(i), 1, "big").decode(
encoding="utf-8", errors="ignore") for i in data_corrected_noerr]
print(f"Recovered data from no errors: {data_corrected_noerr_str}")