<p>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</p>
<pre><code>print(audiomoth(dir(dir, pattern=&quot;[wav]<span class="math-container">$|[WAV]$</span>&quot;)))
</code></pre>
<p>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?</p>
<p>If the times are decoded correctly, then the frequency axis is not generated properly.
The code for this generation is</p>
<pre><code>  freq &lt;- seq(0, f/2-f/wl, length=wl/2)/1000
</code></pre>
<p>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.</p>
<p>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</p>
<pre><code>################################################################################
##                                AUDIOMOTH
################################################################################

audiomoth &lt;- function(x,                  # a character vector, not a Wave object
                      tz = &quot;&quot;             # a character vector defining a time zone specification, see as.POSIXct(), argument 'tz'
)
{
  ## INPUT
  if (!is.character(x)) stop(&quot;'x' should be of mode character.&quot;)
  
  ## PREPARE RESULTS
  options(stringsAsFactors = FALSE)
  res &lt;- data.frame(year = numeric(0), month = numeric(0), day = numeric(0),
                    hour = numeric(0), min = numeric(0), sec = numeric(0),
                    time = numeric(0))
  N &lt;- length(x)  ## number of file names    
  ## LOOP
  for (i in 1:N)
  { tmp &lt;- x[i]
    n &lt;- nchar(tmp)
    ## check if .WAV or .wav file
    extension &lt;- substr(tmp, start = n-3, stop = n)
    if(extension != &quot;.wav&quot; &amp; extension != &quot;.WAV&quot;){warning(paste(&quot;File '&quot;, tmp, &quot;' is not a '.wav' file&quot;, sep=&quot;&quot;))}
    else{
      if(nchar(tmp)==12)   ## hexadecimal format
      {
        hex &lt;- unlist(strsplit(tmp, extension))
        num &lt;- strtoi(hex, base = 16)
        time &lt;- as.POSIXct(num, tz=tz, origin = &quot;1970-01-01&quot;)  ## POSIXct as in songmeter()
        year &lt;- as.numeric(format(time, &quot;%Y&quot;))
        month &lt;- as.numeric(format(time, &quot;%m&quot;))
        day &lt;- as.numeric(format(time, &quot;%d&quot;))
        hour &lt;- as.numeric(format(time, &quot;%H&quot;))
        min &lt;- as.numeric(format(time, &quot;%M&quot;))
        sec &lt;- as.numeric(format(time, &quot;%S&quot;))
      }
      else if(nchar(tmp)==19)  ## YYYYMMDD_HHMMSS.wav format
      { print(tmp)
        year &lt;- substr(tmp, start = 1, stop = 4)
        month &lt;- substr(tmp, start = 5, stop = 6)
        day &lt;- substr(tmp, start = 7, stop = 8)
        hour &lt;- substr(tmp, start = 10, stop = 11)
        min &lt;- substr(tmp, start = 12, stop = 13)
        sec &lt;- substr(tmp, start = 14, stop = 15)
        time &lt;- strptime(paste(year,month,day,hour,min,sec), &quot;%Y%m%d%H%M%S&quot;)
      }
      res &lt;- rbind(res, data.frame(year, month, day, hour, min, sec, time))
    }
  }
  return(res)    
  options(stringsAsFactors = TRUE)
}
dir &lt;- &quot;F:/WavFiles&quot;
print(audiomoth(dir(dir, pattern=&quot;[wav]<span class="math-container">$|[WAV]$</span>&quot;)))
</code></pre>
<p>which will in my case result to</p>
<pre><code>&gt; source(&quot;C:/Users/zimme/Documents/R/Scripts/do_lts_2.R&quot;)
[1] &quot;20111208_203052.wav&quot;
[1] &quot;20111208_203122.wav&quot;
  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
</code></pre>
<p>Obviously directory path to data needs to be adapted</p>

CC BY-SA 4.0
2023-09-14T10:24:35.010Z
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)
Mario Rollo
CC BY-SA 4.0
2023-09-15T09:42:38.540Z
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.
Mario Rollo
CC BY-SA 4.0
2023-09-15T16:49:25.743Z
Can you make one file available?
WMXZ
CC BY-SA 4.0
2023-09-15T19:51:50.793Z
I modidified my answer to be a complete script that can be sourced.
WMXZ
CC BY-SA 4.0
2023-09-20T08:12:02.293Z
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: https://shorturl.at/efqB8
Mario Rollo
CC BY-SA 4.0
2023-09-20T08:34:39.477Z
I even tested other file groups, also more recent than those of my initial question. The audiomoth function from the same seewave package also does not read the timestamps of these files. 
> dir <- "~/20220908"
> audiomoth("20220908_094608.WAV")
  year month day hour min sec time
