Created
August 17, 2017 14:16
-
-
Save sauravrt/cf8a53f75e7197b52e8e8d5a61c4fae1 to your computer and use it in GitHub Desktop.
Read binary data file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Init