Exemple #1
0
 def test_will_fit(self):
     ring_buffer = RingBuffer(size=8)
     self.assertEqual(ring_buffer.space_left, 8)
     self.assertTrue(ring_buffer.will_fit(4))
     self.assertTrue(ring_buffer.will_fit(8))
     self.assertFalse(ring_buffer.will_fit(9))
     ring_buffer.push(np.array([1, 2, 3, 4], dtype=np.complex64))
     self.assertEqual(ring_buffer.space_left, 4)
     self.assertTrue(ring_buffer.will_fit(3))
     self.assertTrue(ring_buffer.will_fit(4))
     self.assertFalse(ring_buffer.will_fit(5))
Exemple #2
0
 def test_will_fit(self):
     ring_buffer = RingBuffer(size=8)
     self.assertEqual(ring_buffer.space_left, 8)
     self.assertTrue(ring_buffer.will_fit(4))
     self.assertTrue(ring_buffer.will_fit(8))
     self.assertFalse(ring_buffer.will_fit(9))
     ring_buffer.push(np.array([1, 2, 3, 4], dtype=np.complex64))
     self.assertEqual(ring_buffer.space_left, 4)
     self.assertTrue(ring_buffer.will_fit(3))
     self.assertTrue(ring_buffer.will_fit(4))
     self.assertFalse(ring_buffer.will_fit(5))
Exemple #3
0
    def test_pop(self):
        ring_buffer = RingBuffer(size=5)
        add1 = np.array([1, 2, 3], dtype=np.complex64)
        ring_buffer.push(add1)
        self.assertTrue(np.array_equal(add1, ring_buffer.pop(40)))
        self.assertTrue(ring_buffer.is_empty)

        add2 = np.array([1, 2, 3, 4], dtype=np.complex64)
        ring_buffer.push(add2)
        self.assertTrue(np.array_equal(add2, ring_buffer.pop(4)))
        self.assertTrue(ring_buffer.is_empty)

        add3 = np.array([1, 2], dtype=np.complex64)
        ring_buffer.push(add3)
        popped_item = ring_buffer.pop(1)
        self.assertTrue(np.array_equal(add3[0:1], popped_item),
                        msg=popped_item)
        self.assertFalse(ring_buffer.is_empty)

        add4 = np.array([7, 8, 9, 10], dtype=np.complex64)
        ring_buffer.push(add4)
        self.assertFalse(ring_buffer.will_fit(1))
        self.assertTrue(
            np.array_equal(np.concatenate((np.atleast_1d(add3[1]), add4)),
                           ring_buffer.pop(5)))
Exemple #4
0
    def test_push(self):
        ring_buffer = RingBuffer(size=10)
        self.assertEqual(0, ring_buffer.left_index)

        add1 = np.array([1, 2, 3, 4, 5], dtype=np.complex64)
        ring_buffer.push(add1)
        self.assertEqual(5, ring_buffer.right_index)
        self.assertTrue(np.array_equal(ring_buffer.data[0:5], add1))

        add2 = np.array([10, 20, 30, 40, 50, 60], dtype=np.complex64)
        self.assertFalse(ring_buffer.will_fit(len(add2)))
        ring_buffer.push(add2[:-1])
        self.assertTrue(np.array_equal(ring_buffer.data[5:10], add2[:-1]))
        self.assertTrue(np.array_equal(ring_buffer.data[0:5], add1))
Exemple #5
0
    def test_push(self):
        ring_buffer = RingBuffer(size=10)
        self.assertEqual(0, ring_buffer.left_index)

        add1 = np.array([1, 2, 3, 4, 5], dtype=np.complex64)
        ring_buffer.push(add1)
        self.assertEqual(5, ring_buffer.right_index)
        self.assertTrue(np.array_equal(ring_buffer.data[0:5], add1))

        add2 = np.array([10, 20, 30, 40, 50, 60], dtype=np.complex64)
        self.assertFalse(ring_buffer.will_fit(len(add2)))
        ring_buffer.push(add2[:-1])
        self.assertTrue(np.array_equal(ring_buffer.data[5:10], add2[:-1]))
        self.assertTrue(np.array_equal(ring_buffer.data[0:5], add1))
