Skip to main content
3 of 4
added 538 characters in body
WMXZ
  • 7.6k
  • 1
  • 10
  • 35

Not an R-user, so I checked code and played a little bit. The error, you mention, occurs if lts cannot generate time vector (or frequency vector). As you were using AudioMoth naming convention, I suggest to test audiomoth time extraction by

print(audiomoth(dir(dir, pattern="[wav]$|[WAV]$")))

if this returns NaN NaN then something in the filename is not correct (not obvious to me from your post) but maybe an additional character?

If the times are decoded correctly, then the frequency axis is not generated properly. The code for this generation is

  freq <- seq(0, f/2-f/wl, length=wl/2)/1000

So, this could mean that sampling frequency f is bad or too low. You should check sampling frequency independently, or specify in lts call. I know you say 48000 Hz, but maybe you typed 48 and thought input is in kHz.

Edit: In case of NaN NaN response, you could check what went wrong with decoding the time stamp. I did it by extacted the decoding code from seewave into local script and added some print statement in the function

################################################################################
##                                AUDIOMOTH
################################################################################

audiomoth <- function(x,                  # a character vector, not a Wave object
                      tz = ""             # a character vector defining a time zone specification, see as.POSIXct(), argument 'tz'
)
{
  ## INPUT
  if (!is.character(x)) stop("'x' should be of mode character.")
  
  ## PREPARE RESULTS
  options(stringsAsFactors = FALSE)
  res <- data.frame(year = numeric(0), month = numeric(0), day = numeric(0),
                    hour = numeric(0), min = numeric(0), sec = numeric(0),
                    time = numeric(0))
  N <- length(x)  ## number of file names    
  ## LOOP
  for (i in 1:N)
  { tmp <- x[i]
    n <- nchar(tmp)
    ## check if .WAV or .wav file
    extension <- substr(tmp, start = n-3, stop = n)
    if(extension != ".wav" & extension != ".WAV"){warning(paste("File '", tmp, "' is not a '.wav' file", sep=""))}
    else{
      if(nchar(tmp)==12)   ## hexadecimal format
      {
        hex <- unlist(strsplit(tmp, extension))
        num <- strtoi(hex, base = 16)
        time <- as.POSIXct(num, tz=tz, origin = "1970-01-01")  ## POSIXct as in songmeter()
        year <- as.numeric(format(time, "%Y"))
        month <- as.numeric(format(time, "%m"))
        day <- as.numeric(format(time, "%d"))
        hour <- as.numeric(format(time, "%H"))
        min <- as.numeric(format(time, "%M"))
        sec <- as.numeric(format(time, "%S"))
      }
      else if(nchar(tmp)==19)  ## YYYYMMDD_HHMMSS.wav format
      { print(tmp)
        year <- substr(tmp, start = 1, stop = 4)
        month <- substr(tmp, start = 5, stop = 6)
        day <- substr(tmp, start = 7, stop = 8)
        hour <- substr(tmp, start = 10, stop = 11)
        min <- substr(tmp, start = 12, stop = 13)
        sec <- substr(tmp, start = 14, stop = 15)
        time <- strptime(paste(year,month,day,hour,min,sec), "%Y%m%d%H%M%S")
      }
      res <- rbind(res, data.frame(year, month, day, hour, min, sec, time))
    }
  }
  return(res)    
  options(stringsAsFactors = TRUE)
}
dir <- "F:/WavFiles"
print(audiomoth(dir(dir, pattern="[wav]$|[WAV]$")))

which will in my case result to

> source("C:/Users/zimme/Documents/R/Scripts/do_lts_2.R")
[1] "20111208_203052.wav"
[1] "20111208_203122.wav"
  year month day hour min sec                time
1 2011    12  08   20  30  52 2011-12-08 20:30:52
2 2011    12  08   20  31  22 2011-12-08 20:31:22

Obviously directory path to data needs to be adapted

WMXZ
  • 7.6k
  • 1
  • 10
  • 35