xref: /openwifi/user_space/side_ch_ctl_src/side_info_display.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_side_info(freq_offset, csi, equalizer, CSI_LEN, EQUALIZER_LEN):
12    if not hasattr(display_side_info, 'freq_offset_store'):
13        display_side_info.freq_offset_store = np.zeros((256,))
14
15    len_freq_offset = len(freq_offset)
16    display_side_info.freq_offset_store[:(256-len_freq_offset)] = display_side_info.freq_offset_store[len_freq_offset:]
17    display_side_info.freq_offset_store[(256-len_freq_offset):] = freq_offset
18
19    fig_freq_offset = plt.figure(0)
20    fig_freq_offset.clf()
21    plt.xlabel("packet idx")
22    plt.ylabel("Hz")
23    plt.title("freq offset")
24    plt.plot(display_side_info.freq_offset_store)
25    fig_freq_offset.show()
26    plt.pause(0.0001)
27
28    good_row_idx = 0
29    if ( len(equalizer)==0 ):
30        csi_for_plot = csi.T
31    else:
32        equalizer[equalizer == 32767+32767*1j] = 0
33        num_row_equalizer, num_col_equalizer = equalizer.shape
34        equalizer_for_plot = np.zeros((num_row_equalizer, num_col_equalizer)) + 1j*np.zeros((num_row_equalizer, num_col_equalizer))
35
36        num_row_csi, num_col_csi = csi.shape
37        csi_for_plot = np.zeros((num_row_csi, num_col_csi)) + 1j*np.zeros((num_row_csi, num_col_csi))
38
39        # only take out the good equalizer result, when output > 2000, it is not good
40        for i in range(num_row_equalizer):
41            if (not (np.any(abs(equalizer[i,:].real)>2000) or np.any(abs(equalizer[i,:].imag)>2000)) ):
42                equalizer_for_plot[good_row_idx,:] = equalizer[i,:]
43                csi_for_plot[good_row_idx,:] = csi[i,:]
44                good_row_idx = good_row_idx + 1
45
46        csi_for_plot = csi_for_plot[0:good_row_idx,:]
47        equalizer_for_plot = equalizer_for_plot[0:good_row_idx,:]
48        csi_for_plot = csi_for_plot.T
49        equalizer_for_plot = equalizer_for_plot.T
50
51    if ( (len(equalizer)==0) or ((len(equalizer)>0)and(good_row_idx>0)) ):
52        fig_csi = plt.figure(1)
53        fig_csi.clf()
54        ax_abs_csi = fig_csi.add_subplot(211)
55        ax_abs_csi.set_xlabel("subcarrier idx")
56        ax_abs_csi.set_ylabel("abs")
57        ax_abs_csi.set_title("CSI")
58        plt.plot(np.abs(csi_for_plot))
59        ax_phase_csi = fig_csi.add_subplot(212)
60        ax_phase_csi.set_xlabel("subcarrier idx")
61        ax_phase_csi.set_ylabel("phase")
62        plt.plot(np.angle(csi_for_plot))
63        fig_csi.show()
64        plt.pause(0.0001)
65
66    if ( (len(equalizer)>0) and (good_row_idx>0) ):
67        fig_equalizer = plt.figure(2)
68        fig_equalizer.clf()
69        plt.xlabel("I")
70        plt.ylabel("Q")
71        plt.title("equalizer")
72        plt.scatter(equalizer_for_plot.real, equalizer_for_plot.imag)
73        fig_freq_offset.show()
74        plt.pause(0.0001)
75
76def parse_side_info(side_info, num_eq, CSI_LEN, EQUALIZER_LEN, HEADER_LEN):
77    # print(len(side_info), num_eq, CSI_LEN, EQUALIZER_LEN, HEADER_LEN)
78    CSI_LEN_HALF = round(CSI_LEN/2)
79    num_dma_symbol_per_trans = HEADER_LEN + CSI_LEN + num_eq*EQUALIZER_LEN
80    num_int16_per_trans = num_dma_symbol_per_trans*4 # 64bit per dma symbol
81    num_trans = round(len(side_info)/num_int16_per_trans)
82    # print(len(side_info), side_info.dtype, num_trans)
83    side_info = side_info.reshape([num_trans, num_int16_per_trans])
84
85    timestamp = side_info[:,0] + pow(2,16)*side_info[:,1] + pow(2,32)*side_info[:,2] + pow(2,48)*side_info[:,3]
86
87    freq_offset = (20e6*side_info[:,4]/512)/(2*3.14159265358979323846)
88
89    csi = np.zeros((num_trans, CSI_LEN), dtype='int16')
90    csi = csi + csi*1j
91
92    equalizer = np.zeros((0,0), dtype='int16')
93    if num_eq>0:
94        equalizer = np.zeros((num_trans, num_eq*EQUALIZER_LEN), dtype='int16')
95        equalizer = equalizer + equalizer*1j
96
97    for i in range(num_trans):
98        tmp_vec_i = side_info[i,8:(num_int16_per_trans-1):4]
99        tmp_vec_q = side_info[i,9:(num_int16_per_trans-1):4]
100        tmp_vec = tmp_vec_i + tmp_vec_q*1j
101        # csi[i,:] = tmp_vec[0:CSI_LEN]
102        csi[i,:CSI_LEN_HALF] = tmp_vec[CSI_LEN_HALF:CSI_LEN]
103        csi[i,CSI_LEN_HALF:] = tmp_vec[0:CSI_LEN_HALF]
104        if num_eq>0:
105            equalizer[i,:] = tmp_vec[CSI_LEN:(CSI_LEN+num_eq*EQUALIZER_LEN)]
106        # print(i, len(tmp_vec), len(tmp_vec[0:CSI_LEN]), len(tmp_vec[CSI_LEN:(CSI_LEN+num_eq*EQUALIZER_LEN)]))
107
108    return timestamp, freq_offset, csi, equalizer
109
110UDP_IP = "192.168.10.1" #Local IP to listen
111UDP_PORT = 4000         #Local port to listen
112
113sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
114sock.bind((UDP_IP, UDP_PORT))
115sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 464) # for low latency. 464 is the minimum udp length in our case (CSI only)
116
117# align with side_ch_control.v and all related user space, remote files
118MAX_NUM_DMA_SYMBOL = 8192
119CSI_LEN = 56 # length of single CSI
120EQUALIZER_LEN = (56-4) # for non HT, four {32767,32767} will be padded to achieve 52 (non HT should have 48)
121HEADER_LEN = 2 # timestamp and frequency offset
122
123if len(sys.argv)<2:
124    print("Assume num_eq = 8!")
125    num_eq = 8
126else:
127    num_eq = int(sys.argv[1])
128    print(num_eq)
129    # print(type(num_eq))
130
131num_dma_symbol_per_trans = HEADER_LEN + CSI_LEN + num_eq*EQUALIZER_LEN
132num_byte_per_trans = 8*num_dma_symbol_per_trans
133
134if os.path.exists("side_info.txt"):
135    os.remove("side_info.txt")
136side_info_fd=open('side_info.txt','a')
137
138while True:
139    try:
140        data, addr = sock.recvfrom(MAX_NUM_DMA_SYMBOL*8) # buffer size
141        # print(addr)
142        print(len(data), num_byte_per_trans)
143        test_residual = len(data)%num_byte_per_trans
144        if (test_residual != 0):
145            print("Abnormal length")
146
147        side_info = np.frombuffer(data, dtype='int16')
148        np.savetxt(side_info_fd, side_info)
149
150        timestamp, freq_offset, csi, equalizer = parse_side_info(side_info, num_eq, CSI_LEN, EQUALIZER_LEN, HEADER_LEN)
151        print(timestamp)
152        # print(freq_offset)
153        # print(csi[0,0:10])
154        # print(equalizer[0,0:10])
155        display_side_info(freq_offset, csi, equalizer, CSI_LEN, EQUALIZER_LEN)
156
157    except KeyboardInterrupt:
158        print('User quit')
159        break
160
161print('close()')
162side_info_fd.close()
163sock.close()
164