def sim_hop(): ber = 0.0001 chunk_size = 50 per = 1 - (1 - ber)**(chunk_size * 8) loop_num = 5 hop = range(1, 6) w1_size = 0.4 p1 = 0.4 for h in tqdm(hop): fountain = EW_Fountain(m, chunk_size=chunk_size, w1_size=w1_size, w1_pro=p1) for j in tqdm(range(loop_num)): drop_id = 0 w1_done_t = 0 glass = Glass(Fountain.num_chunks) while not glass.isDone(): a_drop = fountain.droplet() ew_drop = EW_Droplet(a_drop.data, a_drop.seed, a_drop.num_chunks, w1_size, w1_pro=p1) glass.addDroplet(ew_drop) if glass.is_w1_done() and w1_done_t == 0: w1_done_t = drop_id del a_drop del ew_drop
def add_a_drop(self, d_byte): a_drop = self.glass.droplet_from_Bytes(d_byte) print("++ seed: {}\tnum_chunk : {}\tdata len: {}+++++".format(a_drop.seed, a_drop.num_chunks, len(a_drop.data))) ew_drop = EW_Droplet(a_drop.data, a_drop.seed, a_drop.num_chunks) if self.glass.num_chunks == 0: print('init num_chunks : ', a_drop.num_chunks) self.glass = Glass(a_drop.num_chunks) self.chunk_size = len(a_drop.data) self.glass.addDroplet(ew_drop) self.recv_bit = self.glass.get_bits() # print('recv bits length : ', int(self.recv_bit.length())) if (int(self.recv_bit.length()) > 0) and \ (self.recv_bit.length() > self.current_recv_bits_len): self.current_recv_bits_len = self.recv_bit.length() self.i_spiht = self._123(self.recv_bit, self.chunk_size) try: self.i_dwt[0] = spiht_decode(self.i_spiht[0], self.eng) self.i_dwt[1] = spiht_decode(self.i_spiht[1], self.eng) self.i_dwt[2] = spiht_decode(self.i_spiht[2], self.eng) self.img_mat = [func_IDWT(ii) for ii in self.i_dwt] # 单通道处理 # self.idwt_coeff_r = spiht_decode(self.recv_bit, self.eng) # self.r_mat =func_IDWT(self.idwt_coeff_r) self.img_shape = self.img_mat[0].shape self.show_recv_img() except: print('decode error in matlab')
def sim_chunk_size(): test_dir = os.path.join(SIM_PATH, 'sim_chunk_size') if not os.path.exists(test_dir): os.mkdir(test_dir) test_res_file = os.path.join( test_dir, time.asctime().replace(' ', '_').replace(':', '_')) + '.csv' # chunk_sizees = [int(ii) for ii in np.logspace(1.7, 4.99, 20)] chunk_sizees = range(100, 5050, 50) # chunk_sizees.reverse() code_rate = [0] * len(chunk_sizees) drops_num = [0] * len(chunk_sizees) per_list = [0] * len(chunk_sizees) i = 0 loop_num = 100 BER = 0.00001 for size in tqdm(chunk_sizees): for j in tqdm(range(loop_num)): fountain = EW_Fountain(m, chunk_size=size, seed=255) glass = Glass(fountain.num_chunks) drop_id = 0 PER = 1 - (1 - BER)**(size * 8) while not glass.isDone(): drop_id += 1 a_drop = fountain.droplet() ew_drop = EW_Droplet(a_drop.data, a_drop.seed, a_drop.num_chunks) glass.addDroplet(ew_drop) code_rate[i] += size * drop_id / (1 - PER) drops_num[i] += drop_id per_list[i] = PER i += 1 code_rate = [float(ii) / (len(m) * loop_num) for ii in code_rate] drops_num = [float(ii) / loop_num for ii in drops_num] df_res = pd.DataFrame({ "size": chunk_sizees, "code_rate": code_rate, 'drops_num': drops_num, 'loop_num': [loop_num for ii in chunk_sizees], 'BER': [BER for ii in chunk_sizees], 'PER': per_list }) df_res.to_csv(test_res_file) return test_res_file
def add_a_drop(self, d_byte): drop = self.glass.droplet_from_Bytes(d_byte) # drop if self.glass.num_chunks == 0: print('init num_chunks : ', drop.num_chunks) self.glass = Glass(drop.num_chunks) # 初始化接收glass self.chunk_size = len(drop.data) #若为ew,则需要将drop转为ewdrop,两个drop里译码方式不一样 ew_drop = EW_Droplet(drop.data, drop.seed, drop.num_chunks, drop.process, drop.func_id, drop.feedback_idx) print('drop data len: ', len(drop.data)) self.glass.addDroplet(drop) # glass add drops logging.info('=============================') logging.info('Decode Progress: ' + str(self.glass.chunksDone()) + '/' + str(self.glass.num_chunks)) logging.info('=============================') # 接收完成 if self.glass.isDone(): self.recv_done_flag = True logging.info('============Recv done===========') # 接收完成写入图像 img_data = self.glass.get_bits() os.mkdir(self.recv_dir) with open(os.path.join(self.recv_dir, "img_recv" + ".jpg"), 'wb') as f: f.write(img_data) self.send_recv_done_ack() # 接收完成返回ack # 接收到K个数据包之后 if self.drop_id >= self.glass.num_chunks: if (self.drop_id - self.glass.num_chunks) % 10 == 0: self.chunk_process = self.glass.getProcess() # 用于返回进程包 self.glass.glass_process_history.append( self.chunk_process) # 添加反馈历史数据,用于droplet参数,正确译码 self.send_feedback() print("Feedback chunks: ", self.chunk_process) print("Feedback chunks num: ", len(self.chunk_process)) print("Feedback idx: ", self.feedback_idx) self.feedback_idx += 1
def main_test_ew_fountain(): m = open(os.path.join(DOC_PATH, 'fountain.txt'), 'r').read() fountain = EW_Fountain(m, chunk_size=10) glass = Glass(fountain.num_chunks) ew_drop = None i = 0 drop_size = 0 while not glass.isDone(): i += 1 a_drop = fountain.droplet() ew_drop = EW_Droplet(a_drop.data, a_drop.seed, a_drop.num_chunks) drop_size = len(ew_drop.data) glass.addDroplet(ew_drop) # sleep(1) logging.info('+++++++++++++++++++++++++++++') logging.info(glass.getString()) logging.info("data size : {}".format(len(m))) logging.info("send drop num : {} drop size : {}".format(i, drop_size)) logging.info("send data size : {}".format(i * drop_size)) logging.info("scale : {}".format((i * drop_size) / float(len(m)))) logging.info('done')
def add_a_drop(self, d_byte): self.drop_id += 1 print("====================") print("Recv dropid : ", self.drop_id) print("====================") print('glass_process_history len: ', len(self.glass.glass_process_history)) drop = self.glass.droplet_from_Bytes(d_byte) # drop if self.glass.num_chunks == 0: print('init num_chunks : ', drop.num_chunks) self.glass = Glass(drop.num_chunks) # 初始化接收glass self.chunk_size = len(drop.data) #若为ew,则需要将drop转为ewdrop,两个drop里译码方式不一样 ew_drop = EW_Droplet(drop.data, drop.seed, drop.num_chunks, drop.process, drop.func_id, drop.feedback_idx) print('drop data len: ', len(drop.data)) self.glass.addDroplet(ew_drop) # 这里解决度函数切换过程不接收,解决进度包更新过程不接收 # if (self.feedback_ack_flag==False) or (self.feedback_ack_flag==True and ew_drop.func_id==1 and ew_drop.feedback_idx==self.feedback_idx-1): # self.real_drop_id += 1 # self.glass.addDroplet(ew_drop) # glass add drops # print("=============================================================") # print("除去度函数切换和进度包更新时收到的drop,Real_Recv_dropid : ", self.real_drop_id) # print("=============================================================") # self.feedback_send_done=False logging.info('=============================') logging.info('Decode Progress: ' + str(self.glass.chunksDone()) + '/' + str(self.glass.num_chunks)) logging.info('=============================') # 接收完成 if self.glass.isDone(): self.recv_done_flag = True self.t1 = time.time() logging.info('============Recv done===========') logging.info("Fountain time elapsed:" + str(self.t1 - self.t0)) # 接收完成写入图像 img_data = self.glass.get_bits() os.mkdir(self.recv_dir) with open(os.path.join(self.recv_dir, "img_recv" + ".bmp"), 'wb') as f: f.write(img_data) # 串口模拟水声延迟进行反馈 t1 = threading.Timer(1.8, self.send_recv_done_ack) t1.start() # 实际写入水声通信机进行反馈 # self.send_recv_done_ack() # 接收完成返回ack # 记录吞吐量 self.cal_ttl() print('packet_id history: ', self.packid_save, len(self.packid_save)) print('packets_per_sec: ', self.packs_per_sec, len(self.packs_per_sec)) print('drop_id history: ', self.dropid_save, len(self.dropid_save)) logging.info('Send Sonic Fountain ACK done') logging.info('Recv Packets: : ' + str(self.packet_id)) logging.info('Recv drops: ' + str(self.drop_id)) logging.info('Feedback num: ' + str(self.feedback_idx)) print('drops_per_sec: ', self.drops_per_sec, len(self.drops_per_sec)) res = pd.DataFrame({ 'packet_id_history': self.packid_save, 'packets_per_sec': self.packs_per_sec, 'drop_id_history': self.dropid_save, 'drops_per_sec': self.drops_per_sec }) res.to_csv( ('data_save/Recv_ttl' + '_' + time.asctime().replace(' ', '_').replace(':', '_') + '.csv'), mode='a') # print('avgs_decode_time: ', float(sum(self.decode_time)/len(self.decode_time))) # print('max_decode_time:', max(self.decode_time)) # print('min_decode_time:', min(self.decode_time)) # 反馈 n1 = self.glass.w1_done_dropid n2 = 30 if self.glass.is_w1_done(0.6) and self.recv_done_flag == False: if (self.drop_id - n1) % n2 == 0: process = self.glass.getProcess() # 用于添加反馈历史数据, 用于droplet参数,正确译码 self.chunk_process = process[0] self.glass.glass_process_history.append(self.chunk_process) # 用于实际反馈 process_bitmap = process[1] # self.process_bytes = self.bit2hex(process_bitmap) # 实际水声通信机反馈可能需要用到这个 # print(process_bitmap) # print(self.process_bytes) process_bits = bitarray.bitarray(process_bitmap) self.process_bytes = process_bits.tobytes() # 串口模拟水声延迟进行反馈 t = threading.Timer(1.8, self.send_feedback) t.start() # 实际写入水声通信机进行反馈 # self.send_feedback() print("Feedback chunks: ", self.chunk_process) print("Feedback chunks num: ", len(self.chunk_process)) print("Feedback idx: ", self.feedback_idx) self.feedback_idx += 1 self.feedback_ack_flag = True self.feedback_send_done = True
def add_a_drop(self, d_byte): self.rx_drop_id += 1 print("====================") print("Recv dropid : ", self.rx_drop_id) print("====================") print('glass_process_history len: ', len(self.glass.glass_process_history)) drop = self.glass.droplet_from_Bytes(d_byte) # drop if self.glass.num_chunks == 0: print('init num_chunks : ', drop.num_chunks) self.glass = Glass(drop.num_chunks) # 初始化接收glass self.chunk_size = len(drop.data) #若为ew,则需要将drop转为ewdrop,两个drop里译码方式不一样 ew_drop = EW_Droplet(drop.data, drop.seed, drop.num_chunks, drop.process, drop.func_id, drop.feedback_idx) print('drop data len: ', len(drop.data)) self.glass.addDroplet(ew_drop) logging.info('=============================') logging.info('Decode Progress: ' + str(self.glass.chunksDone()) + '/' + str(self.glass.num_chunks)) logging.info('=============================') # 接收完成 if self.glass.isDone(): self.recv_done_flag = True self.t1 = time.time() logging.info('============Recv done===========') logging.info("Fountain time elapsed:" + str(self.t1 - self.t0)) # 接收完成写入图像 img_data = self.glass.get_bits() self.img_recv_bits = img_data os.mkdir(self.recv_dir) with open(os.path.join(self.recv_dir, "img_recv" + ".bmp"), 'wb') as f: f.write(img_data) self.send_recv_done_ack() # 接收完成返回ack # 记录吞吐量 self.rx_cal_ttl() logging.info('Recv drops: ' + str(self.rx_drop_id)) logging.info('Feedback num: ' + str(self.feedback_idx)) print('drops_per_sec: ', self.rx_drops_per_sec, len(self.rx_drops_per_sec)) res = pd.DataFrame({ 'packet_id_history': self.rx_packid_save, 'packets_per_sec': self.rx_packs_per_sec, 'drop_id_history': self.rx_dropid_save, 'drops_per_sec': self.rx_drops_per_sec }) res.to_csv( ('data_save/Recv_ttl' + '_' + time.asctime().replace(' ', '_').replace(':', '_') + '.csv'), mode='a') # 反馈 n1 = self.glass.w1_done_dropid n2 = 30 if self.glass.is_w1_done(0.6) and self.recv_done_flag == False: if (self.rx_drop_id - n1) % n2 == 0: process = self.glass.getProcess() # 用于添加反馈历史数据, 用于droplet参数,正确译码 self.chunk_process = process[0] self.glass.glass_process_history.append(self.chunk_process) # 用于实际反馈 process_bitmap = process[1] # self.process_bytes = self.bit2hex(process_bitmap) # print(process_bitmap) # print(self.process_bytes) process_bits = bitarray.bitarray(process_bitmap) self.process_bytes = process_bits.tobytes() self.send_feedback() print("Feedback chunks: ", self.chunk_process) print("Feedback chunks num: ", len(self.chunk_process)) print("Feedback idx: ", self.feedback_idx) self.feedback_idx += 1 self.feedback_ack_flag = True
def add_a_drop(self, d_byte): a_drop = self.glass.droplet_from_Bytes(d_byte) print("seed: {}\tnum_chunk : {}\tdata len: {}".format( a_drop.seed, a_drop.num_chunks, len(a_drop.data))) ew_drop = EW_Droplet(a_drop.data, a_drop.seed, a_drop.num_chunks) if self.glass.num_chunks == 0: self.wlt_ds = time.time() print('init num_chunks : ', a_drop.num_chunks) self.glass = Glass(a_drop.num_chunks) self.chunk_size = len(a_drop.data) self.glass.addDroplet(ew_drop) logging.info('current chunks') logging.info( [ii if ii == None else '++++' for ii in self.glass.chunks]) # 解码w1信息 w1_size = 0.1 if self.glass.is_w1_done(w1_size) and self.w1_done: print('w1_data complete!') w1_bits = self.glass.get_w1_bits( int(round(self.glass.num_chunks * w1_size))) self.w1_done = False if (int(w1_bits.length()) > 0) and \ (w1_bits.length() > self.current_recv_bits_len): self.current_recv_bits_len = w1_bits.length() self.i_spiht = self.w1_123(w1_bits) try: self.i_dwt[0] = spiht_decode(self.i_spiht[0], self.eng) self.i_dwt[1] = spiht_decode(self.i_spiht[1], self.eng) self.i_dwt[2] = spiht_decode(self.i_spiht[2], self.eng) self.img_mat = [func_IDWT(ii) for ii in self.i_dwt] # 单通道处理 # self.idwt_coeff_r = spiht_decode(self.recv_bit, self.eng) # self.r_mat =func_IDWT(self.idwt_coeff_r) self.img_shape = self.img_mat[0].shape self.show_recv_img() except: print('decode error in matlab') # 解码全部信息 if self.glass.isDone(): # 喷泉码接收译码完成 wlt_de = time.time() print('Window LT decode time used:', wlt_de - self.wlt_ds) self.recv_bit = self.glass.get_bits() # print('recv bits length : ', int(self.recv_bit.length())) if (int(self.recv_bit.length()) > 0) and \ (self.recv_bit.length() > self.current_recv_bits_len): self.current_recv_bits_len = self.recv_bit.length() self.i_spiht = self._123(self.recv_bit) try: spihtstart = time.time() self.i_dwt[0] = spiht_decode(self.i_spiht[0], self.eng) self.i_dwt[1] = spiht_decode(self.i_spiht[1], self.eng) self.i_dwt[2] = spiht_decode(self.i_spiht[2], self.eng) self.img_mat = [func_IDWT(ii) for ii in self.i_dwt] spihtend = time.time() print('SPIHT decode time:', spihtend - spihtstart) # 单通道处理 # self.idwt_coeff_r = spiht_decode(self.recv_bit, self.eng) # self.r_mat =func_IDWT(self.idwt_coeff_r) self.img_shape = self.img_mat[0].shape self.show_recv_img() self.recv_done_flag = True self.send_recv_done_ack() except: print('decode error in matlab')
def add_a_drop(self, d_byte): self.drop_id += 1 print("====================") print("Recv dropid : ", self.drop_id) print("====================") drop = self.glass.droplet_from_Bytes(d_byte) # drop if self.glass.num_chunks == 0: print('init num_chunks : ', drop.num_chunks) self.glass = Glass(drop.num_chunks) # 初始化接收glass self.chunk_size = len(drop.data) #若为ew,则需要将drop转为ewdrop,两个drop里译码方式不一样 ew_drop = EW_Droplet(drop.data, drop.seed, drop.num_chunks, drop.process, drop.func_id, drop.feedback_idx) print('drop data len: ', len(drop.data)) self.glass.addDroplet(ew_drop) # 这里解决度函数切换过程不接收,解决进度包更新过程不接收 # if (self.feedback_ack_flag==False) or (self.feedback_ack_flag==True and ew_drop.func_id==1 and ew_drop.feedback_idx==self.feedback_idx-1): # self.real_drop_id += 1 # self.glass.addDroplet(ew_drop) # glass add drops # print("=============================================================") # print("除去度函数切换和进度包更新时收到的drop,Real_Recv_dropid : ", self.real_drop_id) # print("=============================================================") # self.feedback_send_done=False logging.info('=============================') logging.info('Decode Progress: '+str(self.glass.chunksDone())+'/'+str(self.glass.num_chunks)) logging.info('=============================') # 接收完成 if self.glass.isDone(): self.recv_done_flag = True self.t1 = time.time() logging.info('============Recv done===========') logging.info("Sonic Feedback Fountain time elapsed:"+ str(self.t1-self.t0)) # 接收完成写入图像 img_data = self.glass.get_bits() os.mkdir(self.recv_dir) with open(os.path.join(self.recv_dir, "img_recv" + ".bmp"), 'wb') as f: f.write(img_data) self.send_recv_done_ack() # 接收完成返回ack # 记录吞吐量 self.cal_ttl() # print('packet_id history: ', self.packid_save, len(self.packid_save)) print('packets_per_sec: ', self.packs_per_sec, len(self.packs_per_sec)) # print('drop_id history: ', self.dropid_save, len(self.dropid_save)) print('drops_per_sec: ', self.drops_per_sec, len(self.drops_per_sec)) res = pd.DataFrame({'packet_id_history':self.packid_save, 'packets_per_sec':self.packs_per_sec, 'drop_id_history':self.dropid_save, 'drops_per_sec':self.drops_per_sec}) res.to_csv(('data_save/Recv_ttl'+ '_' + time.asctime().replace(' ', '_').replace(':', '_') + '.csv'), mode='a') print('........................................................................') print('INFO: srcID=0, selfID=1, desID=2, starting Relay Forwarding procedure...') print('INFO: Send acoustic broadcast message done ! ') print('INFO: acoustic ACK Received !') print('INFO: ...Starting auto align...')
def sim_ber(): test_dir = os.path.join(SIM_PATH, 'sim_ber') if not os.path.exists(test_dir): os.mkdir(test_dir) chunk_size = 40 loop_num = 5 chunk_num = ceil(len(m) / float(chunk_size)) p1 = 0.4 ber = np.arange(0.0001, 0.015, 0.001) code_rate = [0] * len(ber) w1_size = 0.4 w1_done = [0] * len(ber) w2_done = [0] * len(ber) per_list = [0] * len(ber) raw_data_size = len(m) i = 0 for ber_ in tqdm(ber): print(heap) fountain = EW_Fountain(m, chunk_size=chunk_size, w1_size=w1_size, w1_pro=p1) for j in tqdm(range(loop_num)): glass = Glass(fountain.num_chunks) drop_id = 0 PER = 1 - (1 - ber_)**(chunk_size * 8) w1_done_t = 0 while (not glass.isDone()): # print(drop_id * float(chunk_size) / len(m)) drop_id += 1 a_drop = fountain.droplet() ew_drop = EW_Droplet(a_drop.data, a_drop.seed, a_drop.num_chunks, w1_size, w1_pro=p1) glass.addDroplet(ew_drop) if glass.is_w1_done(w1_size) and w1_done_t == 0: w1_done_t = drop_id del a_drop del ew_drop if (drop_id * chunk_size / raw_data_size > 50): print("_") code_rate[i] += chunk_size * drop_id / (1 - PER) w1_done[i] += w1_done_t * chunk_size / (1 - PER) w2_done[i] += drop_id * chunk_size / (1 - PER) per_list[i] = PER del glass del fountain i += 1 code_rate = [float(ii) / (len(m) * loop_num) for ii in code_rate] w1_done = [float(ii) / (len(m) * loop_num) for ii in w1_done] w2_done = [float(ii) / (len(m) * loop_num) for ii in w2_done] df_res = pd.DataFrame({ "ber": ber, "code_rate": code_rate, 'w1_done': w1_done, 'w2_done': w2_done, 'per': [1 - (1 - ii)**(8 * chunk_size) for ii in ber], 'loop_num': [loop_num for ii in ber], 'chunk_size': [chunk_size for ii in ber], 'p1': [p1 for ii in ber], 'chunk_num': [chunk_num for ii in ber], 'w1_size': [w1_size for ii in ber] }) res_file = os.path.join( test_dir, time.asctime().replace(' ', '_').replace( ':', '_')) + "_" + str(p1) + "_" + str(chunk_size) + '.csv' df_res.to_csv(res_file) print(df_res) return res_file
def sim_diff_psnr(): test_dir = os.path.join(SIM_PATH, 'sim_diff_psnr') if not os.path.exists(test_dir): os.mkdir(test_dir) res_file = os.path.join(test_dir, time.asctime().replace(' ', '_').replace( ':', '_')) + '.csv' need_lest = 79 packet_ceil = 631 deli_rate = pkl.load(open(os.path.join(SIM_PATH, 'deil.pkl'), 'rb')) loop_num = 1000 m = ' ' * need_lest p1_list = list(np.arange(0.1, 0.9, 0.1)) w1_list = list(np.arange(0.1, 0.9, 0.1)) res_size = len(p1_list) * len(w1_list) res_w1 = [0] * res_size for i in range(res_size): res_w1[i] = w1_list[int(i / len(p1_list))] res = pd.DataFrame({ "w1": res_w1, 'p1': p1_list * len(w1_list), # 'w1_done' : [0] * res_size, # "all_done": [0] * res_size }) w1_done_tmp = [0] * res_size all_done_tmp = [0] * res_size i = 0 for w1 in tqdm(w1_list): j = 0 for p1 in tqdm(p1_list): all_done = 0 res_index = i * len(p1_list) + j for loop in tqdm(range(loop_num)): fountain = EW_Fountain(m, chunk_size=1, w1_size=w1, w1_pro=p1) glass = Glass(fountain.num_chunks) w1_done = 0 for drop_id in range(packet_ceil): a_drop = fountain.droplet() ew_drop = EW_Droplet(a_drop.data, a_drop.seed, a_drop.num_chunks, w1, p1) glass.addDroplet(ew_drop) if glass.is_w1_done(w1) and w1_done == 0: w1_done_tmp[res_index] += drop_id w1_done = 1 # print("w1 done at {}".format(drop_id)) elif glass.isDone(): # print("all done at {}".format(drop_id)) all_done_tmp[res_index] += drop_id break w1_done_tmp[res_index] = w1_done_tmp[res_index] / float(loop_num) all_done_tmp[res_index] = all_done_tmp[res_index] / float(loop_num) j += 1 i += 1 res['w1_done'] = w1_done_tmp res['all_done'] = all_done_tmp res = res[['w1', 'p1', 'w1_done', 'all_done']] print(res) res.to_csv(res_file)
def sim_p1(): test_dir = os.path.join(SIM_PATH, 'sim_p1') res_file = os.path.join(test_dir, time.asctime().replace(' ', '_').replace( ':', '_')) + '.csv' step = 0.05 floor = 0.3 ceiling = 0.7 p1_list = list(np.arange(floor, ceiling, step)) p1_list.reverse() code_rate = [0] * len(p1_list) w1_size = 0.5 w1_done = [0] * len(p1_list) w2_done = [0] * len(p1_list) per_list = [0] * len(p1_list) chunk_size = 255 loop_num = 50 chunk_num = ceil(len(m) / float(chunk_size)) BER = 0.0001 i = 0 for p1 in tqdm(p1_list): for j in tqdm(range(loop_num)): fountain = EW_Fountain(m, chunk_size=chunk_size, w1_size=w1_size, w1_pro=p1) glass = Glass(fountain.num_chunks) drop_id = 0 PER = 1 - (1 - BER)**(chunk_size * 8) w1_done_t = 0 while not glass.isDone(): drop_id += 1 a_drop = fountain.droplet() ew_drop = EW_Droplet(a_drop.data, a_drop.seed, a_drop.num_chunks, w1_size, w1_pro=p1) glass.addDroplet(ew_drop) if glass.is_w1_done(w1_size) and w1_done_t == 0: w1_done_t = drop_id del a_drop del ew_drop code_rate[i] += chunk_size * drop_id / (1 - PER) w1_done[i] += w1_done_t * chunk_size / (1 - PER) w2_done[i] += drop_id * chunk_size / (1 - PER) per_list[i] = PER del fountain del glass i += 1 code_rate = [float(ii) / (len(m) * loop_num) for ii in code_rate] w1_done = [float(ii) / (len(m) * loop_num) for ii in w1_done] w2_done = [float(ii) / (len(m) * loop_num) for ii in w2_done] df_res = pd.DataFrame({ "p1": p1_list, "code_rate": code_rate, 'w1_done': w1_done, 'w2_done': w2_done, 'per': per_list, 'loop_num': [loop_num for ii in p1_list], 'chunk_size': [chunk_size for ii in p1_list], 'chunk_num': [chunk_num for ii in p1_list], 'w1_size': [w1_size for ii in p1_list] }) df_res.to_csv(res_file) print(df_res) return res_file
def add_a_drop(self, d_byte): self.drop_id += 1 print("====================") print("Recv drop_id : ", self.drop_id) print("====================") drop = self.glass.droplet_from_Bytes(d_byte) # drop if self.glass.num_chunks == 0: self.glass = Glass(drop.num_chunks) # 初始化接收glass self.chunk_size = len(drop.data) #若为ew,则需要将drop转为ewdrop,两个drop里译码方式不一样 ew_drop = EW_Droplet(drop.data, drop.seed, drop.num_chunks, drop.process, drop.func_id, drop.feedback_idx) self.glass.addDroplet(ew_drop) # glass add drops logging.info('=============================') logging.info('Decode Progress: ' + str(self.glass.chunksDone()) + '/' + str(self.glass.num_chunks)) logging.info('=============================') # 接收完成 if self.glass.isDone(): self.t1 = time.time() self.recv_done_flag = True # # 接收完成写入图像 # img_data = self.glass.get_bits() # os.mkdir(self.recv_dir) # with open(os.path.join(self.recv_dir, "img_recv" + ".bmp"), 'wb') as f: # f.write(img_data) t1 = threading.Timer(2.6, self.send_recv_done_ack) t1.start() # self.send_recv_done_ack() # 接收完成返回ack logging.info('============Recv done===========') logging.info('Send Sonic Fountain ACK done') logging.info('Recv packets: ' + str(self.drop_id)) logging.info('Recv drops: ' + str(self.drop_id)) logging.info("Sonic Feedback Fountain time elapsed:" + str(self.t1 - self.t0)) # 接收到K个数据包之后 n1 = round(0.8 * self.glass.num_chunks) n2 = 30 if self.drop_id >= n1 and self.recv_done_flag == False: if (self.drop_id - n1) % n2 == 0: process = self.glass.getProcess() # 用于添加反馈历史数据, 用于droplet参数,正确译码 self.chunk_process = process[0] self.glass.glass_process_history.append(self.chunk_process) # 用于实际反馈 process_bitmap = process[1] process_bits = bitarray.bitarray(process_bitmap) self.process_bytes = process_bits.tobytes() t = threading.Timer(2.6, self.send_feedback) t.start() print(self.process_bytes) print("Feedback chunks: ", self.chunk_process) print("Feedback chunks num: ", len(self.chunk_process)) print("Feedback idx: ", self.feedback_idx) self.feedback_idx += 1