예제 #1
0
    def __init__(self):
        self.connection = connect_client(type=peers.AMPLIFIER)
        # We need to get basic configuration from hashtable as
        # other modules will expect the same configuration..

        # sampling rate
        self.sampling_rate = int(
            self.connection.query(message="SamplingRate",
                                  type=types.DICT_GET_REQUEST_MESSAGE).message)

        self.num_of_channels = int(
            self.connection.query(message="NumOfChannels",
                                  type=types.DICT_GET_REQUEST_MESSAGE).message)

        sampleVector = variables_pb2.SampleVector()
        for x in range(self.num_of_channels):
            samp = sampleVector.samples.add()
            samp.value = float(x)
            samp.timestamp = time.time()

        self.msg1 = sampleVector.SerializeToString()

        sampleVector = variables_pb2.SampleVector()
        for x in range(self.num_of_channels):
            samp = sampleVector.samples.add()
            samp.value = float(x * -1)
            samp.timestamp = time.time()
        self.msg2 = sampleVector.SerializeToString()

        self.num_of_samples = 0
        self.msg_type = 0
예제 #2
0
    def __init__(self, addresses, duration, cache_size, dump_file, monitor_last_channel):
        self._configurer = configurer.Configurer(addresses)
        configs = self._configurer.get_configs(["NumOfChannels", "SamplesPerVector", "PEER_READY"+str(peers.AMPLIFIER)])
        self.number_of_channels = int(configs['NumOfChannels'])
        self.samples_per_vector = int(configs['SamplesPerVector'])
        if duration == 0: 
            self.duration = sys.float_info.max
        else:
            self.duration = duration
        self.cache_size = cache_size
        self.dump_file = dump_file
        if dump_file is not None:
            self.dump_file = open(dump_file, 'w')
        else:
            self.dump_file = None
        self.monitor_last_channel = monitor_last_channel

        self._prev_last_channel = 0
        self._samples_vector = variables_pb2.SampleVector()
        self._samples_count = 0
        self._start_ts = time.time()
        self._last_log_ts = 0.0
        self._last_log_count = 0
        self._cached_samples = numpy.zeros((self.number_of_channels, self.cache_size))
        self._cache_index = 0
        super(Receiver, self).__init__(addresses=addresses, type=peers.SIGNAL_STREAMER)
        self._configurer.set_configs({'PEER_READY':str(peers.SIGNAL_STREAMER)}, self.conn)

        print("Receiver initialsed! with num of channels: "+str(self.number_of_channels))
예제 #3
0
def get_sample_vector(per, ch, mult):
    sample_vector = variables_pb2.SampleVector()
    for x in range(per):
        samp = sample_vector.samples.add()
        for j in range(ch):
            samp.channels.append(float(j + 1) * mult + x)
        samp.timestamp = 10.0
    return sample_vector
예제 #4
0
    def __init__(self, addresses):
        """Get data from hashtable and create filter - butter by now."""
        super(Filter, self).__init__(addresses=addresses, type=peers.FILTER)
        channels_num = int(
            self.conn.query(message = "NumOfChannels", 
                            type = types.DICT_GET_REQUEST_MESSAGE, 
                            timeout = 1).message)
        sampling = float(
            self.conn.query(message = "SamplingRate", 
                            type = types.DICT_GET_REQUEST_MESSAGE, 
                            timeout = 1).message)
        
        f_level = int(
            self.conn.query(message = "FilterLevel", 
                                  type = types.DICT_GET_REQUEST_MESSAGE, 
                                  timeout = 1).message)
        down = int(
            self.conn.query(message = "FilterDown", 
                            type = types.DICT_GET_REQUEST_MESSAGE, 
                            timeout = 1).message)
        up = int(
            self.conn.query(message = "FilterUp", 
                            type = types.DICT_GET_REQUEST_MESSAGE, 
                            timeout = 1).message)
        f_band = self.conn.query(message = "FilterBand", 
                                 type = types.DICT_GET_REQUEST_MESSAGE, 
                                 timeout = 1).message

        b,a = signal.butter(f_level,[down/sampling, up/sampling],btype=f_band)
        b = list(b)
        a = list(a)

        # reverse a and b for more convinient use
        self.a0 = a[0]
        a = a[1:]
        a.reverse()
        b.reverse()

        self.a = numpy.array(a)
        self.b = numpy.array(b)
        self.len_x = len(b)
        self.len_y = len(a)


        self.last_x = [[0.0]]*channels_num
        self.last_y = [[0.0]]*channels_num
        for i in range(channels_num):
            self.last_x[i] = numpy.array([0.0]*len(b))
            self.last_y[i] = numpy.array([0.0]*len(a))
        self.i = 0

        self.vec = variables_pb2.SampleVector()
        self.time = time.time()
        self.processing_time = 0.0
