14 lines
No EOL
588 B
Python
14 lines
No EOL
588 B
Python
# encoder.py (unchanged except default r)
|
|
import numpy as np
|
|
from codebook import construct_codebook
|
|
from utils import char_to_index, normalize_energy
|
|
|
|
def text_to_signal(text, r=5, Eb=3):
|
|
assert len(text) == 40, "Message must be exactly 40 characters."
|
|
codebook, n, m, alpha = construct_codebook(r, Eb)
|
|
# Map each character to its codeword
|
|
msg_indices = [char_to_index[c] for c in text]
|
|
signal = np.concatenate([codebook[i] for i in msg_indices])
|
|
# Enforce the energy constraint
|
|
signal = normalize_energy(signal, energy_limit=2000)
|
|
return signal, codebook |