birdnet_spectro: Plot spectrograms of BirdNET detections

View source: R/birdnet_spectro.R

birdnet_spectroR Documentation

Plot spectrograms of BirdNET detections

Description

Plot spectrograms of user-selected verified or unverified data

Usage

birdnet_spectro(
  data,
  audio.directory,
  title,
  frq.lim = c(0, 12),
  new.window = TRUE,
  spec.col = monitoR::gray.3(),
  box = TRUE,
  box.lwd = 1,
  box.col = "black",
  title.size = 1
)

Arguments

data

Data.table or data.frame of subsetted detections that a user would like to plot for a single species. This allows the user precise control over which detections to plot.

audio.directory

Top-level input directory path to audio files to be processed. Files are expected to have the naming convention SITEID_YYYYMMDD_HHMMSS.wav.

title

Optional title describing which detections are being plotted (e.g., "Confidence > 0.5", "True Positives", "Alarm Calls").

frq.lim

Optional two-element numeric vector specifying frequency limits to the plotted spectrograms, in kHz. Default = c(0, 12).

new.window

Logical value for whether to use dev.new to produce new plot windows. Default = TRUE.

spec.col

The colors used to plot verification spectrograms. Default = gray.3(). Spectrogram colors are adjustable, and users may create their own gradients for display. A few spectrogram color options are provided via the R package monitoR, including gray.1(), gray.2(), gray.3(), rainbow.1(), and topo.1(), all of which are based on existing R colors.

box

Logical for whether to draw a box around each detection. Default = TRUE.

box.lwd

Integer value for box line thickness. Default = 1.

box.col

Box color. Default = 'black'.

title.size

Size of title. Default = 1.

Details

This function was developed by the National Park Service Natural Sounds and Night Skies Division to process audio data produced by BirdNET.

Value

Plot of verified detections

See Also

birdnet_analyzer, birdnet_format, birdnet_verify

Examples

## Not run: 

# Create an audio directory for this example
dir.create('example-audio-directory')

# Read in example wave files
data(exampleAudio1)
data(exampleAudio2)

# Write example waves to example audio directory
tuneR::writeWave(
  object = exampleAudio1,
  filename = 'example-audio-directory/Rivendell_20210623_113602.wav'
)
tuneR::writeWave(
  object = exampleAudio2,
  filename = 'example-audio-directory/Rivendell_20210623_114602.wav'
)

# Read in example data.table/data.frame for plotting
data(exampleSpectroData)

# Plot detections of Swainson's Thrush that contain songs
# with frequency limits ranging from 0.5 to 12 kHz, gray spectrogram colors,
# a custom title, and a gray box around each detection
plot.songs <- exampleSpectroData[common_name == "Swainson's Thrush" & verify %in% c("song", "both")]
birdnet_spectro(
  data = plot.songs,
  audio.directory = 'example-audio-directory',
  title = "Swainson's Thrush Songs",
  frq.lim = c(0.5, 12),
  new.window = TRUE,
  spec.col = gray.3(),
  box = TRUE,
  box.lwd = 1,
  box.col = 'gray'
)

# Plot only detections of Swainson's Thrush that contain calls
# with frequency limits ranging from 0.5 to 6 kHz, a custom title, no boxes,
# and colors sampled from the viridis color package
plot.calls <- exampleSpectroData[common_name == "Swainson's Thrush" & verify %in% c("call", "both")]
birdnet_spectro(
  data = plot.calls,
  audio.directory = 'example-audio-directory',
  title = "Swainson's Thrush Calls",
  frq.lim = c(0.5, 12),
  new.window = TRUE,
  spec.col = viridis::viridis(30),
  box = FALSE,
)

# Loop through to plot detections for selected unverified species
# where confidence of detection >= 0.25
# with frequency limits ranging from 0.5 to 12 kHz, custom titles, gray boxes,
# and gray spectrogram colors
sp <- c('Pacific Wren', 'Pacific-slope Flycatcher')
for (i in 1:length(sp)) {
 plot.sp <- exampleSpectroData[common_name == sp[i] & confidence >= 0.25]
 birdnet_spectro(data = plot.sp,
    audio.directory = 'example-audio-directory',
    title = paste0(sp[i], ' Detections >= 0.25'),
    frq.lim = c(0.5, 12),
    new.window = TRUE,
    spec.col = gray.3(),
    box = TRUE,
    box.lwd = 0.5,
    box.col = 'gray',
    title.size = 1.5
 )
}

# Delete all temporary example files when finished
unlink(x = 'example-audio-directory', recursive = TRUE)


## End(Not run)


nationalparkservice/NSNSDAcoustics documentation built on March 4, 2025, 10:24 p.m.