xref: /openwifi/user_space/side_ch_ctl_src/iq_capture.py (revision 38796372a867b70f7d3ed3d3e62872f94c497953)
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(iq_capture, agc_gain, rssi_half_db):
12
13    fig_iq_capture = plt.figure(0)
14    fig_iq_capture.clf()
15    plt.xlabel("sample")
16    plt.ylabel("I/Q")
17    plt.title("I (blue) and Q (red) capture")
18    plt.plot(iq_capture.real, 'b')
19    plt.plot(iq_capture.imag, 'r')
20    plt.ylim(-32767, 32767)
21    fig_iq_capture.show()
22    plt.pause(0.0001)
23
24    agc_gain_lock = np.copy(agc_gain)
25    agc_gain_lock[agc_gain>127] = 80 # agc lock
26    agc_gain_lock[agc_gain<=127] = 0 # agc not lock
27
28    agc_gain_value = np.copy(agc_gain)
29    agc_gain_value[agc_gain>127] = agc_gain[agc_gain>127] - 128
30
31    fig_agc_gain = plt.figure(1)
32    fig_agc_gain.clf()
33    plt.xlabel("sample")
34    plt.ylabel("gain/lock")
35    plt.title("AGC gain (blue) and lock status (red)")
36    plt.plot(agc_gain_value, 'b')
37    plt.plot(agc_gain_lock, 'r')
38    plt.ylim(0, 82)
39    fig_agc_gain.show()
40    plt.pause(0.0001)
41
42    fig_rssi_half_db = plt.figure(2)
43    fig_rssi_half_db.clf()
44    plt.xlabel("sample")
45    plt.ylabel("dB")
46    plt.title("RSSI half dB (uncalibrated)")
47    plt.plot(rssi_half_db)
48    plt.ylim(100, 270)
49    fig_rssi_half_db.show()
50    plt.pause(0.0001)
51
52def parse_iq(iq, iq_len):
53    # print(len(iq), iq_len)
54    num_dma_symbol_per_trans = 1 + iq_len
55    num_int16_per_trans = num_dma_symbol_per_trans*4 # 64bit per dma symbol
56    num_trans = round(len(iq)/num_int16_per_trans)
57    # print(len(iq), iq.dtype, num_trans)
58    iq = iq.reshape([num_trans, num_int16_per_trans])
59
60    timestamp = iq[:,0] + pow(2,16)*iq[:,1] + pow(2,32)*iq[:,2] + pow(2,48)*iq[:,3]
61    iq_capture = iq[:,4::4] + iq[:,5::4]*1j
62    agc_gain = iq[:,6::4]
63    rssi_half_db = iq[:,7::4]
64    # print(num_trans, iq_len, iq_capture.shape, agc_gain.shape, rssi_half_db.shape)
65
66    iq_capture = iq_capture.reshape([num_trans*iq_len,])
67    agc_gain = agc_gain.reshape([num_trans*iq_len,])
68    rssi_half_db = rssi_half_db.reshape([num_trans*iq_len,])
69
70    return timestamp, iq_capture, agc_gain, rssi_half_db
71
72UDP_IP = "192.168.10.1" #Local IP to listen
73UDP_PORT = 4000         #Local port to listen
74
75sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
76sock.bind((UDP_IP, UDP_PORT))
77sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 464) # for low latency. 464 is the minimum udp length in our case (CSI only)
78
79# align with side_ch_control.v and all related user space, remote files
80MAX_NUM_DMA_SYMBOL = 8192
81
82if len(sys.argv)<2:
83    print("Assume iq_len = 8187! (Max UDP 65507 bytes; (65507/8)-1 = 8187)")
84    iq_len = 8187
85else:
86    iq_len = int(sys.argv[1])
87    print(iq_len)
88    # print(type(num_eq))
89
90if iq_len>8187:
91    iq_len = 8187
92    print('Limit iq_len to 8187! (Max UDP 65507 bytes; (65507/8)-1 = 8187)')
93
94num_dma_symbol_per_trans = 1 + iq_len
95num_byte_per_trans = 8*num_dma_symbol_per_trans
96
97if os.path.exists("iq.txt"):
98    os.remove("iq.txt")
99iq_fd=open('iq.txt','a')
100
101while True:
102    try:
103        data, addr = sock.recvfrom(MAX_NUM_DMA_SYMBOL*8) # buffer size
104        # print(addr)
105        test_residual = len(data)%num_byte_per_trans
106        # print(len(data)/8, num_dma_symbol_per_trans, test_residual)
107        if (test_residual != 0):
108            print("Abnormal length")
109
110        iq = np.frombuffer(data, dtype='int16')
111        np.savetxt(iq_fd, iq)
112
113        timestamp, iq_capture, agc_gain, rssi_half_db = parse_iq(iq, iq_len)
114        print(timestamp)
115        display_iq(iq_capture, agc_gain, rssi_half_db)
116
117    except KeyboardInterrupt:
118        print('User quit')
119        break
120
121print('close()')
122side_info_fd.close()
123sock.close()
124