Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import factorial as fact
- from random import random
- class Packet(object):
- def __init__(self, nDataBits, nOverheadBits, nRedundantBits):
- self.u = nDataBits
- self.o = nOverheadBits
- self.k = nDataBits + nOverheadBits
- self.n = nDataBits + nOverheadBits + nRedundantBits
- self.computeHammingBound()
- def send(self, bitErrorRate):
- '''
- Repeatedly attempts to send the packet until successful while simulating
- the specified bit error rate. Returns the efficiency of the packet.
- '''
- attempts = 0
- sent = False
- while not sent:
- attempts += 1
- errors = 0
- for i in xrange(self.n):
- if random() < bitErrorRate:
- errors += 1
- sent = (errors <= self.t)
- return float(self.u)/(self.n*attempts)
- def computeHammingBound(self):
- limit = 2**(self.n-self.k)
- total = 0
- t = 0
- while total <= limit:
- print total,limit
- total += fact(self.n) / (fact(t)*fact(self.n-t))
- t += 1
- self.t = t-2
- def __repr__(self):
- return ("=PACKET=======\n"+
- "Data bits: %d\n"%self.u+
- "Overhead bits: %d\n"%self.o+
- "Redundant bits: %d\n"%(self.n-self.k)+
- "Hamming bound: %d\n"%self.t+
- "==============")
- if __name__ == '__main__':
- while True:
- nDataBits = input("Input user data size: ")
- nRedundantBits = input("Input redundant bits: ")
- bitErrorRate = input("Input bit error rate: ")
- packet = Packet(nDataBits, 100, nRedundantBits)
- print "Packet ready:"
- print packet
- raw_input("Press enter to simulate 1,000,000 transmissions with bit "+
- "error rate of %f"%bitErrorRate)
- efficiency = 0.0
- N = 1000000
- for i in xrange(100):
- for _ in xrange(N/100):
- efficiency += packet.send(bitErrorRate)
- print "%d%% complete..."%i
- efficiency /= N
- print "1,000,000 packets transferred with %2.3f efficiency"%efficiency
- if raw_input("Again? y/n: ").lower() in ("n","no","x","q"):
- exit()
Add Comment
Please, Sign In to add comment