1   NA    NA  NA   NA  NA  NA <NA>
Consequently, the lts will not work once again.
Mario Rollo
CC BY-SA 4.0
2023-09-20T08:44:57.417Z
Perhaps these related seewave functions work with files from SongMeter recorder, but apparently not with AudioMoth, with any and all files I have, whether older or more recent.
Mario Rollo
CC BY-SA 4.0
2023-09-20T15:56:46.593Z
I source the script from R-studio. I copied  the audiomoth script from Github, so it is the latest version. If you run it (after modified the path to the files) the there is an additional line (generated by print statement) that indicates the filename that the function tries to interpret. You can add additional print statements to narrow down the issue. If you are only a R-user and not familiar with coding, maybe someone in your vicinity can help. BTW, I cannot access the file you uploaded, access limited to your university accounts. lts() works ONLY for AudioMoth and SongMeter (unfortunately)
WMXZ
CC BY-SA 4.0
2023-10-05T11:09:07.203Z
I sourced your script to the correct path and received this output:
> source("~/20220908/do_lts_2.R")
Error in if (extension != ".wav" & extension != ".WAV") { : 
  missing value where TRUE/FALSE needed
Mario Rollo
CC BY-SA 4.0
2023-09-05T13:21:38.163Z
2024-06-12T05:33:55.617Z
2024-06-12T05:33:55.617Z
WMXZ
<p>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 <code>rmoffset=TRUE</code> that is really important for SM4 recordings. I would try with <code>rmoffset=FALSE</code> and see if that solve your issue. I could look more into it but I would need your files to play with the error.</p>

CC BY-SA 4.0
2024-07-15T19:36:17.837Z
2024-07-18T11:52:31.743Z
2024-07-18T11:52:31.743Z
Amandine Gasc
<p>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 <a href="https://rdrr.io/cran/seewave/man/lts.html" rel="nofollow noreferrer">https://rdrr.io/cran/seewave/man/lts.html</a>.</p>
<p>I then run the function using this code:</p>
<pre><code>lts(dir, wl = 1024, ovlp = 50, col = spectro.colors(30), fftw = FALSE, verbose = TRUE, ntann = NULL, tlab = &quot;Time(min)&quot;, flab = &quot;Frequency (kHz)&quot; , recorder=&quot;audiomoth&quot;)
</code></pre>
<p>The function appeared to process and list all the files correctly; however, an error message appears at the end:</p>
<pre><code>[1] &quot;File # 1 20191003_050000.wav processed&quot;
[1] &quot;File # 2 20191003_050505.wav processed&quot;
[1] &quot;File # 3 20191003_051010.wav processed&quot;
[1] &quot;File # 4 20191003_051515.wav processed&quot;
[1] &quot;File # 5 20191003_052020.wav processed&quot;
[1] &quot;File # 6 20191003_052525.wav processed&quot;
[1] &quot;File # 7 20191003_053030.wav processed&quot;
[1] &quot;File # 8 20191003_053535.wav processed&quot;
[1] &quot;File # 9 20191003_054040.wav processed&quot;
[1] &quot;File # 10 20191003_054545.wav processed&quot;
[1] &quot;File # 11 20191003_055050.wav processed&quot;
[1] &quot;File # 12 20191003_055555.wav processed&quot;

Error in image.default(x = time, xaxt = &quot;n&quot;, xlab = &quot;&quot;, y = freq, ylab = flab,  : 
  'x' and 'y' values must be finite and non-missing
</code></pre>
<p>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.</p>

CC BY-SA 4.0
2023-09-06T09:40:12.087Z
thanks Noil for formatting
WMXZ
<r><audiomoth><ltsa>
Error using lts() from R package seewave
# Error using lts() from R package seewave

(Asked by: username_0 on 2023-09-05)

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.

**Tags:** r, audiomoth, ltsa

> **Comments:**
> **username_1**: thanks Noil for formatting  

---

## 2 answers

### Answer by username_1 on 2023-09-05. Score: 1

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

> **Comments:**
> **username_0**: 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)  
> **username_0**: 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.  
> **username_1**: Can you make one file available?  
> **username_1**: I modidified my answer to be a complete script that can be sourced.  
> **username_0**: 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: https://shorturl.at/efqB8  
> **username_0**: I even tested other file groups, also more recent than those of my initial question. The audiomoth function from the same seewave package also does not read the timestamps of these files.  > dir <- "~/20220908" > audiomoth("20220908_094608.WAV")   year month day hour min sec time 1   NA    NA  NA   NA  NA  NA <NA> Consequently, the lts will not work once again.  
> **username_0**: Perhaps these related seewave functions work with files from SongMeter recorder, but apparently not with AudioMoth, with any and all files I have, whether older or more recent.  
> **username_1**: I source the script from R-studio. I copied  the audiomoth script from Github, so it is the latest version. If you run it (after modified the path to the files) the there is an additional line (generated by print statement) that indicates the filename that the function tries to interpret. You can add additional print statements to narrow down the issue. If you are only a R-user and not familiar with coding, maybe someone in your vicinity can help. BTW, I cannot access the file you uploaded, access limited to your university accounts. lts() works ONLY for AudioMoth and SongMeter (unfortunately)  
> **username_0**: I sourced your script to the correct path and received this output: > source("~/20220908/do_lts_2.R") Error in if (extension != ".wav" & extension != ".WAV") { :    missing value where TRUE/FALSE needed  

---

### Answer by username_2 on 2024-07-15. Score: 1

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.


---

