37 lines
No EOL
1.1 KiB
Python
37 lines
No EOL
1.1 KiB
Python
# performance_local.py
|
|
#!/usr/bin/env python3
|
|
import argparse
|
|
import numpy as np
|
|
import random
|
|
from encoder import make_codebook, encode_message, ALPHABET
|
|
from decoder import decode_blocks, count_errors
|
|
from channel import channel
|
|
|
|
|
|
def random_message(length):
|
|
return ''.join(random.choice(ALPHABET) for _ in range(length))
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Monte Carlo evaluation over local channel")
|
|
parser.add_argument("--num", type=int, required=True, help="Number of trials")
|
|
parser.add_argument("--r", type=int, default=5, help="Hadamard order (default 5)")
|
|
args = parser.parse_args()
|
|
|
|
num_trials = args.num
|
|
successes = 0
|
|
|
|
for _ in range(num_trials):
|
|
msg = random_message(40)
|
|
C = make_codebook(r=args.r, num_blocks=len(msg))
|
|
x = encode_message(msg, C)
|
|
Y = channel(x)
|
|
msg_hat = decode_blocks(Y, C)
|
|
if msg_hat == msg:
|
|
successes += 1
|
|
|
|
ratio = successes / num_trials
|
|
print(f"Correctly decoded messages: {successes}/{num_trials} ({ratio:.2%})")
|
|
|
|
if __name__ == "__main__":
|
|
main() |