def encode(plain_string, base=3): bit_string = ''.join( [format(c, 'b').zfill(8) for c in plain_string.encode('utf8')]) # split bit_string into chunks the length of base chunks = [bit_string[::-1][i:i + 8] for i in range(0, len(bit_string), 8)] # reverse list of chunks AND every chunk itself. chunks = [chunk[::-1].zfill(base) for chunk in chunks[::-1]] result = [] tmp = [] # CRTCode Object is used for error correction code = CRC.CRTCode([1, 2, 3, 5, 7]) # encode each chunk with error correction for chunk in chunks: # Encode with error correction m = CRC.Message(m_binary=chunk) code.encode(m) tmp.extend(code.getVals()) # split bit_string into chunks the length of 3 for x in tmp: result.append(format(x, '0' + str(base) + 'b')) assert len(result) == len(tmp) return result
def findPossibleMessages(): segmentLength = 0.2 #Seconds f = open("data.bin", "r") Input = np.fromfile(f, dtype=np.int16) Input = Input[10**4:] messageList=[] for segment in range(0,numSeconds/segmentLength): print(segment) InputSegment=Input[2*segmentLength*Fs*segment:2*segmentLength*Fs*(segment+1)] #Strip out the I and Q I = InputSegment[0::2].astype(float) Q = InputSegment[1::2].astype(float) m = np.sqrt(I**2+Q**2) messages = PreambleDetection.findPreambles(m,threshold,'8D') for messageStart in messages: Time = (acquisitionTime-startTime)+(segment*segmentLength)+messageStart/Fs bitString,confidenceArray = decodeMessage(m,messageStart) hexString = CRC.bin2hex(bitString) if hexString[0:2] == '8D': messageList.append((Time,hexString)) return(messageList)
def findPossibleMessages(): segmentLength = 0.2 #Seconds f = open("data.bin", "r") Input = np.fromfile(f, dtype=np.int16) Input = Input[10**4:] messageList = [] for segment in range(0, numSeconds / segmentLength): print(segment) InputSegment = Input[2 * segmentLength * Fs * segment:2 * segmentLength * Fs * (segment + 1)] #Strip out the I and Q I = InputSegment[0::2].astype(float) Q = InputSegment[1::2].astype(float) m = np.sqrt(I**2 + Q**2) messages = PreambleDetection.findPreambles(m, threshold, '8D') for messageStart in messages: Time = (acquisitionTime - startTime) + (segment * segmentLength) + messageStart / Fs bitString, confidenceArray = decodeMessage(m, messageStart) hexString = CRC.bin2hex(bitString) if hexString[0:2] == '8D': messageList.append((Time, hexString)) return (messageList)
def polarcodes(n, rate, snr, channel_con, decode_method, decode_para, information_pos, frozen_bit, crc_n): N = 2**n information_num = int(N * rate) # 信息-information_bit生成 information_bit = np.random.randint(0, 2, size=(1, information_num)) if crc_n == 0: pass else: informationbit = information_bit.copy() informationbit.resize(information_num, ) information_bit_list = list(informationbit) crc_info = CRC.CRC(information_bit_list, crc_n) crc_information_bit = crc_info.code crc_information_bit = np.array(crc_information_bit) crc_information_bit.resize(1, information_num + crc_n) # 编码前序列-u生成 u = np.ones((1, N)) * frozen_bit j = 0 #print(u.size) #print(information_bit.size) if crc_n == 0: for i in information_pos: u[0][i] = information_bit[0][j] j += 1 else: for i in information_pos: u[0][i] = crc_information_bit[0][j] j += 1 #print(information_pos) # 生成矩阵-G生成 G = function.generate_matrix(n) # 编码比特-x生成 x = u * G x = np.array(x % 2) # 经过信道生成y y = function.channel(x, channel_con, snr, rate) # y进入译码器生成u_d u_d = decoder.decoder(y, decode_method, decode_para, information_pos, frozen_bit, channel_con, snr, rate, crc_n) #print(u_d) # 计算错误数 information_pos = information_pos[0:information_num] information_bit_d = u_d[information_pos] error_num = int(np.sum((information_bit_d + information_bit) % 2)) if error_num != 0: decode_fail = 1 else: decode_fail = 0 r_value = np.array([error_num, decode_fail]) return r_value
def scf_decoder(y_llr, information_pos, frozen_bit,decode_para,crc_n): # 第一次用SC译码的过程 N = y_llr.size n = int(np.log2(N)) u_d_1_list = sc_decoder(y_llr,information_pos,frozen_bit) u_d_1_llr=u_d_1_list[1].copy() u_d_1=u_d_1_list[0].copy() u_d_1 = np.array([0 if u_d_1[i] == 0 else 1 for i in range(u_d_1.size)]) u_d_1_info=u_d_1[information_pos] u_d_1_llr_info=u_d_1_llr[information_pos] #print(u_d_1_llr_info) u_d_1_info=list(u_d_1_info) #print(u_d_1) crc_c = CRC.CRC(u_d_1_info, crc_n) flag = crc_c.detection() #print(flag) flip_info_pos1 = list(np.argsort(np.abs(u_d_1_llr_info))) flip_info_pos = flip_info_pos1[0:decode_para[0]] information_pos_array=np.array(information_pos) flip_pos=information_pos_array[flip_info_pos] #print(flip_pos) if flag == 0: T=1 while T <= decode_para[0]: #print(T) u_d_2_list=sc_flip1_decoder(y_llr,information_pos,frozen_bit,flip_pos[T-1]) u_d_2_llr = u_d_2_list[1].copy() u_d_2 = u_d_2_list[0].copy() u_d_2 = np.array([0 if u_d_2[i] == 0 else 1 for i in range(u_d_2.size)]) u_d_2_info=u_d_2[information_pos] u_d_2_info=list(u_d_2_info) crc_c = CRC.CRC(u_d_2_info, crc_n) flag1 = crc_c.detection() if flag1 == 1: u_d = np.array(u_d_2) #print('The scf decoder flip: ',T,' times and it works.') break else: T += 1 u_d = np.array(u_d_1) else: #print('no flip and success') u_d=np.array(u_d_1) return u_d
def process_data(self, Data): data = "" for x in Data: crc = CRC.CRC(x, 4) for y in crc.code: if y == 1: data += "1" else: data += "0" return data
def packet(data, seq, ack_field): ## chunk, seq, '00' flag = "7E" seq_string = str(seq) print("Frame num: ", seq_string) print("Data: ", data) crc = CRC.calc_checksum(data) print( "Checksum (frame) ", seq_string, ": ", crc ) # when the server does a checksum on this chunk, should yield same result - send ack data = str(data) header = flag + "-" + seq_string + "-" + ack_field trailer = crc + "-" + flag packet = header + "-" + data + "-" + trailer return str(packet)
def newFrame(buffer, next_frame_to_send, nBuffered, frame_expected, MAX_SEQ): from_network_layer(buffer, next_frame_to_send, nBuffered) # 帧属性 info = buffer[next_frame_to_send] # 待发送的信息 seq = next_frame_to_send # 序列号 ack = (frame_expected + MAX_SEQ) % (MAX_SEQ + 1) # ACK号 print("Cur Seq: ", seq) print("Cur Ack: ", ack) crc = CRC.generateCRC(info) # 生成校验和 data = crc.copy() # 整合后的数据 data.insert(0, ack) data.insert(0, seq) return data
def encoder(bin_path, out_path, time_lim, width=960, highth=960): time_lim = int(time_lim) fra = 10 pixel_size = 20 bin_in = dec2bin(bin_path) x = len(bin_in) row_num = int(highth / pixel_size) - 1 col_num = int(width / pixel_size / 8) y = int(x // (row_num * col_num)) # 生成的图片数 if (y > time_lim * fra): y = time_lim * fra for i in range(y): m = i * row_num * col_num n = (i + 1) * row_num * col_num bin_slice = bin_in[m:n] list_CRC = for_CRC(bin_slice) crc = CRC.CRC(list_CRC, 32) #n=crc.code_list #m=CRC.check(n) #print(m) c = form_CRC(crc) img = np.zeros((highth + 12 * pixel_size, width + 12 * pixel_size), dtype=np.uint8) for j in range(row_num): img[j * pixel_size + 120:(j + 1) * pixel_size + 120, 120:width + 120] = made_row( bin_slice[j * col_num:(j + 1) * col_num], pixel_size, width) img[1060:1080, 120:1080] = c img[100:1100, 100:120] = 255 img[100:1100, 1080:1100] = 255 img[1080:1100, 100:1100] = 255 img[100:120, 100:1100] = 255 im = Image.fromarray(img) im.save(str(i) + ".png") ff = FFmpeg(inputs={'': '-f image2 -r ' + str(fra) + ' -i %d.png'}, outputs={out_path: '-vcodec mpeg4'}) print(ff.cmd) ff.run() return y
if ("101010101010" in data) == False: # not corrupted isValid = True # valid for the moment - not corrupted print"Expected sequence number is ", correct_seq, ", got sequence number ", int(ack_seq) # -- start error-checking the data -- # if correct_seq != int(ack_seq): # error has occurred - send a nack to request retransmission isValid = False print"Incorrect sequence number - the original packet was dropped" dropped = True nack_packet = packet.packet(bytes(payload), correct_seq, "01") connectionSocket.send(bytes(nack_packet)) # NACK else: # we have the correct sequence num - continue error-checking print"Correct sequence number" # print"CRC received: ", crc_recvd chk = CRC.calc_checksum(bytes(payload)) if chk != crc_recvd: isValid = False # request a resend print"CRC fields do not match" else: isValid = True print"CRC correct" else: # packet corrupt print"Corrupt packet!" isValid = False if isValid == True: # no errors - ACK frame ack_packet = packet.packet(bytes(payload), correct_seq, '10') connectionSocket.send(bytes(ack_packet)) print"Sent the ack frame (",ack_packet ," ) for seq ", correct_seq, " back to the client"
t1 = Time if msg0 != '' and msg1 != '': if abs(t0 - t1) < 10: decodedPositions = decoder.get_position(msg0, msg1, t0, t1) if decodedPositions != None and len(decodedPositions) > 0: points.append(decodedPositions) return (np.asarray(points)) messageCount = 0 print('Generating plane dict') f = open('Syd.txt', 'r') planeDict = {} for line in f: Time, Message = line[:-1].split(' ') isvalid = CRC.computeChecksum(Message) addr = decoder.get_icao_addr(Message) tc = decoder.get_tc(Message) if tc >= 9 and tc <= 18: rawmsg = Message if isvalid == False: isvalid, Message = CRC.correctBitError(rawmsg) ''' if isvalid==False: isvalid,Message = CRC.correct2BitError(rawmsg) ''' if isvalid == True: messageCount += 1 addr = decoder.get_icao_addr(Message) if addr not in planeDict.keys():
counter += 1 if (counter == wielkoscPakietu): # pakiet po 8 bitow packets.append(packet) # tworzy liste pakietow packet = [] counter = 0 if ( len(packet) > 0 ): # mozliwe tworzenie pakietow wiekszych niz 8 bitow. po prostu ostatni pakiet, bedzie malutki packets.append(packet) # DODANIE BITU PARZYSTOSCI DO KAZDEGO Z PAKIETOW tmr = TMR() hamming = Hamming() crc = CRC() parity = ParityBit() channel = Channel(prawdopodobienstwoBSC, channelS0, channelS1, channelP01, channelP10) # print(packets) packetsWithParityBit = [] for pack in packets: pack = tmr.codeTMR(pack) # DODANIE TMR pack = parity.addParityBit(pack) # DODANIE PARITY BIT # pack = hamming.codeHamming(pack) # DODANIE Hamminga # pack = crc.addCRC(pack) # DODANIE CRC packetsWithParityBit.append(pack) # print(packetsWithParityBit)
event = event.decode("utf-8") event = [int(x) for x in list(event)] # 类型转换 event = delHeadTail(event) # 去掉头尾 curSeq = event[0] curACK = event[1] curInfo = event[2:] print("Frame expected:", frame_expected) print("Got Seq: ", curSeq) print("Got Ack: ", curACK) print('{:10}\t{}'.format("Receive frame:", event)) if frameLost == False: # 验证CRC if CRC.verifyCRC(curInfo.copy()) == False: # CRC 错误 print("Frame CRC error") else: # CRC 正确 if curSeq == frame_expected: # 接收到对应的帧 frame_expected = inc(frame_expected) while between(ack_expected, curACK, next_frame_to_send): nBuffered -= 1 timer[ack_expected] = 0 # 停止已接收帧的计时 ack_expected = inc(ack_expected) print("Timer:", t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7]) cnt += 1 # 循环结束 if cnt == 21:
def bp_decoder(y_llr,information_pos,frozen_bit,decode_para,crc_n): N = y_llr.size n = int(np.log2(N)) if decode_para[1] == 'max_iter': bp_max_iter=int(decode_para[0]) #初始化左传播和右传播矩阵 left_matrix=np.zeros([N,n+1]) right_matrix=np.zeros([N,n+1]) left_matrix[:,n]=y_llr temp_value=(1-2*frozen_bit)*np.infty temp=[temp_value if i not in information_pos else 0 for i in range(N)] right_matrix[:,0]=temp for iter in range(bp_max_iter): for i in range(n): left_matrix[:,n-i-1]=function.bp_update_left(left_matrix[:,n-i],right_matrix[:,n-i-1],n-i) for i in range(n): right_matrix[:,i+1]=function.bp_update_right(left_matrix[:,i+1],right_matrix[:,i],i+1) u_d_llr=left_matrix[:,0]+right_matrix[:,0] u_d=[0 if u_d_llr[i] >= 0 else 1 for i in range(N)] u_d=np.array(u_d) elif decode_para[1] == 'g_matrix': # 初始化左传播和右传播矩阵 left_matrix = np.zeros([N, n + 1]) right_matrix = np.zeros([N, n + 1]) left_matrix[:, n] = y_llr temp_value = (1 - 2 * frozen_bit) * np.infty temp = [temp_value if i not in information_pos else 0 for i in range(N)] right_matrix[:, 0] = temp flag=0 iter_num=1 while flag == 0 and iter_num <= decode_para[0]: for i in range(n): left_matrix[:, n - i - 1] = function.bp_update_left(left_matrix[:, n - i], right_matrix[:, n - i - 1],n - i) for i in range(n): right_matrix[:, i + 1] = function.bp_update_right(left_matrix[:, i + 1], right_matrix[:, i], i + 1) u_d_llr = left_matrix[:, 0] + right_matrix[:, 0] u_d = [0 if u_d_llr[i] >= 0 else 1 for i in range(N)] x_d_llr = left_matrix[:, n] + right_matrix[:, n] x_d = [0 if x_d_llr[i] >= 0 else 1 for i in range(N)] G = function.generate_matrix(n) x_g = u_d * G x_g = np.array(x_g % 2) #print(x_g) cor = [0 if x_g[0][i]==x_d[i] else 1 for i in range(N)] if sum(cor) == 0: flag = 1 else: flag = 0 iter_num += 1 u_d = np.array(u_d) elif decode_para[1] == 'crc_es': # 初始化左传播和右传播矩阵 left_matrix = np.zeros([N, n + 1]) right_matrix = np.zeros([N, n + 1]) left_matrix[:, n] = y_llr temp_value = (1 - 2 * frozen_bit) * np.infty temp = [temp_value if i not in information_pos else 0 for i in range(N)] right_matrix[:, 0] = temp flag = 0 iter_num = 1 while flag == 0 and iter_num <= decode_para[0]: for i in range(n): left_matrix[:, n - i - 1] = function.bp_update_left(left_matrix[:, n - i], right_matrix[:, n - i - 1],n - i) for i in range(n): right_matrix[:, i + 1] = function.bp_update_right(left_matrix[:, i + 1], right_matrix[:, i], i + 1) u_d_llr = left_matrix[:, 0] + right_matrix[:, 0] u_d = np.array([0 if u_d_llr[i] >= 0 else 1 for i in range(N)]) u_d_info = u_d[information_pos] u_d_info = list(u_d_info) crc_c=CRC.CRC(u_d_info,crc_n) flag=crc_c.detection() # if flag == 1: # print('It work!',iter_num) iter_num += 1 u_d = np.array(u_d) else: print('This is not a early stopping method of BP decoder or s have not added it to the endecoder.bp_decoder !') quit() return u_d
def scl_decoder(y_llr, information_pos, frozen_bit, decode_para, crc_n): N = y_llr.size n = int(np.log2(N)) llr_matrix = np.ones((n + 1, N)) llr_matrix[llr_matrix == 1] = float('nan') bit_matrix = llr_matrix.copy() llr_matrix[0] = y_llr list_max= decode_para[0] pm_method=decode_para[1] split_pos=information_pos #采用传统的方法,在所有信息位分裂,未裁剪和减复杂度 llr_list = [llr_matrix] bit_list = [bit_matrix] #print(llr_list) #print(bit_list) pm_list = [0] split_loc=0 split_len=len(split_pos) l_now=1 while split_len-1 >= split_loc: for i in range(l_now): llr_matrix_temp = llr_list[i] bit_matrix_temp = bit_list[i] pm_temp = pm_list[i] matrix_temp = sc_stepping_decoder(llr_matrix_temp, bit_matrix_temp, information_pos, frozen_bit, split_pos[split_loc]) llr_list[i] = matrix_temp[0] bit_list[i] = matrix_temp[1] right_pm_update = function.get_pm_update( matrix_temp[0][n][split_pos[split_loc - 1] + 1:split_pos[split_loc] + 1], matrix_temp[1][n][split_pos[split_loc - 1] + 1:split_pos[split_loc] + 1], pm_method) pm_list[i] = pm_temp + right_pm_update llr_list.append(matrix_temp[0].copy()) bit_matrix_wrong = matrix_temp[1].copy() bit_matrix_wrong[n][split_pos[split_loc]] = 1 - bit_matrix_wrong[n][split_pos[split_loc]] bit_list.append(bit_matrix_wrong.copy()) wrong_pm_update = function.get_pm_update( matrix_temp[0][n][split_pos[split_loc - 1] + 1:split_pos[split_loc] + 1], bit_matrix_wrong[n][split_pos[split_loc - 1] + 1:split_pos[split_loc] + 1], pm_method) pm_list.append(pm_temp + wrong_pm_update) if l_now > list_max/2: pm_list_arg=np.argsort(pm_list) del_list_arg=pm_list_arg[list_max:] #删减多余的列表分支 pm_list = [pm_list[i] for i in range(len(pm_list)) if i not in del_list_arg] llr_list = [llr_list[i] for i in range(len(llr_list)) if i not in del_list_arg] bit_list = [bit_list[i] for i in range(len(bit_list)) if i not in del_list_arg] l_now = len(pm_list) split_loc += 1 if split_pos[-1] != N-1: for i in range(l_now): llr_matrix_temp = llr_list[i] bit_matrix_temp = bit_list[i] pm_temp = pm_list[i] matrix_temp = sc_stepping_decoder(llr_matrix_temp, bit_matrix_temp, information_pos, frozen_bit,N-1) llr_list[i] = matrix_temp[0] bit_list[i] = matrix_temp[1] right_pm_update = function.get_pm_update( matrix_temp[0][n][split_pos[split_loc - 1]+1:N], matrix_temp[1][n][split_pos[split_loc - 1]+1:N], pm_method) pm_list[i] = pm_temp + right_pm_update #接下来提取list中的译码值经过crc校验,采取的是排序pm,从最小的开始经过CRC,第一个通过CRC的即作为译码值输出 pm_argsort=np.argsort(pm_list) for i in pm_argsort: u_d_temp=bit_list[i][n] u_d_temp = np.array([0 if u_d_temp[i] == 0 else 1 for i in range(u_d_temp.size)]) u_d_temp_info = u_d_temp[information_pos] u_d_temp_info = list(u_d_temp_info) crc_c = CRC.CRC(u_d_temp_info, crc_n) flag = crc_c.detection() if flag == 1: u_d=u_d_temp break elif flag == 0 and i == pm_argsort[-1]: u_d_temp=bit_list[pm_argsort[0]][n] u_d_temp = np.array([0 if u_d_temp[i] == 0 else 1 for i in range(u_d_temp.size)]) u_d=u_d_temp return u_d
def decoder(video_path, bin_path, error_path, highth=960, width=960, row_num=6, col_num=47): ff = FFmpeg(inputs={video_path: None}, outputs={'': '%d.png'}) print(ff.cmd) ff.run() k = 2 while (1 == 1): image_dire = str(k) + ".png" if cut(image_dire) == 0: k = k + 1 else: a = np.zeros((col_num, row_num), dtype=np.uint8) image = cv2.imread(image_dire, 0) v_pixel_size = image.shape[0] // col_num h_pixel_size = image.shape[1] // (row_num * 8) re_image = cv2.resize(image, (highth, width)) #print(re_image.shape) cccc = list() for i in range(col_num): for j in range(row_num): a[i][j], strr = arr2byte( re_image[i * 20:(i + 1) * 20, j * 160:(j + 1) * 160], 20, 20) cccc.append(strr) #print(a.shape) ccccc = for_CRC(cccc) cccc = arr2CRC(re_image[940:960, 0:960]) for g in cccc: ccccc.append(g) flag = CRC.check(ccccc) if flag == 0: with open(error_path, 'ab') as f: a3 = np.zeros((col_num, row_num), dtype=np.uint8) a3[0:col_num, 0:row_num] = 255 f.write(a3) else: with open(error_path, 'ab') as f: a4 = np.zeros((col_num, row_num), dtype=np.uint8) a4[0:col_num, 0:row_num] = 0 f.write(a4) with open(bin_path, 'ab') as f: f.write(a) break k = k + 5 z = 0 while (1): image_dir = str(k + 6 * z) + ".png" #print(image_dir) image_dir_next = str(k + 6 * z + 6) + ".png" d = is_same_frame(image_dir, image_dir_next) if (d == 1): break cut(image_dir) #print(image_dir) a = np.zeros((col_num, row_num), dtype=np.uint8) image = cv2.imread(image_dir, 0) v_pixel_size = image.shape[0] // col_num h_pixel_size = image.shape[1] // (row_num * 8) re_image = cv2.resize(image, (highth, width)) ccc = list() for i in range(col_num): for j in range(row_num): a[i][j], strr = arr2byte( re_image[i * 20:(i + 1) * 20, j * 160:(j + 1) * 160], 20, 20) ccc.append(strr) #print(a.shape) cc = for_CRC(ccc) ccc = arr2CRC(re_image[940:960, 0:960]) for g in ccc: cc.append(g) flag = CRC.check(cc) if flag == 0: with open(error_path, 'ab') as f: a1 = np.zeros((col_num, row_num), dtype=np.uint8) a1[0:col_num, 0:row_num] = 255 f.write(a1) else: with open(error_path, 'ab') as f: a2 = np.zeros((col_num, row_num), dtype=np.uint8) a2[0:col_num, 0:row_num] = 0 f.write(a2) with open(bin_path, 'ab') as f: f.write(a) z = z + 1
msg1=msg t1=Time if msg0!='' and msg1!='': if abs(t0-t1)<10: decodedPositions = decoder.get_position(msg0, msg1, t0, t1) if decodedPositions != None and len(decodedPositions)>0: points.append(decodedPositions) return(np.asarray(points)) messageCount=0 print('Generating plane dict') f = open('Syd.txt','r') planeDict={} for line in f: Time,Message = line[:-1].split(' ') isvalid = CRC.computeChecksum(Message) addr = decoder.get_icao_addr(Message) tc = decoder.get_tc(Message) if tc>=9 and tc<=18: rawmsg=Message if isvalid==False: isvalid,Message = CRC.correctBitError(rawmsg) ''' if isvalid==False: isvalid,Message = CRC.correct2BitError(rawmsg) ''' if isvalid==True: messageCount+=1 addr = decoder.get_icao_addr(Message)