Пример #1
0
 def handle_packet(self, pdu):
     t0 = time.time()
     self.packet_cnt += 1
     tag_dict = pmt.car(pdu)
     vec = pmt.cdr(pdu)
     # tag_p = pmt.to_python(tag)
     data_c = pmt.to_python(vec)
     # update
     if self.run_mode == "train":
         self.train_preamble(data_c[0:len(self.preamble_si)])
     # demod
     labels_g = integers_to_bits(self.demodulate(data_c, 'exploit'),
                                 self.bits_per_symbol)
     # Sample some BERs during training, but sample all while frozen to collect average
     # behavior faster
     if self.packet_cnt % self.log_constellation_interval == 0 or self.run_mode == "freeze":
         ber = sum(numpy.abs(self.preamble - labels_g[:self.preamble.size])
                   ) * 1.0 / self.preamble.size
         with open("ber_{}.csv".format(self.uuid_str), "a") as f:
             f.write("{},{}\n".format(self.packet_cnt, ber))
     # publish
     self.message_port_pub(
         self.port_id_out,
         pmt.cons(pmt.PMT_NIL,
                  pmt.to_pmt(labels_g.astype(dtype=numpy.int8))))
     t1 = time.time()
     self.logger.debug(
         "neural demod {} handled {} bits in {} seconds".format(
             self.uuid_str, labels_g.size, t1 - t0))
Пример #2
0
    def handle_demod_classic(self, pdu):
        t0 = time.time()
        self.demod_packet_cnt += 1
        tag_dict = pmt.car(pdu)
        vec = pmt.to_python(pmt.cdr(pdu))
        _, _, new_echo_s, my_echo_s = self.split_packet_iq(vec)
        bits = util_data.integers_to_bits(self.demod.demodulate(vec),
                                          self.bits_per_symbol)
        spy, hdr, new_echo, _ = self.split_packet_bits(bits)
        # Check spy header to see if packet is corrupt
        if self.spy_length > 0:
            spy_ber = sum(spy != self.spy_master) * 1.0 / self.spy_length
        else:
            spy_ber = 0
        # Interpret header
        if hdr is not None:
            idxpre = hdr[0]
            idxecho = hdr[1]
            valid = hdr[2]
        else:
            valid = False
            idxpre = (1 << 16) - 1
            idxecho = (1 << 16) - 1
            if spy_ber < self.spy_threshold:
                self.logger.debug(
                    "classic demod {} spy passed ({}) but header failed to decode"
                    .format(self.uuid_str, spy_ber))
        if spy_ber > self.spy_threshold:
            # BAD PACKET!
            self.logger.debug(
                "classic demod {} spy ber {} above threshold {}".format(
                    self.uuid_str, spy_ber, self.spy_threshold))
            # Publish to both ports so mod can decide what to do with the bad packet
            #self.message_port_pub(self.port_id_corrupt,
            #                      pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(np.int8))))
            self.message_port_pub(
                self.port_id_demod_out,
                pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(np.int8))))
        else:
            # Publish good packet
            self.demod_update_cnt += 1
            self.message_port_pub(
                self.port_id_demod_out,
                pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(np.int8))))

            try:
                preamble = self.get_preamble_hist(idxecho)[:self.preamble.size]
                ber = sum(preamble != new_echo) * 1.0 / self.preamble.size
                if self.demod_update_cnt % self.log_interval == 0 or self.run_mode == 'freeze':
                    with open("ber_{}.csv".format(self.uuid_str), "a") as f:
                        f.write("{},{}\n".format(self.demod_update_cnt, ber))
            except KeyError as e:
                self.logger.info(
                    "DEBUG::Unable to calculate BER with stored index {}".
                    format(idxecho))
        t1 = time.time()
        self.logger.debug(
            "classic demod {} handled {} bits in {} seconds".format(
                self.uuid_str, bits.size, t1 - t0))
        return bits
Пример #3
0
 def handle_packet(self, pdu):
     t0 = time.time()
     self.packet_cnt += 1
     tag_dict = pmt.car(pdu)
     vec = pmt.to_python(pmt.cdr(pdu))
     bits = util_data.integers_to_bits(self.demodulate(vec),
                                       self.bits_per_symbol)
     self.message_port_pub(
         self.port_id_out,
         pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(numpy.int8))))
     if self.preamble is not None:
         # Compare decoded preamble to known version to get a BER estimate
         errs = 0
         for a, b in zip(self.preamble, bits):
             if a != b:
                 errs += 1
         self.ber = errs * 1.0 / len(self.preamble)
         if self.packet_cnt % self.log_ber_interval == 0:
             with open("ber_{}.csv".format(self.uuid_str), "a") as f:
                 f.write("{},{}\n".format(self.packet_cnt, self.ber))
     t1 = time.time()
     self.logger.debug(
         "classic demod {} handled {} bits in {} seconds".format(
             self.uuid_str, bits.size, t1 - t0))
