daily-stats.R (1439B)
1 library(DBI) 2 3 source("R/common.R") 4 5 db <- dbConnect(RSQLite::SQLite(), "samples/load-cell.sqlite3") 6 RSQLite::initExtension(db) 7 8 velocity = dbGetQuery(db, 9 "SELECT timestamp/(60*60*2) AS interval, 10 --STRFTIME('%Y-%m-%dT%H', DATETIME(timestamp, 'unixepoch')) AS timestamp, 11 timestamp, 12 t, x, y, z 13 FROM samples 14 ORDER BY timestamp") 15 16 i <- 0 17 result <- data.frame(interval=numeric(0),timestamp=numeric(0), 18 x_sd=numeric(0),y_sd=numeric(0),z_sd=numeric(0), 19 speed=numeric(0)) 20 c <- calibration_coefficients() 21 unique_intervals <- unique(velocity$interval) 22 pb <- txtProgressBar(min = 0, max = length(unique_intervals), style = 3) 23 for (interval in unique_intervals) { 24 v <- (velocity[velocity$interval==interval, ]) 25 v$x <- sample_to_speed(v$x, c$x1, c$x2) 26 v$y <- sample_to_speed(v$y, c$y1, c$y2) 27 v$z <- sample_to_speed(v$z, c$z1, c$z2) 28 new_row = nrow(result)+1 29 result[new_row, "interval"] <- interval 30 result[new_row, "timestamp"] <- min(v$timestamp) 31 result[new_row, "x_sd"] <- sd(v$x) 32 result[new_row, "y_sd"] <- sd(v$y) 33 result[new_row, "z_sd"] <- sd(v$z) 34 result[new_row, "speed"] <- sqrt(var(v$x) + var(v$y) + var(v$z)) 35 i <- i + 1 36 setTxtProgressBar(pb, i) 37 } 38 close(pb) 39 write.table(result, "build/daily-stats", row.names=FALSE, quote=FALSE) 40 41 dbDisconnect(db)