Skip to content

Instantly share code, notes, and snippets.

@sauravrt
Created August 17, 2017 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sauravrt/cf8a53f75e7197b52e8e8d5a61c4fae1 to your computer and use it in GitHub Desktop.
Save sauravrt/cf8a53f75e7197b52e8e8d5a61c4fae1 to your computer and use it in GitHub Desktop.
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
Copy link
Author

Init

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