R/minT.r

Defines functions minT

Documented in minT

#'@export

#' @title Get thresholds for rain detection
#'
#' @param wav A vector of wav filenames (including directories)
#' @param freqLo Lower frequency cut off - defaults to 0.6 kHz
#' @param freqHi Higher frequency cut off - defaults to 1.2 kHz
#' @return If min = TRUE, the threshold values of psd and s2n, if min = FALSE (default), a vector of \code{psd} and \code{s2n}.
#' @examples
#'


minT <- function(wav, freqLo = 0.6, freqHi = 1.2, min = F){

  # check for presence of fftw package and use if present

  if(requireNamespace("fftw", quietly = TRUE)) {
    fftw <- T
  } else {
    fftw <- F
  }


  pb <- txtProgressBar(min = 0, max = length(wav), style = 3)

  a <- lapply(wav, function(x) {

    setTxtProgressBar(pb, which(x == wav))

    b <- tuneR::readWave(x) # read in audiofile

    #mean frequency spectrum
    mfs <- as.data.frame(seewave::meanspec(b, PSD = T, wn = "rectangle", ovlp = 0, fftw = fftw, plot = F))
    # x is frequency (kHz); y is amplitude
    mfs[mfs[,1] > freqLo & mfs[,1] < freqHi, 2] # take psd scores for the rain frequency window khz

    })

  close(pb)

  psd <- sapply(a, mean) # psd of filtered frequency window
  s2n <- sapply(a, function(x) mean(x)/sd(x)) # sig2noise ratio

  if(min) {
    psd <- min(psd)
    s2n <- min(s2n)
  }

  return(list(psd = psd, s2n = s2n))

}
Cdevenish/rainR documentation built on May 22, 2019, 5:36 p.m.