mirror of
https://github.com/romanz/amodem.git
synced 2026-03-17 07:05:59 +08:00
optimize error checking
This commit is contained in:
24
errors.py
24
errors.py
@@ -1,20 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
import common
|
||||
import bitarray
|
||||
import sys
|
||||
|
||||
tx, rx = sys.argv[1:]
|
||||
tx = bytearray(open(tx).read())
|
||||
rx = bytearray(open(rx).read())
|
||||
tx_fname, rx_fname = sys.argv[1:]
|
||||
tx = bitarray.bitarray()
|
||||
tx.fromfile(open(tx_fname))
|
||||
|
||||
rx = bitarray.bitarray()
|
||||
rx.fromfile(open(rx_fname))
|
||||
|
||||
L = min(len(tx), len(rx))
|
||||
if L == 0:
|
||||
sys.exit(1)
|
||||
|
||||
rx = list(common.to_bits(rx[:L]))
|
||||
tx = list(common.to_bits(tx[:L]))
|
||||
indices = [index for index, (r, t) in enumerate(zip(rx, tx)) if r != t]
|
||||
|
||||
total = L*8
|
||||
errors = len(indices)
|
||||
print('{}/{} bit error rate: {:.3f}%'.format(errors, total, (100.0 * errors) / total))
|
||||
rx = rx[:L]
|
||||
tx = tx[:L]
|
||||
errors = (rx ^ tx).count(1)
|
||||
total = L
|
||||
ber = float(errors) / total # bit error rate
|
||||
print('BER: {:.3f}% ({}/{})'.format(100 * ber, errors, total))
|
||||
sys.exit(int(errors > 0))
|
||||
|
||||
Reference in New Issue
Block a user