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