Skip to content

Instantly share code, notes, and snippets.

Embed
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)
%4F(RawAccelX)%4F(RawAccelY)%4F(RawAccelZ)"
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:
file.seek(110) # skip header + newline = 110 bytes
while True:
bdata = file.read(dwidth) # 22 bytes
if len(bdata) < dwidth:
break
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
@sauravrt

This comment has been minimized.

Copy link
Owner Author

@sauravrt sauravrt commented Aug 17, 2017

Init

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