1# 2# openwifi side info receive and display program 3# Xianjun jiao. [email protected]; [email protected] 4# 5import os 6import sys 7import socket 8import numpy as np 9import matplotlib.pyplot as plt 10 11def display_iq(iq0_capture, iq1_capture): 12 fig_iq_capture = plt.figure(0) 13 fig_iq_capture.clf() 14 15 ax_iq0 = fig_iq_capture.add_subplot(211) 16 # ax_iq0.set_xlabel("sample") 17 ax_iq0.set_ylabel("I/Q") 18 ax_iq0.set_title("rx0 I/Q") 19 plt.plot(iq0_capture.real, 'b') 20 plt.plot(iq0_capture.imag, 'r') 21 plt.ylim(-32767, 32767) 22 23 ax_iq1 = fig_iq_capture.add_subplot(212) 24 ax_iq1.set_xlabel("sample") 25 ax_iq1.set_ylabel("I/Q") 26 ax_iq1.set_title("rx1 I/Q") 27 plt.plot(iq1_capture.real, 'b') 28 plt.plot(iq1_capture.imag, 'r') 29 plt.ylim(-32767, 32767) 30 fig_iq_capture.show() 31 plt.pause(0.0001) 32 33def parse_iq(iq, iq_len): 34 # print(len(iq), iq_len) 35 num_dma_symbol_per_trans = 1 + iq_len 36 num_int16_per_trans = num_dma_symbol_per_trans*4 # 64bit per dma symbol 37 num_trans = round(len(iq)/num_int16_per_trans) 38 # print(len(iq), iq.dtype, num_trans) 39 iq = iq.reshape([num_trans, num_int16_per_trans]) 40 41 timestamp = iq[:,0] + pow(2,16)*iq[:,1] + pow(2,32)*iq[:,2] + pow(2,48)*iq[:,3] 42 iq0_capture = iq[:,4::4] + iq[:,5::4]*1j 43 iq1_capture = iq[:,6::4] + iq[:,7::4]*1j 44 # print(num_trans, iq_len, iq0_capture.shape, iq1_capture.shape) 45 46 iq0_capture = iq0_capture.reshape([num_trans*iq_len,]) 47 iq1_capture = iq1_capture.reshape([num_trans*iq_len,]) 48 49 return timestamp, iq0_capture, iq1_capture 50 51UDP_IP = "192.168.10.1" #Local IP to listen 52UDP_PORT = 4000 #Local port to listen 53 54sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP 55sock.bind((UDP_IP, UDP_PORT)) 56 57# align with side_ch_control.v and all related user space, remote files 58MAX_NUM_DMA_SYMBOL = 8192 59 60if len(sys.argv)<2: 61 print("Assume iq_len = 8187! (Max UDP 65507 bytes; (65507/8)-1 = 8187)") 62 iq_len = 8187 63else: 64 iq_len = int(sys.argv[1]) 65 print(iq_len) 66 # print(type(num_eq)) 67 68if iq_len>8187: 69 iq_len = 8187 70 print('Limit iq_len to 8187! (Max UDP 65507 bytes; (65507/8)-1 = 8187)') 71 72num_dma_symbol_per_trans = 1 + iq_len 73num_byte_per_trans = 8*num_dma_symbol_per_trans 74 75if os.path.exists("iq_2ant.txt"): 76 os.remove("iq_2ant.txt") 77iq_fd=open('iq_2ant.txt','a') 78 79while True: 80 try: 81 data, addr = sock.recvfrom(MAX_NUM_DMA_SYMBOL*8) # buffer size 82 # print(addr) 83 test_residual = len(data)%num_byte_per_trans 84 # print(len(data)/8, num_dma_symbol_per_trans, test_residual) 85 if (test_residual != 0): 86 print("Abnormal length") 87 88 iq = np.frombuffer(data, dtype='int16') 89 np.savetxt(iq_fd, iq) 90 91 timestamp, iq0_capture, iq1_capture = parse_iq(iq, iq_len) 92 print(timestamp, max(iq0_capture.real), max(iq1_capture.real)) 93 display_iq(iq0_capture, iq1_capture) 94 95 except KeyboardInterrupt: 96 print('User quit') 97 break 98 99print('close()') 100side_info_fd.close() 101sock.close() 102