32 lines
No EOL
837 B
Python
32 lines
No EOL
837 B
Python
# decoder.py
|
|
import numpy as np
|
|
from encoder import ALPHABET, G
|
|
|
|
|
|
def decode_blocks(Y: np.ndarray, C: np.ndarray) -> str:
|
|
"""
|
|
Decode received samples by maximum correlation score
|
|
"""
|
|
n = C.shape[1]
|
|
half = n // 2
|
|
num = Y.size // n
|
|
C1 = C[:, :half]
|
|
C2 = C[:, half:]
|
|
sqrtG = np.sqrt(G)
|
|
recovered = []
|
|
for k in range(num):
|
|
Yb = Y[k*n:(k+1)*n]
|
|
Ye, Yo = Yb[0::2], Yb[1::2]
|
|
s1 = sqrtG * (Ye @ C1.T) + (Yo @ C2.T)
|
|
s2 = (Ye @ C1.T) + sqrtG * (Yo @ C2.T)
|
|
score = np.maximum(s1, s2)
|
|
best = int(np.argmax(score))
|
|
recovered.append(ALPHABET[best])
|
|
return "".join(recovered)
|
|
|
|
|
|
def count_errors(orig: str, est: str):
|
|
"""
|
|
List mismatches between orig and est
|
|
"""
|
|
return [(i, o, e) for i, (o, e) in enumerate(zip(orig, est)) if o != e] |