예제 #5
0
 def handle_message(self, mxmsg):
     """
     Get filtered samples, remeber few first samples, do the job, finish."""
     if mxmsg.type == types.FILTERED_SIGNAL_MESSAGE:
         vec = variables_pb2.SampleVector()
         vec.ParseFromString(mxmsg.message)
         if not self.channels:
             self.channels = []
             for sample in vec.samples:
                 self.channels.append([sample.value])
         else:
             for i, sample in enumerate(vec.samples):
                 self.channels[i].append(sample.value)
             self.determine_scale()
         self.no_response()
예제 #6
0
def get_sample_vector(per, ch_num):
    """generate signal like:
    [ [1,2,3,4,5....
      [10,20,30,40,50...
      [100,200,300, ...
    ...
    ]
    """
    global COUNT
    sample_vector = variables_pb2.SampleVector()
    for x in range(per):
        COUNT += 1
        samp = sample_vector.samples.add()
        for j in range(ch_num):
            samp.channels.append((10**(j)) * COUNT)
        samp.timestamp = 10.0
    return sample_vector
예제 #7
0
 def perform_unpacking_test(self):
     start_time = time.time()
     test_vec = variables_pb2.SampleVector()
     msg = self.sample_vec.SerializeToString()
     print "Start deserializing test: "
     self.logger.mark_start()
     for i in xrange(self.num_of_samples):
         test_vec.ParseFromString(msg)
         #self.logger.log_sample()
     self.logger.mark_end()
     end_time = time.time()
     print "End of unpacking test - time: ", end_time - start_time,\
             " approx. sample rate: ", float(self.num_of_samples) / (end_time - start_time)
     #self.logger.report()
     data_size = len(self.msg) * self.num_of_samples
     print(float(data_size) / 1024 / 1024), " MiB"
     print(float(data_size) / (end_time - start_time) / 1000 / 1000 *
           8), " Mbps"
예제 #8
0
파일: monitor.py 프로젝트: vjenissr/openbci
    def __init__(self, channel_number):
        QtGui.QWidget.__init__(self, None)

        self.channel_number = channel_number

        timer = QtCore.QTimer(self)
        self.connect(timer, QtCore.SIGNAL("timeout()"), self,
                     QtCore.SLOT("update()"))
        timer.start(100)

        self.setWindowTitle(
            self.tr("Monitor of channel " + str(channel_number)))
        self.resize(512, 512)

        self.connection = connect_client(type=peers.MONITOR)

        self.sampling_rate = int(self.connection.query(message="SamplingRate", \
            type=types.DICT_GET_REQUEST_MESSAGE).message)
        self.vec = variables_pb2.SampleVector()
예제 #9
0
    def __init__(self, samp_num=100000, channels_num=25, log_interval=100):

        print "Samples:  ", samp_num, " channels: ", channels_num,\
                " log_interval: ", log_interval

        self.num_of_samples = samp_num
        self.log_interval = log_interval
        self.num_of_channels = channels_num
        self.logger = SampleLogger(self.log_interval)

        self.sample_vec = variables_pb2.SampleVector()
        for x in range(1):  #self.num_of_channels):
            samp = self.sample_vec.samples.add()
            for i in range(self.num_of_channels):
                samp.channels.append(float(x))
            samp.timestamp = time.time()

        self.msg = self.sample_vec.SerializeToString()
        print "Approx. serialized sample vector size: ", len(self.msg)
