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)) 56sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 464) # for low latency. 464 is the minimum udp length in our case (CSI only) 57 58# align with side_ch_control.v and all related user space, remote files 59MAX_NUM_DMA_SYMBOL = 8192 60 61if len(sys.argv)<2: 62 print("Assume iq_len = 8187! (Max UDP 65507 bytes; (65507/8)-1 = 8187)") 63 iq_len = 8187 64else: 65 iq_len = int(sys.argv[1]) 66 print(iq_len) 67 # print(type(num_eq)) 68 69if iq_len>8187: 70 iq_len = 8187 71 print('Limit iq_len to 8187! (Max UDP 65507 bytes; (65507/8)-1 = 8187)') 72 73num_dma_symbol_per_trans = 1 + iq_len 74num_byte_per_trans = 8*num_dma_symbol_per_trans 75 76if os.path.exists("iq_2ant.txt"): 77 os.remove("iq_2ant.txt") 78iq_fd=open('iq_2ant.txt','a') 79 80while True: 81 try: 82 data, addr = sock.recvfrom(MAX_NUM_DMA_SYMBOL*8) # buffer size 83 # print(addr) 84 test_residual = len(data)%num_byte_per_trans 85 # print(len(data)/8, num_dma_symbol_per_trans, test_residual) 86 if (test_residual != 0): 87 print("Abnormal length") 88 89 iq = np.frombuffer(data, dtype='int16') 90 np.savetxt(iq_fd, iq) 91 92 timestamp, iq0_capture, iq1_capture = parse_iq(iq, iq_len) 93 print(timestamp, max(iq0_capture.real), max(iq1_capture.real)) 94 display_iq(iq0_capture, iq1_capture) 95 96 except KeyboardInterrupt: 97 print('User quit') 98 break 99 100print('close()') 101side_info_fd.close() 102sock.close() 103