classifyRain: Classify audio files for presence/absence of rain

View source: R/classifyRain.r

classifyRainR Documentation

Classify audio files for presence/absence of rain

Description

Using thresholds from getThreshold function, audio files are classified as TRUE/FALSE for presence of rain using a minimum or second quartile (more conservative) based threshold at multiple frequency bands - defaults to 2 bands (0.6-1.2 kHz and 4.4-5.6 kHz).

Usage

classifyRain(
  wav,
  thresh.vals,
  freqLo = c(0.6, 4.4),
  freqHi = c(1.2, 5.6),
  t.step = NULL,
  threshold = c("min", "Q2"),
  ID = NULL,
  parallel = 0
)

Arguments

wav

A vector of wav filenames (including directories) or an object of class wav from the tuner package (see parallel, below)

thresh.vals

A named matrix of thresholds obtained from getThreshold. Alternatively, you can create your own matrix (see examples for format).

freqLo

A numeric vector of Lower frequency cut offs for each band.

freqHi

A numeric vector of Higher frequency cut offs for each band.

t.step

NULL or a numeric vector giving time in seconds in which to divide longer files. If NULL, it is assumed that all files analysed are suitably short (e.g. 15 s each) and do not need to be subdivided (see details)

threshold

threshold type ("min" or "Q2") defaults to both - see details

ID

vector of IDs (character or factor) for each wav file identifying rain status, e.g. rain or non-rain (optional). This can be used for testing and calculating accuracy metrics.

parallel

Numeric (or logical). Defaults to 0 - no parallel processing. If 'TRUE' will use multicore processing with the parallel package (Windows only; must be loaded), with number of cores - 2. Otherwise, a positive integer specifies number of cores to use. If wav is a single wav object it makes no sense to use parallel here (rather use hardRain functions within a larger parallelised loop).

Details

This function is based on rain classification techniques in Metcalfe et al. (2019). Thresholds are calculated using minimum psd and signal to noise ratio (mean/sd).

Metcalf, O. C., Lees, A.C., Barlow, J., Marsden, S.J.M. & Devenish, C. (2019). hardRain: an R package for quick, automated rainfall detection in ecoacoustic datasets using a threshold-based approach. Ecological Indicators (in press)

See also: Bedoya C, Isaza C, Daza JM, López JD. (2017). Automatic identification of rainfall in acoustic recordings. Ecological Indicators 75:95–100.

Value

a dataframe with the following columns: filename (of wav files), ID (if provided), logical columns with threshold values for each frequency band and measurement type. If t.step is not NULL, its value is included in the data frame attributes.

Examples

# Get filenames of training data (known rain recordings in wav files). Only five files are used
# here for purposes of this example

train.fn <- list.files(system.file("extdata/rain", package = "hardRain"), "\\.wav$", full.names = T)

# Calculate the threshold using default settings - for two frequency bands
trBR <- getThreshold(train.fn)
trBR

# Get the test filenames (10 wav files with rain / non-rain)
test.fn <- list.files(system.file("extdata/test", package = "hardRain"), "\\.wav$", full.names = T)

# Classify the test files using the thresholds obtained above
resBR <- classifyRain(test.fn, thresh.vals = trBR)
head(resBR)

# How many files identified as rain/non-rain for each threshold?
tapply(resBR$value, list(resBR$threshold), table)

#Using a custom matrix of thresholds:
thresh.m2 <- matrix(c(0.02995, 0.01507, 1.8849, 1.8340, 0.0324, 0.01715, 1.8880, 1.8792),
                   nrow = 2,
                   ncol = 4,
                   byrow = T,
                   dimnames = list(c("min", "Q2"), c("band.1.psd", "band.2.psd", "band.1.s2n", "band.2.s2n")))

# A matrix is required for just one threshold:
thresh.m1 <- matrix(c(0.02995, 0.01507, 1.8849, 1.8340),
                   nrow = 1,
                   ncol = 4,
                   byrow = T,
                   dimnames = list("min", c("band.1.psd", "band.2.psd", "band.1.s2n", "band.2.s2n")))

# Classify with custom thresholds
resBR2 <- classifyRain(test.fn, thresh.vals = thresh.m2)
head(resBR2)

resBR3 <- classifyRain(test.fn, threshold = "min", thresh.vals = thresh.m1)
head(resBR3)

Cdevenish/hardRain documentation built on Jan. 10, 2025, 3:39 a.m.