pdc_project/test_local.py
2025-05-25 16:35:41 +02:00

36 lines
1.1 KiB
Python

# test_local.py
#!/usr/bin/env python3
import argparse
import numpy as np
from encoder import make_codebook, encode_message
from decoder import decode_blocks, count_errors
from channel import channel
def main():
parser = argparse.ArgumentParser(description="Local test using channel.py")
parser.add_argument("--message", required=True, help="40-character message")
args = parser.parse_args()
msg = args.message
if len(msg) != 40:
raise ValueError("Message must be exactly 40 characters.")
C = make_codebook(r=5, num_blocks=len(msg))
x = encode_message(msg, C)
print(f"→ Total samples = {x.size}, total energy = {np.sum(x*x):.1f}")
Y = channel(x)
msg_hat = decode_blocks(Y, C)
print(f"↓ Decoded message: {msg_hat}")
errors = count_errors(msg, msg_hat)
print(f"Errors: {len(errors)} / {len(msg)} characters differ")
if errors:
for i, o, e in errors:
print(f" Pos {i}: sent '{o}' but got '{e}'")
else:
print("✔️ No decoding errors!")
if __name__ == "__main__":
main()