From 7325e2460846ff3bf6836316b18582294a851562 Mon Sep 17 00:00:00 2001 From: Vincent van Hees Date: Mon, 25 Mar 2024 17:25:07 +0100 Subject: [PATCH] increment time correctly between pages, fixes #61 --- R/readGENEActiv.R | 4 +++- inst/NEWS.Rd | 1 + src/GENEActivReader.cpp | 12 +++--------- 3 files changed, 7 insertions(+), 10 deletions(-) diff --git a/R/readGENEActiv.R b/R/readGENEActiv.R index 6903dec..a3ececb 100644 --- a/R/readGENEActiv.R +++ b/R/readGENEActiv.R @@ -70,7 +70,9 @@ readGENEActiv = function(filename, start = 0, end = 0, progress_bar = FALSE, } # Correct timestamps - page_offset = (((start - 1) * 300) / rawdata$info$SampleRate) + if (start > 0) { + page_offset = (((start - 1) * 300) / rawdata$info$SampleRate) + } starttime_num = as.numeric(starttime_posix) + page_offset rawdata$time = rawdata$time + abs(rawdata$time[1]) + starttime_num return(invisible(list( diff --git a/inst/NEWS.Rd b/inst/NEWS.Rd index 0c89c3f..ca0fdb3 100755 --- a/inst/NEWS.Rd +++ b/inst/NEWS.Rd @@ -5,6 +5,7 @@ \section{Changes in version 0.3.4 (release date:??-??-2024)}{ \itemize{ \item GENEActiv no longer prints error to console when more data is requested than is in the file because this is not an error, issue #58 + \item readGENEActiv: Fixed bug in incrementing time between file pages #61 } } diff --git a/src/GENEActivReader.cpp b/src/GENEActivReader.cpp index d02e737..8eea7f9 100644 --- a/src/GENEActivReader.cpp +++ b/src/GENEActivReader.cpp @@ -127,18 +127,14 @@ Rcpp::List GENEActivReader(std::string filename, std::size_t start = 0, std::siz while (std::getline(input_file, line)) { ++blockCount; if (blockCount >= start && blockCount <= end) { + blockTime = lastvalue; // header: "Recorded Data" (0), serialCode (1), seq num (2), // blockTime (3), unassigned (4), temp (5), batteryVolt (6), // deviceStatus (7), freq (8), data (9) for (int i = 1; i < blockHeaderSize; i++) { try { std::getline(input_file, header); - if (i == 3) { - std::stringstream ss(header); - int milliseconds; - ss >> milliseconds; - blockTime = lastvalue + milliseconds; - } else if (i == 5) { + if (i == 5) { std::stringstream ss(header); ss.ignore(max_streamsize, ':'); ss >> temperature; @@ -168,7 +164,6 @@ Rcpp::List GENEActivReader(std::string filename, std::size_t start = 0, std::siz double y = 0.0; double z = 0.0; double t = 0.0; - int i = 0; while (hexPosition < data.size() - 1) { try { @@ -182,9 +177,8 @@ Rcpp::List GENEActivReader(std::string filename, std::size_t start = 0, std::siz x = (xRaw * 100. - mfrOffset[0]) / mfrGain[0]; y = (yRaw * 100. - mfrOffset[1]) / mfrGain[1]; z = (zRaw * 100. - mfrOffset[2]) / mfrGain[2]; - t = (double)blockTime + (double)i * (1.0 / freq) * 1000; // Unix millis - lastvalue = t; + lastvalue = t + (1.0 / freq) * 1000; time_array.push_back(t); x_array.push_back(x); y_array.push_back(y);