Background
This article contains examples for the initial loading of raw accelerometer data to RStudio and Python. Both examples are processing a .bin file.
For exporting the .bin file, see this article.
RStudio
Below is an example script in RStudio for importing and organising the accelerometer data.
#######
#Import data
#######
fileBlock <- readRaw("22_1.HEX", width=24, signed=TRUE)
data<-blockValue(fileBlock)
data2<-as.data.frame(data)
df2 <- as.data.frame(matrix(data2[,1], byrow=TRUE, ncol = 25))
df2$zero<-"0x"
# Organise time variable
df2$time <- paste(df2$zero,df2$V1,df2$V2,df2$V3,df2$V4,df2$V5,df2$V6,df2$V7,df2$V8,df2$V9,df2$V10,df2$V11,df2$V12, sep="")
df2$time<-as.numeric(df2$time)
options(digits.secs=6)
df2$time2<-as.POSIXct(df2$time/1000, origin="1970-01-01", tz="GMT")
# Organise X axis data
df2$x<-paste(df2$V13,df2$V14,df2$V15,df2$V16, sep="")
df2$x2 <- strtoi(df2$x, base=16)
df2$x3 <- ifelse(bitwAnd(df2$x2, 0x8000) > 0, -0xFFFF-1, 0) + df2$x2
df2$x3<-df2$x3*0.0078125
# Organise Y axis data
df2$y<-paste(df2$V17,df2$V18,df2$V19,df2$V20, sep="")
df2$y2 <- strtoi(df2$y, base=16)
df2$y3 <- ifelse(bitwAnd(df2$y2, 0x8000) > 0, -0xFFFF-1, 0) + df2$y2
df2$y3<-df2$y3*0.0078125
# Organise Z axis data
df2$z<-paste(df2$V21,df2$V22,df2$V23,df2$V24, sep="")
df2$z2 <- strtoi(df2$z, base=16)
df2$z3 <- ifelse(bitwAnd(df2$z2, 0x8000) > 0, -0xFFFF-1, 0) + df2$z2
df2$z3<-df2$z3*0.0078125
###
finaldata<-data.frame(time=df2$time2, x=df2$x3, y=df2$y3, z=df2$z3)
data<-finaldata
Python
The following Python function reads a .bin file and stores it in a NumPy array with 4 columns. Column 0 is the timestamp, and column 1-3 are x, y, z. The x, y and z must be scaled with 0.0078125 G.
def read_bin_file(self, filename):s = struct.Struct('>qhhh')
sz = os.path.getsize(filename)
data = numpy.zeros([int(sz/12), 4])
i = 0
f = open(filename, 'rb')
while True:
d = f.read(12)
if len(d) == 0:
break
data[i, :] = s.unpack(b'\0\0' + d)
#ts, x, y, z = s.unpack(b'\0\0' + d)
i += 1
return data
Comments
0 comments
Please sign in to leave a comment.