42 lines
No EOL
1.3 KiB
Python
42 lines
No EOL
1.3 KiB
Python
import numpy as np
|
|
|
|
def bits_to_text(bits, alphabet):
|
|
char_to_idx = {char: idx for idx, char in enumerate(alphabet)}
|
|
idx_to_char = {idx: char for char, idx in char_to_idx.items()}
|
|
text = ''
|
|
for i in range(0, len(bits), 6):
|
|
idx = int(''.join(map(str, bits[i:i + 6])), 2)
|
|
text += idx_to_char.get(idx, '?')
|
|
return text
|
|
|
|
def receiver(Y):
|
|
G = 10 # Match channel.py
|
|
sigma2 = 10
|
|
alphabet = (
|
|
'abcdefghijklmnopqrstuvwxyz'
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
'0123456789 .'
|
|
)
|
|
if Y.size != 480:
|
|
raise ValueError("Received signal must have length 480.")
|
|
n = 480
|
|
bits = np.zeros(240, dtype=int)
|
|
E = 4
|
|
|
|
# Estimate channel state
|
|
even_energy = np.mean(np.abs(Y[::2]))
|
|
odd_energy = np.mean(np.abs(Y[1::2]))
|
|
# If even indices have higher energy, likely s=1; else s=2
|
|
s_hat = 1 if even_energy > odd_energy else 2
|
|
|
|
for i in range(240):
|
|
y_even = Y[2 * i]
|
|
y_odd = Y[2 * i + 1]
|
|
if s_hat == 1:
|
|
llr = (y_even * np.sqrt(E) * np.sqrt(G) + y_odd * np.sqrt(E)) / sigma2
|
|
else:
|
|
llr = (y_even * np.sqrt(E) + y_odd * np.sqrt(E) * np.sqrt(G)) / sigma2
|
|
bits[i] = 1 if llr < 0 else 0
|
|
|
|
text = bits_to_text(bits, alphabet)
|
|
return text |