Learn R Programming

Ruido (version 1.0.1)

bgNoise: Background Noise and Soundscape Power Index

Description

Compute the Background Noise and Soundscape Power values of an audio using Towsey 2017 methodology

Usage

bgNoise(
  soundfile,
  channel = "stereo",
  timeBin = 60,
  dbThreshold = -90,
  targetSampRate = NULL,
  wl = 512,
  window = signal::hamming(wl),
  overlap = ceiling(length(window)/2),
  histbreaks = "FD"
)

Value

A list containing three objects: The first and second one contains a matrix with the values of Background Noise and Soundscape Power respectively to each time bin and for each frequency window of your soundfile. The third object is the duration in second of your time bins.

Arguments

soundfile

tuneR Wave object or path to a valid audio

channel

channel where the background noise values will be extract from. Available channels are: "stereo", "mono", "left" or "right". Defaults to "stereo".

timeBin

size (in seconds) of the time bin. Defaults to 60.

dbThreshold

minimum allowed value of dB for the spectrograms. Defaults to -90, as set by Towsey 2017.

targetSampRate

sample rate of the audios. Defaults to NULL to not change the sample rate. This argument is only used to down sample the audio.

wl

window length of the spectrogram. Defaults to 512.

window

window used to smooth the spectrogram. Defaults to signal::hammning(wl). Switch to signal::hanning(wl) if to use hanning instead.

overlap

overlap between the spectrogram windows. Defaults to wl/2 (half the window length)

histbreaks

breaks used to calculate Background Noise. Available breaks are: "FD", "Sturges", "scott" and 100. Defaults to "FD".
Can also be set to any number to limit or increase the amount of breaks.

Details

Background Noise (BGN) is an index that measures the most common continuous baseline level of acoustic energy in a frequency window and in a time bin. It was developed by Towsey 2017 using the Lamel et al 1981 algorithm. is calculated by taking the modal value of intensity values in temporal bin c in frequency window f:

$$BGN_{f} = mode(dB_{cf})$$

Soundscape Power represents a measure of signal-to-noise ratio. It measures the relation of BGN to the loudest intensities in temporal bin c in frequency window f:

$$POW_{f} = max(dB_{cf}) - BGN_{cf}$$

References

Towsey, M. W. (2017). The calculation of acoustic indices derived from long-duration recordings of the natural environment. In eprints.qut.edu.au. https://eprints.qut.edu.au/110634/
Lamel, L., Rabiner, L., Rosenberg, A., & Wilpon, J. (1981). An improved endpoint detector for isolated word recognition. IEEE Transactions on Acoustics, Speech, and Signal Processing, 29(4), 777-785 https://doi.org/10.1109/TASSP.1981.1163642

Examples

Run this code
### For our main example we'll create an artificial audio with
### white noise to test its Background Noise
# We'll use the package tuneR
library(tuneR)

# Define the audio sample rate, duration and number of samples
sampRate <- 12050
dur <- 59
sampN <- sampRate * dur

# Then we Ggenerate the white noise for our audio and apply FFT
set.seed(413)
ruido <- rnorm(sampN)
spec <- fft(ruido)

# Now we create a random spectral envelope for the audio and apply the spectral envelope
nPoints <- 50
env <- runif(nPoints)
env <- approx(env, n=nPoints)$y
specMod <- spec * env

# Now we invert the FFT back to into a time waveform and normalize and convert to Wave
out <- Re(fft(specMod, inverse=TRUE)) / sampN
wave <- Wave(left = out, samp.rate = sampRate, bit = 16)
wave <- normalize(wave, unit = "16")

# Here's our artificial audio
wave

# Running the bgNoise function with all the default arguments
bgn <- bgNoise(wave)

# Print the results
head(bgn$mono$BGN)
head(bgn$mono$POW)

# Plotting background noise and soundscape profile for the first minute of the recording
par(mfrow = c(2,2))
plot(x = bgn$mono$BGN$BGN1, y = seq(1,bgn$sampRate, length.out = 256),
     xlab = "Background Noise (dB)", ylab = "Frequency (hz)",
     main = "BGN by Frequency",
     type = "l")
plot(x = bgn$mono$POW$POW1, y = seq(1,bgn$sampRate, length.out = 256),
     xlab = "Soundscape Power (dB)", ylab = "Frequency (hz)",
     main = "POW by Frequency",
     type = "l")
plot(bgn$mono$BGN$BGN1~bgn$mono$POW$POW1, pch = 16,
     xlab = "Soundscape Power (dB)", ylab = "Background Noise (dB)",
     main = "BGN~POW")
hist(bgn$mono$BGN$BGN1, main = "Histogram of BGN distribution",
      xlab = "Background Noise (BGN)")

# \donttest{
  oldpar <- par(no.readonly = TRUE)
  ### This is a secondary example using audio from a real soundscape
  ### These audios are originated from the Escutadô Project
  # Getting audiofile from the online Zenodo library
  dir <- tempdir()
  rec <- paste0("GAL24576_20250401_", sprintf("%06d", 0),".wav")
  recDir <- paste(dir,rec , sep = "/")
  url <- paste0("https://zenodo.org/records/17575795/files/", rec, "?download=1")

  # Downloading the file, might take some time denpending on your internet
  download.file(url, destfile = recDir, mode = "wb")

  # Running the bgNoise function with all the default arguments
  bgn <- bgNoise(recDir)

  # Print the results
  head(bgn$left$BGN)
  head(bgn$left$POW)

  # Plotting background noise and soundscape profile for the first minute of the recording
  par(mfrow = c(2,2))
  plot(x = bgn$left$BGN$BGN1, y = seq(1,bgn$sampRate, length.out = 256),
       xlab = "Background Noise (dB)", ylab = "Frequency (hz)",
       main = "BGN by Frequency",
       type = "l")
  plot(x = bgn$left$POW$POW1, y = seq(1,bgn$sampRate, length.out = 256),
       xlab = "Soundscape Power (dB)", ylab = "Frequency (hz)",
       main = "POW by Frequency",
       type = "l")
  plot(bgn$left$BGN$BGN1~bgn$left$POW$POW1, pch = 16,
       xlab = "Soundscape Power (dB)", ylab = "Background Noise (dB)",
       main = "BGN~POW in left ear")
  plot(bgn$right$BGN$BGN1~bgn$right$POW$POW1, pch = 16,
       xlab = "Soundscape Power (dB)", ylab = "Background Noise (dB)",
       main = "BGN~POW in right ear")

  unlink(recDir)
  par(oldpar)
# }

Run the code above in your browser using DataLab