# This is the template file for Lab #4, Task #4 import numpy,random,sys import lab4 from lab4_3 import ViterbiDecoder # Note: Task 4 will only work if Task 3 does class SoftViterbiDecoder(ViterbiDecoder): # Override the default branch metric with a soft decision metric: # the square of the Euclidian distance between the # expected and received voltages. def branch_metric(self,expected,received): # your code here... pass if __name__=='__main__': # Test whether branch metrics are as expected (needed for checkoff) if lab4.verify_task4(SoftViterbiDecoder): print "Branch metric test for soft decoder matches" else: print "Soft decision branch metric test failed" sys.exit(1) # This test for soft decoding produces a random message of # nbits=1000 in length, from a fixed random seed (so each time the # program is run, the same behavior will occur. We encode the # message and then use your soft decoder (code above) to test it. # If the decoded stream is the same as what we expect from our # encoder, we consider the test case to have passed. Note, it is # possible (though not likely) that your soft decoder may not pass # this test case, but is in fact correct. That's why we have not # made the test case part of the verification before checkoff. K, glist = 3, (7,6) soft = SoftViterbiDecoder(K, glist) nbits = 1000 sigma = 0.5 # noise defined as 2*sigma^2 = 1/8 numpy.random.seed(617617617) message = numpy.random.random_integers(0,1,nbits) expected = numpy.array([1,1,0,0,1,0,1,0,0,0,0,0,0,0,0,1,1,0,1,1,0,1,0,1,1,0,0,1,0,0,1,0,1,0,0,0,1,1,1,0,1,1,1,1,0,1,0,1,0,1,0,0,1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,1,1,1,0,1,0,0,0,0,1,1,1,0,1,0,0,1,1,0,0,1,0,1,1,1,0,0,0,1,0,0,1,0,1,0,0,1,0,1,0,1,1,0,0,0,1,0,1,0,0,0,1,0,1,1,0,1,0,1,0,1,1,1,0,1,1,0,0,1,1,0,0,0,0,1,1,1,1,1,0,1,1,0,0,1,1,1,1,0,1,1,0,1,0,1,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,1,0,0,0,1,1,0,1,1,1,1,0,0,0,0,1,0,0,1,0,1,1,0,0,1,1,0,1,1,1,1,0,1,0,0,0,0,0,1,1,0,1,0,0,0,1,0,1,1,1,0,1,0,0,0,0,0,0,1,0,0,0,1,1,1,1,1,1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,0,0,0,1,0,1,1,0,0,1,1,1,1,1,1,0,0,1,0,0,1,0,1,1,1,1,0,0,0,0,1,1,1,0,1,1,0,1,0,0,1,1,1,0,1,1,0,1,0,1,0,1,0,1,1,0,0,1,1,0,0,1,1,0,1,0,1,1,0,1,1,1,1,0,1,0,0,1,1,1,0,1,0,1,0,0,1,0,1,0,1,1,0,0,0,1,1,0,0,0,0,1,0,0,1,1,0,1,1,0,0,1,0,0,1,0,1,1,1,0,0,1,1,1,0,0,1,0,0,1,1,0,0,0,0,0,1,1,0,0,0,1,0,0,1,1,1,0,1,1,1,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,0,1,1,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,1,1,1,0,0,1,1,0,0,0,1,1]) encoded = lab4.convolutional_encoder(message, K, glist) noisy = numpy.random.normal(0,sigma,len(encoded)) received = encoded + noisy decoded = numpy.array(soft.decode(received)) if (decoded == expected).all() == True: print 'Soft decoding succeeded on test case: congratulations!' else: print 'Oops... error in soft decoding' print 'Decoded as', decoded print 'Expected ', expected sys.exit(1) # When ready for checkoff, enable the following line. #lab4.checkoff(SoftViterbiDecoder,task='L4_4',debug=False)