Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Read binary data file
import struct
import numpy as np
if __name__ == '__main__':
# Data format: "%1B(Hours)%1B(Minutes)%4F(Seconds)%4U(ChipTimeUS)
The header string is 108 bytes long + CRLF\r\n (2bytes)
Each valid data record is 22 bytes
fname = '../Yost/data/data1.dat'
dwidth = 22
data = []
with open(fname, 'rb') as file: # skip header + newline = 110 bytes
while True:
bdata = # 22 bytes
if len(bdata) < dwidth:
data.append(list(struct.unpack('>BBfIfff', bdata)))
data = np.array(data)
tm = (data[:, 3] - data[0, 3])/1e6
dtm = np.diff(tm)
print(1/np.mean(dtm)) # mean sample rate

This comment has been minimized.

Copy link
Owner Author

@sauravrt sauravrt commented Aug 17, 2017


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment