-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdata_read_only_sensor.py
147 lines (125 loc) · 5.04 KB
/
data_read_only_sensor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import numpy as np
import struct
import sys
import os
FRAMES = 50
dca_name = sys.argv[1]
n_frames = int(sys.argv[2])
annotated_fname = dca_name.split("/")[0]+"/only_sensor"+dca_name.split("/")[1]
FRAMES = n_frames+1
ADC_PARAMS = {'chirps': 128, # 32
'rx': 4,
'tx': 3,
'samples': 256,
'IQ': 2,
'bytes': 2}
array_size = ADC_PARAMS['chirps'] * ADC_PARAMS['rx'] * ADC_PARAMS['tx'] * ADC_PARAMS['IQ'] * ADC_PARAMS['samples']
element_size = ADC_PARAMS['bytes']
def read_and_print_dca_file(filename, packet_size):
rows = FRAMES
cols = (728 * 1536) # (N_bytes_in_packet x N_packets_in_frame) Integer division
timestamp_infos = []
# Creating a numpy array of uint16 type, initialized with zeros
frame_array = np.zeros((rows, cols), dtype=np.uint16)
frame_time_array=np.zeros(FRAMES,dtype=np.float64)
dirty_array=np.zeros(FRAMES)
index=0
with open(filename,'rb') as file:
last_packet_num=0
while True:
timestamp_data=file.read(8)
if not timestamp_data:
break
timestamp=struct.unpack('q',timestamp_data)[0]
data=file.read(packet_size)
packet_num=struct.unpack('<1l',data[:4])[0]
last_packet_num=packet_num
# byte_count=struct.unpack('>Q',b'\x00\x00'+data[4:10][::-1])[0]
if (packet_num%(1536))==0:
print("packet_num%(1536))==0", packet_num)
break
packet_idx_in_frame=0
while True:
timestamp_data=file.read(8)
if not timestamp_data:
break
timestamp=struct.unpack('q',timestamp_data)[0]
# print("timestamp: ", timestamp)
data=file.read(packet_size) # The next packet_data
if not data:
break
packet_num=struct.unpack('<1l',data[:4])[0]
if packet_num==last_packet_num+1:
last_packet_num=packet_num
frame_array[index][packet_idx_in_frame:packet_idx_in_frame+728]= np.frombuffer(data[10:], dtype=np.uint16)
packet_idx_in_frame+=728
if packet_idx_in_frame==728*1536:
frame_time_array[index]=timestamp
packet_idx_in_frame=0
index+=1
continue
elif packet_num>last_packet_num+1:
#Packet lost ho gaya hai
#matlab yeh frame chud gaya hai and we have to reject it
dirty_array[index]=1
frame_array[index][packet_idx_in_frame:packet_idx_in_frame+728]=np.zeros(728)
packet_idx_in_frame+=728
last_packet_num=packet_num
if packet_idx_in_frame==728*1536:
# if packet_num%1536==0:
frame_time_array[index]=timestamp
packet_idx_in_frame=0
index+=1
continue
# while (packet_num%1536)!=0:
# timestamp_data=file.read(8)
# data=file.read(packet_size)
# if not data:
# break
# packet_num=struct.unpack('<1l',data[:4])[0]
# frame_time_array[index]=timestamp_data
# index+=1
# byte_count=struct.unpack('>Q',b'\x00\x00'+data[4:10][::-1])[0]
# if c==1:
# print(timestamp)
# print(packet_num)
# if (byte_count%(728*1536))==0:
# print("Hello")
# print(packet_num)
# print(timestamp)
for i in range(FRAMES):
if dirty_array[i]==1:
#reject the frame
if i==0:
j=i
while(dirty_array[j]==0):
j+=1
frame_array[i]=frame_array[j]
else:
j=i
while(j>=0 and dirty_array[j]==0):
j-=1
frame_array[i]=frame_array[j]
#Now we have a frame array proper of 100*(1456*1536))
# print("Dirtysum")
# print(np.sum(dirty_array))
return frame_array,frame_time_array
def annotate(dca_array,frames):
if os.path.exists(annotated_fname):
os.remove(annotated_fname)
annotation_file = open(annotated_fname, "ab")
for i in range (frames):
annotation_file.write(dca_array[i])
annotation_file.close()
def annotate_time_stamp(dca_time_array,frames):
annotated_fname = "time_stamps/time"+dca_name.split("/")[1]
if os.path.exists(annotated_fname):
os.remove(annotated_fname)
annotation_file = open(annotated_fname, "ab")
for i in range (frames):
annotation_file.write(dca_time_array[i])
annotation_file.close()
dca_array,dca_time_array=read_and_print_dca_file(dca_name,1466)
annotate(dca_array,FRAMES)
annotate_time_stamp(dca_time_array, FRAMES)
# print(dca_array.shape)cols), dtype=np.uint16)