Example #1
0
 def testOperators (self):
     "Test the operators +, -, *, /, %, ^, &, |"
     x = Numeric.array([1.,2.,3.,4.,5.,6.])
     y = Numeric.array([-1.,2.,0.,2.,-1, 3.])
     assert_eq(x + y, [0., 4., 3., 6., 4., 9.])
     assert_eq(x - y, [2., 0., 3., 2., 6., 3.])
     assert_eq(x * y, [-1., 4., 0., 8., -5., 18.])
     assert_eq(y / x, [-1, 1., 0., .5, -.2, .5])
     assert_eq(x**2, [1., 4., 9., 16., 25., 36.])
     xc = Numeric.array([1.,2.,3.,4.,5.,6.])
     xc += y
     assert_eq(xc, x + y)
     xc = Numeric.array([1.,2.,3.,4.,5.,6.])
     xc -= y
     assert_eq(xc, x - y)
     xc = Numeric.array([1.,2.,3.,4.,5.,6.])
     xc *= y
     assert_eq(xc, x * y)
     yc = Numeric.array(y)
     yc /= x
     assert_eq( yc, y / x)
     assert_eq(x + y, Numeric.add(x, y))
     assert_eq(x - y, Numeric.subtract(x, y))
     assert_eq(x * y, Numeric.multiply(x, y))
     assert_eq(y / x, Numeric.divide (y, x))
     self.failUnlessRaises(ZeroDivisionError, Numeric.divide, 
                           Numeric.array(1), Numeric.array(0))
     assert_eq(x**2, Numeric.power(x,2))
     x = Numeric.array([1,2])
     y = Numeric.zeros((2,))
     assert_eq(x%x, y)
     assert_eq(Numeric.remainder(x,x), y)
     assert_eq(x <<1, [2,4])
     assert_eq(Numeric.left_shift(x,1), [2,4])
     assert_eq(x >>1, [0,1])
     assert_eq(Numeric.right_shift(x,1), [0,1])
     assert_eq(x & 2, [0,2])
     assert_eq(Numeric.bitwise_and (x, 2), [0,2])
     assert_eq(x | 1, [1,3])
     assert_eq(Numeric.bitwise_or (x, 1), [1,3])
     assert_eq(x ^ 2, [3,0])
     assert_eq(Numeric.bitwise_xor(x,2), [3,0])
     x = divmod(Numeric.array([2,1]), Numeric.array([1,2]))
     assert_eq(x[0], [2,0])
     assert_eq(x[1], [0,1])
     assert (4L*Numeric.arange(3)).typecode() == Numeric.PyObject
     x = Numeric.array([1,2,3,4,5,6],'u')
     y = Numeric.array([1,2,0,2,2,3],'u')
     assert_eq(x + y, [2, 4, 3, 6, 7, 9])
     assert_eq(x - y, [0, 0, 3, 2, 3, 3])
     assert_eq(x * y, [1, 4, 0, 8, 10, 18])
     assert_eq(y / x, [1, 1, 0, 0, 0, 0])
     assert_eq(y // x, [1, 1, 0, 0, 0, 0])
     assert_eq(x**2, [1, 4, 9, 16, 25, 36])
Example #2
0
    def receive(self,data):
        if not self.goodData: # This is the first of the two packets.Store it
            self.goodData = data
            return

        # We have the two packets. First check if an error is detected
        self.badData = data
        data , correct = self.verifyChksumFun(data)

        # Convert the strings into array of unsigned 8 bit integers
        # and compute the XOR between the arrays to find the bytes that differ
        goodbytes=Numeric.fromstring(self.goodData,Numeric.UnsignedInt8)
        badbytes=Numeric.fromstring(self.badData,Numeric.UnsignedInt8)
        diff = Numeric.bitwise_xor(goodbytes,badbytes)

        # Convert the diff bytes into an array of bits,
        # e.g. [0,3,2] is converted to [00000000 00000011 00000010]
        diffbits = Numeric.bitwise_and.outer(diff,self.bitmasks).flat

        # Get the indices of all error bits
        errorbits = Numeric.nonzero(diffbits)

        # Compute the statistics
        numErrors = len(errorbits)
        if numErrors:
            burstlength = max(errorbits)-min(errorbits)+1
            if correct:
                # Undetected bit errors !!!
                self.bitErrorsUndetected[numErrors] = (
                    self.bitErrorsUndetected.get(numErrors,0)+1) 
                self.burstsUndetected[burstlength] = (
                    self.burstsUndetected.get(burstlength,0)+1)
            else:
                # Errors have been detected
                self.bitErrorsDetected[numErrors] = (
                    self.bitErrorsDetected.get(numErrors,0)+1) 
                self.burstsDetected[burstlength] = (
                    self.burstsDetected.get(burstlength,0)+1)

        self.goodData = self.badData = None