Пример #4
0
 def handle_packet(self, pdu):
     t0 = time.time()
     self.packet_cnt += 1
     tag_dict = pmt.car(pdu)
     vec = pmt.cdr(pdu)
     # tag_p = pmt.to_python(tag)
     data_c = pmt.to_python(vec)
     nbits = data_c.size * self.bits_per_symbol
     spy_s, hdr_s, new_echo_s, my_echo_s = self.split_packet_iq(data_c)
     spy_b = integers_to_bits(self.spy_demod.demodulate(spy_s),
                              self.bits_per_symbol)
     hdr_b = integers_to_bits(self.spy_demod.demodulate(hdr_s),
                              self.bits_per_symbol)
     new_echo_b = integers_to_bits(self.demodulate(new_echo_s),
                                   self.bits_per_symbol)
     my_echo_b = integers_to_bits(self.demodulate(my_echo_s),
                                  self.bits_per_symbol)
     bits = np.concatenate([spy_b, hdr_b, new_echo_b, my_echo_b])
     _, hdr, _, _ = self.split_packet_bits(bits)
     if hdr is not None:
         valid = hdr[0]
         pktidx = hdr[1]
     else:
         valid = False
     # Check spy field for corruption
     if self.spy_length > 0:
         spy_ber = sum(spy_b != self.preamble[:self.spy_length]
                       ) * 1.0 / self.spy_length
     else:
         spy_ber = 0
     if spy_ber > self.spy_threshold:
         # BAD PACKET!
         self.logger.debug(
             "neural demod {} spy ber {} above threshold {}".format(
                 self.uuid_str, spy_ber, self.spy_threshold))
         # Publish to both ports so mods can decide how to handle corruptions
         self.message_port_pub(
             self.port_id_corrupt,
             pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(np.int8))))
         self.message_port_pub(
             self.port_id_out,
             pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(np.int8))))
     else:
         # Good packet, demod and train with the neural agent
         self.update_cnt += 1
         ###DEBUG###
         idx = self.update_cnt if hdr is None else pktidx
         np.save("received_preamble_{}".format(idx), new_echo_s)
         np.save("received_echo_{}".format(idx), my_echo_s)
         ###DEBUG###
         # update
         if self.run_mode == "train":
             self.train_preamble(new_echo_s)
         # demod
         new_echo_b = integers_to_bits(
             self.demodulate(new_echo_s, 'exploit'), self.bits_per_symbol)
         my_echo_b = integers_to_bits(self.demodulate(my_echo_s, 'exploit'),
                                      self.bits_per_symbol)
         labels_g = np.concatenate([spy_b, hdr_b, new_echo_b, my_echo_b])
         # Sample some BERs during training, but sample all while frozen to collect average
         # behavior faster
         if self.update_cnt % self.log_constellation_interval == 0 or self.run_mode == "freeze":
             ber = sum(
                 new_echo_b != self.preamble) * 1.0 / self.preamble.size
             with open("ber_{}.csv".format(self.uuid_str), "a") as f:
                 f.write("{},{}\n".format(self.packet_cnt, ber))
         # publish
         self.message_port_pub(
             self.port_id_out,
             pmt.cons(pmt.PMT_NIL,
                      pmt.to_pmt(labels_g.astype(dtype=np.int8))))
     t1 = time.time()
     self.logger.debug(
         "neural demod {} handled {} bits in {} seconds".format(
             self.uuid_str, nbits, t1 - t0))
    def handle_packet(self, pdu):
        t0 = time.time()
        self.packet_cnt += 1
        tag_dict = pmt.car(pdu)
        vec = pmt.to_python(pmt.cdr(pdu))
        _, _, new_echo_s, my_echo_s = self.split_packet_iq(vec)
        ###DEBUG###
        if self.alias == "classic-agent":
            n = self.spy_length / 2
            vec[n:] += (np.random.randn(vec.size - n) +
                        1j * np.random.randn(vec.size - n)) * 0.1
        ###DEBUG###
        bits = util_data.integers_to_bits(self.demodulate(vec),
                                          self.bits_per_symbol)
        spy, hdr, new_echo, _ = self.split_packet_bits(bits)
        if hdr is not None:
            valid = hdr[0]
            pktidx = hdr[1]
        else:
            valid = False
        # Check spy header to see if packet is corrupt
        if self.spy_length > 0:
            spy_ber = sum(
                spy != self.preamble[:self.spy_length]) * 1.0 / self.spy_length
        else:
            spy_ber = 0
        if spy_ber > self.spy_threshold:
            # BAD PACKET!
            self.logger.debug(
                "classic demod {} spy ber {} above threshold {}".format(
                    self.uuid_str, spy_ber, self.spy_threshold))
            # Publish to both ports so mod can decide what to do with the bad packet
            self.message_port_pub(
                self.port_id_corrupt,
                pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(np.int8))))
            self.message_port_pub(
                self.port_id_out,
                pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(np.int8))))
        else:
            # Publish good packet, without spy header
            self.update_cnt += 1
            ###DEBUG###
            if self.alias == "neural-agent":
                np.save("received_preamble_{}".format(pktidx), new_echo_s)
                np.save("received_echo_{}".format(pktidx), my_echo_s)
            else:
                np.save("received_classic_preamble_{}".format(pktidx),
                        new_echo_s)
                np.save("received_classic_echo_{}".format(pktidx), my_echo_s)
            ###DEBUG###
            self.message_port_pub(
                self.port_id_out,
                pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(np.int8))))

            self.ber = sum(
                self.preamble != new_echo) * 1.0 / self.preamble.size
            if self.update_cnt % self.log_ber_interval == 0:
                with open("ber_{}.csv".format(self.uuid_str), "a") as f:
                    f.write("{},{}\n".format(self.update_cnt, self.ber))
        t1 = time.time()
        self.logger.debug(
            "classic demod {} handled {} bits in {} seconds".format(
                self.uuid_str, bits.size, t1 - t0))
