Forum Comments

How to extract temperature data from any Audiomoth's file?
In Device Support
p.holderried
Mar 22, 2022
The function audiomoth_wave from the R-package sonicscrewdriver is exactly what you are looking for. You can find the vignette here. It didn't work properly the last time I tried to use it. I made a few changes to the code and it has worked fine for me so far, All the function does is extract the text from the wav header and split it before/after certain words or a certain number of characters. library(stringr) library(dplyr) audiomoth_header <- function(filename) { f <- readBin(filename, "character", n = 25) for(i in 1:length(f)) { if(regexpr("Recorded", f[i]) == 1) { n = i } } raw <- f[n] if (regexpr("Recorded", raw) != 1) { print("No audiomoth comment field found.") return(FALSE) } r <- regexpr("Recorded at", raw) + 12 date_time <- as.POSIXct(substr(raw, r, r+18), format = "%H:%M:%S %d/%m/%Y", tz = "UTC") r <- regexpr("Recorded at", raw) + 21 day <- as.Date(substr(raw, r, r+10), format = "%d/%m/%Y", tz = "UTC") r <- regexpr("AudioMoth", raw) + 10 serial <- substr(raw, r, r+16) r <- regexpr("AudioMoth [0-9|A-Z]{16} at ", raw) + 30 l <- regexpr("gain setting", raw) - r -2 gain <- substr(raw, r, r+l) if ( regexpr("less than 2\\.5V", raw) != -1) { voltage <- "<2.5" } else if (regexpr("greater than 4\\.9V", raw) != -1) { voltage <- ">4.9" } else { r <- regexpr("[0-9].[0-9]V", raw) voltage <- substr(raw, r, r+2) } temp <- str_extract(raw, "(?<=temperature was )[[:digit:]]{1,2}[[:punct:]]{1}[[:digit:]]{1,2}") filter <- FALSE filter_limit <- FALSE filter.limit <- if (regexpr("Low-pass filter applied", raw) != -1) { filter <- "Low-pass" } if (regexpr("Band-pass filter applied", raw) != -1) { filter <- "Band-pass" } if (regexpr("High-pass filter applied", raw) != -1) { filter <- "High-pass" } if (is.element(filter, c("Low-pass", "High-pass"))) { r <- regexpr("frequency of", raw) + 13 l <- regexpr("kHz", raw) - 1 filter_limit <- substr(raw,r,l) } if (filter == "Band-pass") { al <- regexpr("frequencies of", raw) + 15 ar <- regexpr("kHz", raw) - 1 bl <- regexpr("kHz and", raw) + 8 br <- regexpr("kHz\\.", raw) - 1 filter_limit(paste0(substr(raw,al, ar),"-",substr(raw,bl,br))) } if (regexpr("microphone change.", raw) != -1) { cancelled <- "microphone change" } else if (regexpr("change of switch position.", raw) != -1) { cancelled <- "change of switch position" } else if (regexpr("low voltage.", raw) != -1) { cancelled <- "low voltage" } else if (regexpr("file size limit.", raw) != -1) { cancelled <- "file size limit" } else if (regexpr("change of switch position", raw) != -1) { cancelled <- "change of switch position" } else {cancelled <- FALSE} header <- list( "raw" = raw, "date_time" = date_time, "day" = day, "serial" = serial, "gain" = gain, "voltage" = as.numeric(voltage), "temperature" = as.numeric(temp), "filter" = filter, "filter.limit" = filter_limit, "cancelled" = cancelled ) return(header) } wav_paths <- c("path/to/files/file1.wav", "path/to/files/file2.wav") header <- lapply(wav_paths, function(w) {audiomoth_header(w)})
1
p.holderried
More actions