Exemple #6
0
    def test_pop(self):
        ring_buffer = RingBuffer(size=5)
        add1 = np.array([1, 2, 3], dtype=np.complex64)
        ring_buffer.push(add1)
        self.assertTrue(np.array_equal(add1, ring_buffer.pop(40)))
        self.assertTrue(ring_buffer.is_empty)

        add2 = np.array([1, 2, 3, 4], dtype=np.complex64)
        ring_buffer.push(add2)
        self.assertTrue(np.array_equal(add2, ring_buffer.pop(4)))
        self.assertTrue(ring_buffer.is_empty)

        add3 = np.array([1, 2], dtype=np.complex64)
        ring_buffer.push(add3)
        popped_item = ring_buffer.pop(1)
        self.assertTrue(np.array_equal(add3[0:1], popped_item), msg=popped_item)
        self.assertFalse(ring_buffer.is_empty)

        add4 = np.array([7, 8, 9, 10], dtype=np.complex64)
        ring_buffer.push(add4)
        self.assertFalse(ring_buffer.will_fit(1))
        self.assertTrue(np.array_equal(np.concatenate((np.atleast_1d(add3[1]), add4)), ring_buffer.pop(5)))
class ContinuousModulator(object):
    """
    This class is used in continuous sending mode.
    You pass a list of messages and modulators to it, and it takes care of modulating the messages sequentially.
    This avoids running out of RAM for large amounts of messages.
    """

    BUFFER_SIZE_MB = 100
    WAIT_TIMEOUT = 0.1

    def __init__(self, messages, modulators):
        """
        
        :type messages: list of Message 
        :type modulators: list of Modulator 
        """
        self.messages = messages
        self.modulators = modulators

        self.ring_buffer = RingBuffer(int(self.BUFFER_SIZE_MB*10**6)//8)

        self.current_message_index = Value("L", 0)

        self.abort = Value("i", 0)
        self.process = Process(target=self.modulate_continuously)
        self.process.daemon = True

    @property
    def is_running(self):
        return self.process.is_alive()

    def start(self):
        self.abort.value = 0
        try:
            self.process = Process(target=self.modulate_continuously)
            self.process.daemon = True
            self.process.start()
        except RuntimeError as e:
            logger.debug(str(e))

    def stop(self, clear_buffer=True):
        self.abort.value = 1
        if clear_buffer:
            self.ring_buffer.clear()
        if not self.process.is_alive():
            return

        try:
            self.process.join(0.1)
        except RuntimeError as e:
            logger.debug(str(e))

        if self.process.is_alive():
            self.process.terminate()
            self.process.join()

        logger.debug("Stopped continuous modulation")

    def modulate_continuously(self):
        pos = 0
        while True:
            start = self.current_message_index.value
            for i in range(start, len(self.messages)):
                if self.abort.value:
                    return

                message = self.messages[i]
                self.current_message_index.value = i
                modulator = self.modulators[message.modulator_indx]  # type: Modulator
                modulator.modulate(start=pos, data=message.encoded_bits, pause=message.pause)
                while not self.ring_buffer.will_fit(len(modulator.modulated_samples)):
                    if self.abort.value:
                        return

                    # Wait till there is space in buffer
                    time.sleep(self.WAIT_TIMEOUT)
                self.ring_buffer.push(modulator.modulated_samples)
                pos += len(modulator.modulated_samples)
class ContinuousModulator(object):
    """
    This class is used in continuous sending mode.
    You pass a list of messages and modulators to it, and it takes care of modulating the messages sequentially.
    This avoids running out of RAM for large amounts of messages.
    """

    BUFFER_SIZE_MB = 100
    WAIT_TIMEOUT = 0.1

    def __init__(self, messages, modulators):
        """
        
        :type messages: list of Message 
        :type modulators: list of Modulator 
        """
        self.messages = messages
        self.modulators = modulators

        self.ring_buffer = RingBuffer(int(self.BUFFER_SIZE_MB * 10**6) // 8)

        self.current_message_index = Value("L", 0)

        self.abort = Value("i", 0)
        self.process = Process(target=self.modulate_continuously)
        self.process.daemon = True

    @property
    def is_running(self):
        return self.process.is_alive()

    def start(self):
        self.abort.value = 0
        try:
            self.process = Process(target=self.modulate_continuously)
            self.process.daemon = True
            self.process.start()
        except RuntimeError as e:
            logger.debug(str(e))

    def stop(self, clear_buffer=True):
        self.abort.value = 1
        if clear_buffer:
            self.ring_buffer.clear()
        if not self.process.is_alive():
            return

        try:
            self.process.join(0.1)
        except RuntimeError as e:
            logger.debug(str(e))

        if self.process.is_alive():
            self.process.terminate()
            self.process.join()

        logger.debug("Stopped continuous modulation")

    def modulate_continuously(self):
        pos = 0
        while True:
            start = self.current_message_index.value
            for i in range(start, len(self.messages)):
                if self.abort.value:
                    return

                message = self.messages[i]
                self.current_message_index.value = i
                modulator = self.modulators[
                    message.modulator_indx]  # type: Modulator
                modulator.modulate(start=pos,
                                   data=message.encoded_bits,
                                   pause=message.pause)
                while not self.ring_buffer.will_fit(
                        len(modulator.modulated_samples)):
                    if self.abort.value:
                        return

                    # Wait till there is space in buffer
                    time.sleep(self.WAIT_TIMEOUT)
                self.ring_buffer.push(modulator.modulated_samples)
                pos += len(modulator.modulated_samples)
Exemple #9
0
class ContinuousModulator(object):
    """
    This class is used in continuous sending mode.
    You pass a list of messages and modulators to it, and it takes care of modulating the messages sequentially.
    This avoids running out of RAM for large amounts of messages.
    """
    WAIT_TIMEOUT = 0.1

    def __init__(self, messages, modulators, num_repeats=-1):
        """
        
        :type messages: list of Message 
        :type modulators: list of Modulator 
        """
        self.messages = messages
        self.modulators = modulators
        self.num_repeats = num_repeats  # -1 or 0 = infinite

        self.ring_buffer = RingBuffer(
            int(settings.CONTINUOUS_BUFFER_SIZE_MB * 1e6) // 8,
            dtype=Modulator.get_dtype())

        self.current_message_index = Value("L", 0)

        self.abort = Value("i", 0)
        self.process = Process(target=self.modulate_continuously,
                               args=(self.num_repeats, ),
                               daemon=True)

    @property
    def is_running(self):
        return self.process.is_alive()

    def start(self):
        self.abort.value = 0
        try:
            self.process = Process(target=self.modulate_continuously,
                                   args=(self.num_repeats, ),
                                   daemon=True)
            self.process.start()
        except RuntimeError as e:
            logger.exception(e)

    def stop(self, clear_buffer=True):
        self.abort.value = 1

        if self.process.is_alive():
            try:
                self.process.join(1.5)
            except RuntimeError as e:
                logger.exception(e)
                self.process.terminate()

        if clear_buffer:
            self.ring_buffer.clear()

        logger.debug("Stopped continuous modulation")

    def modulate_continuously(self, num_repeats):
        rng = iter(int, 1) if num_repeats <= 0 else range(
            0, num_repeats)  # <= 0 = forever
        for _ in rng:
            if self.abort.value:
                return

            start = self.current_message_index.value

            for i in range(start, len(self.messages)):
                if self.abort.value:
                    return

                message = self.messages[i]
                self.current_message_index.value = i
                modulator = self.modulators[
                    message.modulator_index]  # type: Modulator
                modulated = modulator.modulate(start=0,
                                               data=message.encoded_bits,
                                               pause=message.pause)
                while not self.ring_buffer.will_fit(len(modulated)):
                    if self.abort.value:
                        return

                    # Wait till there is space in buffer
                    time.sleep(self.WAIT_TIMEOUT)

                self.ring_buffer.push(modulated)

            self.current_message_index.value = 0