Пример #6
0
 def handle_demod_neural(self, pdu):
     t0 = time.time()
     self.demod_packet_cnt += 1
     tag_dict = pmt.car(pdu)
     vec = pmt.cdr(pdu)
     # tag_p = pmt.to_python(tag)
     data_c = pmt.to_python(vec)
     nbits = data_c.size * self.bits_per_symbol
     spy_s, hdr_s, new_echo_s, my_echo_s = self.split_packet_iq(data_c)
     spy_b = util_data.integers_to_bits(self.spy_demod.demodulate(spy_s),
                                        self.bits_per_symbol)
     hdr_b = util_data.integers_to_bits(self.spy_demod.demodulate(hdr_s),
                                        self.bits_per_symbol)
     exploration = 'exploit' if self.run_mode == 'freeze' else 'explore'
     new_echo_b = util_data.integers_to_bits(
         self.demod.demodulate(new_echo_s, mode=exploration),
         self.bits_per_symbol)
     my_echo_b = util_data.integers_to_bits(
         self.demod.demodulate(my_echo_s, mode=exploration),
         self.bits_per_symbol)
     bits = np.concatenate([spy_b, hdr_b, new_echo_b, my_echo_b])
     _, hdr, _, _ = self.split_packet_bits(bits)
     if hdr is not None:
         idxpre = hdr[0]
         idxecho = hdr[1]
         valid = hdr[2]
     else:
         valid = False
     # Check spy field for corruption
     if self.spy_length > 0:
         spy_ber = sum(spy_b != self.spy_master) * 1.0 / self.spy_length
     else:
         spy_ber = 0
     if spy_ber > self.spy_threshold:
         # BAD PACKET!
         self.logger.debug(
             "neural demod {} spy ber {} above threshold {}".format(
                 self.uuid_str, spy_ber, self.spy_threshold))
         # Publish to both ports so mods can decide how to handle corruptions
         self.message_port_pub(
             self.port_id_demod_out,
             pmt.cons(pmt.PMT_NIL, pmt.to_pmt(bits.astype(np.int8))))
     elif valid:
         # Good packet, demod and train with the neural agent
         self.demod_update_cnt += 1
         # update
         if self.run_mode == "train" and self.use_shared:
             self.demod_train_preamble(new_echo_s, idxpre)
         elif self.run_mode == "train" and not self.use_shared:
             self.demod_train_preamble(my_echo_s, idxecho)
         # demod
         #new_echo_b = util_data.integers_to_bits(self.demod.demodulate(new_echo_s, 'exploit'), self.bits_per_symbol)
         #my_echo_b = util_data.integers_to_bits(self.demod.demodulate(my_echo_s, 'exploit'), self.bits_per_symbol)
         #labels_g = np.concatenate([spy_b, hdr_b, new_echo_b, my_echo_b])
         labels_g = bits  # Match python simulation behavior
         # Sample some BERs during training, but sample all while frozen to collect average
         # behavior faster
         if self.demod_update_cnt % self.log_interval == 0 or self.run_mode == "freeze":
             ber = sum(
                 new_echo_b != self.preamble) * 1.0 / self.preamble.size
             with open("ber_{}.csv".format(self.uuid_str), "a") as f:
                 f.write("{},{}\n".format(self.demod_packet_cnt, ber))
         # publish
         self.message_port_pub(
             self.port_id_demod_out,
             pmt.cons(pmt.PMT_NIL,
                      pmt.to_pmt(labels_g.astype(dtype=np.int8))))
     t1 = time.time()
     self.logger.debug(
         "neural demod {} handled {} bits in {} seconds".format(
             self.uuid_str, nbits, t1 - t0))
     return bits