chore: add verbose decoded messages

This commit is contained in:
appellet 2025-05-27 11:38:28 +02:00
parent 0262a27f2c
commit 177bc566cf
2 changed files with 22 additions and 18 deletions

View file

@ -35,7 +35,7 @@ All commands assume you are in the project root directory.
```bash
python3 main.py \
--message "This is a 40-character test message...." \
--message "Lorem ipsum dolor sit amet. consectetuer" \
--trials 1 \
--mode local
```
@ -44,7 +44,7 @@ python3 main.py \
```bash
python3 main.py \
--message "This is a 40-character test message...." \
--message "Lorem ipsum dolor sit amet. consectetuer" \
--trials 500 \
--mode local
```
@ -55,7 +55,7 @@ This will write `input.txt` and `output.txt` in your working directory.
```bash
python3 main.py \
--message "This is a 40-character test message...." \
--message "Lorem ipsum dolor sit amet. consectetuer" \
--trials 1 \
--mode server \
--input_file input.txt \

34
main.py
View file

@ -21,14 +21,12 @@ def transmit(msg, C):
'''
return encoder.encode_message(msg, C)
def receive_local(c):
'''
Sends the samples through the local channel simulation.
'''
return channel.channel(c)
def receive_server(c, hostname, port):
'''
Sends the samples to the remote server via client.py. If INPUT_FILE and/or
@ -72,7 +70,6 @@ def receive_server(c, hostname, port):
os.remove(out_name)
return Y
def receive(c, mode, hostname, port):
'''
Wrapper to choose local or server channel.
@ -82,18 +79,23 @@ def receive(c, mode, hostname, port):
elif mode == 'server':
return receive_server(c, hostname, port)
else:
raise ValueError('Mode must be \'local\' or \'server\'')
raise ValueError("Mode must be 'local' or 'server'")
def test_performance(msg, num_trials, mode, hostname, port):
'''
Runs num_trials transmissions of msg through the specified channel and reports accuracy.
Runs num_trials transmissions of msg through the specified channel and reports
per-trial decoded messages and error counts, plus overall accuracy.
'''
if len(msg) != 40:
raise ValueError('Message must be exactly 40 characters.')
# Build codebook for 64 symbols, 40 blocks
C = encoder.make_codebook(r=5, num_blocks=40)
successes = 0
print(f"Original message: {msg}")
print(f"Running {num_trials} trials over '{mode}' channel...\n")
for i in range(num_trials):
# Transmit
c = transmit(msg, C)
@ -101,16 +103,19 @@ def test_performance(msg, num_trials, mode, hostname, port):
Y = receive(c, mode, hostname, port)
# Decode
est = decoder.decode_blocks(Y, C)
if est == msg:
# Count character errors
errors = sum(1 for a, b in zip(est, msg) if a != b)
# Tally success if no errors
if errors == 0:
successes += 1
pct = successes / num_trials * 100
# Display results
print(f'Message: {msg}')
print(f'Trials: {num_trials}')
print(f'Mode: {mode}')
print(f'Correct decodings: {successes}')
print(f'Accuracy: {pct:.2f}%')
# Print trial result
print(f"Trial {i+1:3d}: Decoded: '{est}' | Errors: {errors}")
pct = successes / num_trials * 100
print("\n=== Summary ===")
print(f"Total trials: {num_trials}")
print(f"Perfect decodings: {successes}")
print(f"Overall accuracy: {pct:.2f}%")
def parse_args():
parser = argparse.ArgumentParser(description='Test communication system performance.')
@ -132,7 +137,6 @@ def parse_args():
if __name__ == '__main__':
args = parse_args()
# Set global paths
INPUT_FILE = args.input_file
OUTPUT_FILE = args.output_file
test_performance(args.message, args.trials, args.mode, args.hostname, args.port)