# Note: This task will only work if the basic (hard-decision decoding) Viterbi # decoder from PS3_viterbi 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): diff = 0 for i in xrange(len(expected)): diff += (expected[i] - received[i])**2 return diff if __name__=='__main__': # Test whether branch metrics are as expected PS3_tests.test_soft_metrics(SoftViterbiDecoder) # 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, 5) soft = SoftViterbiDecoder(K, glist) nbits = 1000 sigma = 0.5 # noise defined as 2*sigma^2 = 1/8 numpy.random.seed(617617617)
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 bm = 0 for i in range(len(expected)): bm = bm + (expected[i] - received[i]) * (expected[i] - received[i]) return bm if __name__ == '__main__': # Test whether branch metrics are as expected PS3_tests.test_soft_metrics(SoftViterbiDecoder) # 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, 5) soft = SoftViterbiDecoder(K, glist) nbits = 1000 sigma = 0.5 # noise defined as 2*sigma^2 = 1/8 numpy.random.seed(617617617)