Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
to_compamp()
packet_header_size = 64
packet_data_size = 4096
packet_size = packet_header_size + packet_data_size
def to_compamp(segment): # convert measurement data to timestamp & numpy array
segment_name = segment[0]
segment_data = segment[1]
bytes_per_segment = len(segment_data)
# example: 0:u:e5sdfb0c-3012-49f4-fe9d-12341ecacc91-000001
segment_nr = int(segment_name.split('-')[-1])
segment_byte_start = (segment_nr - 1) * bytes_per_segment
# the number of full packets preceding this segment
full_packets = int(np.floor(segment_byte_start / packet_size))
remainder = segment_byte_start - (full_packets * packet_size)
# position where first full packet starts in segment
start_pos = packet_size - remainder
if remainder == 0:
start_pos = 0
# amount of full packets in segment
packets = (bytes_per_segment - start_pos) // packet_size
raw = segment_data[start_pos:(start_pos + packets*packet_size)]
timestamp = unpack('BBBBIBBBBIIIddffQII',
segment_data[start_pos:start_pos+packet_header_size])[16]
arr = np.frombuffer(raw, dtype=np.int8)
arr = arr.reshape((packets, packet_size))[:, packet_header_size:]
.reshape((packets * packet_data_size))
n = len(arr) / 2
coef = np.empty(n, complex)
coef.real = arr[::2].reshape(n)
coef.imag = arr[1::2].reshape(n)
return timestamp, coef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment