2
$\begingroup$

I am dealing with an unusual error in trying to create long-term spectral averages with the lts() function from R package seewave. I have a 1 h long time series made up of audio files of 5 minutes each recorded by AudioMoth.These files all share identical WaveObject parameters: number of samples (14400000), duration (300 seconds), sampling rate (48000 Hz), channels (Mono), PCM (integer format), and bit (16). Before running the function, I had previously defined the file directory according to the example given in https://rdrr.io/cran/seewave/man/lts.html.

I then run the function using this code:

lts(dir, wl = 1024, ovlp = 50, col = spectro.colors(30), fftw = FALSE, verbose = TRUE, ntann = NULL, tlab = "Time(min)", flab = "Frequency (kHz)" , recorder="audiomoth")

The function appeared to process and list all the files correctly; however, an error message appears at the end:

[1] "File # 1 20191003_050000.wav processed"
[1] "File # 2 20191003_050505.wav processed"
[1] "File # 3 20191003_051010.wav processed"
[1] "File # 4 20191003_051515.wav processed"
[1] "File # 5 20191003_052020.wav processed"
[1] "File # 6 20191003_052525.wav processed"
[1] "File # 7 20191003_053030.wav processed"
[1] "File # 8 20191003_053535.wav processed"
[1] "File # 9 20191003_054040.wav processed"
[1] "File # 10 20191003_054545.wav processed"
[1] "File # 11 20191003_055050.wav processed"
[1] "File # 12 20191003_055555.wav processed"

Error in image.default(x = time, xaxt = "n", xlab = "", y = freq, ylab = flab,  : 
  'x' and 'y' values must be finite and non-missing

Although I scrutinised all of the parameters used, I was unable to determine the source of the problem. My system includes R version 4.3.1, RStudio Version 2023.06.1+524, seewave version 2.2.1, and macOS Ventura 13.5.1. I am aware that I could use Triton (MATLAB) or PAMGuide (MATLAB and R) to create the LTS, but I would like to give a try to seewave, because all other analysis I do using this package. Thanks a lot in anticipation.

$\endgroup$
1
  • $\begingroup$ thanks Noil for formatting $\endgroup$
    – WMXZ
    Commented Sep 6, 2023 at 9:40

2 Answers 2

1
$\begingroup$

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 extracted 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

$\endgroup$
9
  • $\begingroup$ Indeed, when using the print command, I only get NAs. However, when accessing the file through the terminal, everything appears correct, including the source device. File : 20191003_050000.wav Length : 28800360 WAVE Format : 0x1 => WAVE_FORMAT_PCM INFO ICMT : Recorded at 05:00:00 03/10/2019 (UTC-3) by AudioMoth 24526B055A34AA06 at gain setting 2 while battery state was 4.5V. IART : AudioMoth 24526B055A34AA06 data : 28800000 Sample Rate : 48000 Frames : 14400000 Channels : 1 Duration : 00:05:00.000 Signal Max : -10.46 dB) $\endgroup$ Commented Sep 14, 2023 at 10:24
  • $\begingroup$ No, the output from the audiomoth function was Na and not NaN, which are different things, although the zsh terminal output shows me that the timestamp is there. Anyway, I am afraid I do not understand how to use the decoding code extract that you presented. The working directory is in my user's root, precisely to avoid file reading problems. $\endgroup$ Commented Sep 15, 2023 at 9:42
  • $\begingroup$ Can you make one file available? $\endgroup$
    – WMXZ
    Commented Sep 15, 2023 at 16:49
  • $\begingroup$ I modidified my answer to be a complete script that can be sourced. $\endgroup$
    – WMXZ
    Commented Sep 15, 2023 at 19:51
  • $\begingroup$ I saved your script, but I am not sure I understand how to source it. Anyway, I tested again the function on a more recent set of Audiomoth files, obtained with the device updated to the latest firmware. The result was the same, that is, it did not work and the prompt was the same as with the 2019 files. If you want to test them (the newer ones), here they are: shorturl.at/efqB8 $\endgroup$ Commented Sep 20, 2023 at 8:12
1
$\begingroup$

In the function description they specify that this function is for recordings from Wildlife acoustics song meter recorder. Maybe this could be somehow at the origin of the issue? something with the name of the recordings maybe? they use in the function the argument rmoffset=TRUE that is really important for SM4 recordings. I would try with rmoffset=FALSE and see if that solve your issue. I could look more into it but I would need your files to play with the error.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.