Skip to content

Commit

Permalink
Merge pull request #52 from wadpac/issue48_deprecate_readWav
Browse files Browse the repository at this point in the history
deprecating readWav, fixes #48
  • Loading branch information
vincentvanhees authored Aug 30, 2023
2 parents b6cf545 + 5e53c03 commit f62bc07
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 125 deletions.
114 changes: 3 additions & 111 deletions R/readWav.R
Original file line number Diff line number Diff line change
@@ -1,113 +1,5 @@
readWav = function(filename, start = 1, end = 100, units = "minutes") {
if (start == 0) start = 1
#-----------------------------------------------------
# get data
S = tuneR::readWave(filename, from = start, to = end, units = units)
B = tuneR::extractWave(S, from = start, to = length(S),xunit = c("samples", "time"))
S = as.data.frame(S, stringsAsFactors = TRUE)
B = as.data.frame(B, stringsAsFactors = TRUE)
#-------------------------------------------------------
# extract info from header: fileEncoding does not seem to be consistent, so try variants
header = header_rownames = NULL
Nlines = 18
options(warn = -1) # ignore warnings with unknown character is fields
skiprow = 0
while (length(header) == 0 |
length(grep("Scale-3", header)) == 0 |
length(grep("Scale-2", header)) == 0 |
length(grep("Scale-1", header)) == 0) {

try(expr = {header = suppressWarnings(read.csv(filename,
nrow = Nlines, skip = skiprow,
header = TRUE))}, silent = TRUE)
if (length(header) == 0) {
try(expr = {header = suppressWarnings(read.csv(filename, skipNul = TRUE,
nrow = Nlines, skip = skiprow,
header = TRUE,
fileEncoding = "WINDOWS-1252"))}, silent = TRUE)
}
for (fEn in c("UTF-8", "latin1")) {
if (length(header) == 0) {
try(expr = {header = suppressWarnings(read.csv(filename, skipNul = TRUE,
nrow = Nlines, skip = skiprow,
header = TRUE,
fileEncoding = fEn))}, silent = TRUE)
}
}

if (length(header) > 0) {
if (isTRUE(all.equal(as.character(1:Nlines), header_rownames))) {
header = as.character(header[,1])
} else {
header = rownames(header)
}
}

if (Nlines == 10) {
if (skiprow == 0) {
skiprow = 1
Nlines = 18
} else {
stop("wav file header not recognized in function readWav from the GGIRread package")
}
}
Nlines = Nlines - 1
}
scale3position = grep("Scale-3", header)
options(warn = 0)
header = header[1:scale3position]
P = sapply(as.character(header),function(x) {
tmp = unlist(strsplit(x,": "))
if (length(tmp) == 1) {
tmp = c(tmp, NA)
} else {
tmp = c(tmp[1],tmp[length(tmp)])
}
})

H = tuneR::readWave(filename, from = 1, to = 3600,units = c("seconds"), header = TRUE) #get wav file header
if ("sample.rate" %in% names(H)) {
P = cbind(P, c("sample.rate", as.character(H$sample.rate)))
}
if (is.list(P) == TRUE) {
conv = c()
for (jj in 1:length(P)) {
conv = rbind(conv, P[[jj]], stringsAsFactors = TRUE)
}
P = as.data.frame(conv, stringsAsFactors = TRUE)
} else {
P = as.data.frame(t(P), stringsAsFactors = TRUE)
}
names(P) = c("hnames","hvalues")
row.names(P) = 1:nrow(P)

#-----------------------------------------------
# scale acceleration
scale = as.numeric(as.character(P$hvalues[which(P$hnames == "Scale-1" | P$hnames == "Scale-2" | P$hnames == "Scale-3")]))



if (length(scale) != 3) scale = rep(scale[1],3)
range = 2^(H$bits - 1) # should be 32768 for 16 bit
x = (B$C1/range) * scale[1]
y = (B$C2/range) * scale[2]
z = (B$C3/range) * scale[3]
rawxyz = cbind(x,y,z)
#---------------------------------------------
# get time (we only need first timestamp) --> from header
A = suppressWarnings(scan(filename, what = "character", nlines = 12,
quiet = TRUE, skipNul = TRUE)) #skipNul avoids undesired warning

options(warn = -1) # ignore warnings with unknown character is fields
timestamp = paste0(A[grep("ICMTz", A) + 1:2], collapse = " ")
options(warn = 0)

if (length(timestamp) == 0 | timestamp == "") { #if not possible use other time in fileheader
timestamp = as.character(P$hvalues[which(P$hnames == "Start")])
}

# Note: temperature information is available in channel 4, but documentation is unclear on how to interpret this information
# therefore I am not using it and rely on auto-calibration as performed before .wav file is generated
# g = (B$C4/ range) # temperature, light and battery ?
invisible(list(rawxyz = rawxyz, header = P, timestamp = timestamp))
stop(paste0("readWav has been deprecated (see ",
"https://github.com/wadpac/GGIRread/issues/48). If this is a",
" major problem for you contact package maintainer."))
}
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ Brand | Device name | File extension | GGIRread function
------ | ------- | ------- | ----------------
Axivity Ltd https://axivity.com/ | AX3 and AX6 | .cwa | readAxivity
ActivInsights Ltd https://activinsights.com/ | GENEActiv Original and Sleep | .bin | readGENEActiv
Unilever Discover Ltd | Genea (no longer manufactured) | .bin | readGenea
Axivity Ltd https://axivity.com/ | AX3 | .wav | readWav
Unilever Discover Ltd | Genea (no longer manufactured) | .bin | readGenea
1 change: 1 addition & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
sample pages.
\item Deprecated option to iterate based on timestamps.
\item Now properly accounts for unexpected high sampling rate for the imputation.
\item Deprecated readWav function, see #48 for justification
}
}
\section{Changes in version 0.3.0 (release date: 07-08-2023)}{
Expand Down
8 changes: 6 additions & 2 deletions man/readWav.Rd
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
\name{readWav}
\alias{readWav}
\title{
Reads .wav files as can be stored with OMGUI software by Axivity Ltd
Deprecated function to read .wav files as can be stored with OMGUI software by Axivity Ltd
}
\description{
Reads the wav accelerometer data format as stored with the OMGUI software
Originally designed to read the wav accelerometer data format as stored with the OMGUI software
by Axivity Ltd and documented \href{https://github.com/digitalinteraction/openmovement/wiki/AX3-GUI#export-re-sampled-wav}{here}.

This function has been deprecated as wav files are hardly used, their formatting
is not consistent causing ongoing challenges with keeping the keep functional,
wav format not well facilitated for AX6 recording.
}
\usage{
readWav(filename, start = 1, end = 100,units="minutes")
Expand Down
10 changes: 0 additions & 10 deletions tests/testthat/test_readWav.R

This file was deleted.

0 comments on commit f62bc07

Please sign in to comment.