예제 #10
0
    def __init__(self, addresses):
        """Get data from hashtable and create filter - butter by now."""
        super(Filter, self).__init__(addresses=addresses, type=peers.FILTER)
        channels_num = int(
            self.conn.query(message="NumOfChannels",
                            type=types.DICT_GET_REQUEST_MESSAGE,
                            timeout=1).message)
        sampling = float(
            self.conn.query(message="SamplingRate",
                            type=types.DICT_GET_REQUEST_MESSAGE,
                            timeout=1).message)

        f_level = int(
            self.conn.query(message="FilterLevel",
                            type=types.DICT_GET_REQUEST_MESSAGE,
                            timeout=1).message)
        down = int(
            self.conn.query(message="FilterDown",
                            type=types.DICT_GET_REQUEST_MESSAGE,
                            timeout=1).message)
        up = int(
            self.conn.query(message="FilterUp",
                            type=types.DICT_GET_REQUEST_MESSAGE,
                            timeout=1).message)
        f_band = self.conn.query(message="FilterBand",
                                 type=types.DICT_GET_REQUEST_MESSAGE,
                                 timeout=1).message

        b, a = signal.butter(f_level, [down / sampling, up / sampling],
                             btype=f_band)
        self.b = b
        self.a = a
        f_size = len(self.a) + 1
        self.last_x = [[0.0]] * channels_num
        self.last_y = [[0.0]] * channels_num
        for i in range(channels_num):
            self.last_x[i] = [0.0] * f_size
            self.last_y[i] = [0.0] * f_size
        self.i = 0
        self.vec = variables_pb2.SampleVector()
        self.time = time.time()
        self.processing_time = 0.0
예제 #11
0
    def paintEvent(self, event):
        painter = QtGui.QPainter(self)
        painter.setRenderHint(QtGui.QPainter.Antialiasing)

        painter.setPen(self.colorb)

        data = self.connection.query(message=str(self.channel_number),
                                     type=types.SIGNAL_CATCHER_REQUEST_MESSAGE,
                                     timeout=5).message
        #d = cPickle.loads(data)
        vec = variables_pb2.SampleVector()
        vec.ParseFromString(data)
        d = []
        for x in vec.samples:
            d.append(x.value)
        d2 = d[-int(self.sampling_rate * 4):]
        d2 = abs(numpy.fft.rfft(d2))
        d2[0] = 0
        d2[1] = 0
        j = len(d2)
        for i in range(j):
            if i < 5 * 4 or i > 45 * 4:
                d2[i] = 0
        s = 0
        for x in range(7 * 4, 15 * 4):
            s += d2[x]
        self.ms = max(s, self.ms)
        m = max(d2)
        #print s
        d3 = [400. * x / m for x in d2]
        for i in xrange(len(d3)):
            if i % 8 == 0:
                painter.drawLine(10 + i, 90 - 3 * (i % 32), 10 + i, 100)
                painter.drawText(5 + i, 85 - 3 * (i % 32), str(i / 4))
        painter.setPen(self.colora)
        for i in xrange(len(d3)):
            painter.drawLine(10 + i, 100, 10 + i, 100 + int(d3[i]))
            if d3[i] > 100:
                painter.drawText(5 + i, 110 + int(d3[i]), str(i / 4.))
        painter.drawLine(620, 10 + self.ms / 30., 680, 10 + self.ms / 30.)
        painter.setBrush(QtCore.Qt.green)
        painter.drawRect(640, 10, 40, s / 30.)
예제 #12
0
    def _init_rest(self):
        self._samples_count = 0
        self._sample_interval = (
            1 / float(self.sampling_frequency)
        ) * INTERVAL_FIX_CONSTANT * self._samples_per_vector
        self._start_ts = time.time()
        self._last_log_ts = 0.0
        self._last_log_count = 0

        t = time.time()

        self._samples_vector = variables_pb2.SampleVector()
        for x in range(self._samples_per_vector):
            if self.values_type == 1: v = float(self._samples_count)
            else: v = random.random()
            samp = self._samples_vector.samples.add()
            print(dir(samp.channels))
            for j in range(self.number_of_channels):
                samp.channels.append(v)
            samp.timestamp = t
예제 #13
0
    def handle_message(self, mxmsg):
        if mxmsg.type == types.AMPLIFIER_SIGNAL_MESSAGE:
            vec = variables_pb2.SampleVector()
            vec.ParseFromString(mxmsg.message)
            d = []
            for x in vec.samples:
                d.append(x.value)

        # d = cPickle.loads(mxmsg.message)
        # d = list(d)
            # print d
            # print self.macierz
            res = matrixmultiply([d], self.macierz)[0]
            # s = " ".join(str(x) for x in list(res))
            for i in range(len(vec.samples)):
                vec.samples[i].value = res[i]
            self.conn.send_message(message=vec.SerializeToString(),
                                   type=types.FILTERED_SIGNAL_MESSAGE,
                                   flush=True)

            #self.conn.send_message(message=mxmsg.message, type=types.FILTERED_SIGNAL_MESSAGE, flush=True)
            self.no_response()