Plot spectrograms to check test sound files alignment
Source:R/plot_aligned_sounds.R
plot_aligned_sounds.Rdplot_aligned_sounds() plots spectrograms to visually inspect
alignment precision on test sound files.
Usage
plot_aligned_sounds(
X,
hop.size = getOption("hop.size", 11.6),
wl = getOption("wl", NULL),
ovlp = getOption("ovlp", 50),
path = getOption("sound.files.path", "."),
cores = getOption("mc.cores", 1),
pb = getOption("pb", TRUE),
collevels = seq(-120, 0, 5),
palette = viridis::viridis,
duration = 2,
mar = 0.2,
dest.path = getOption("dest.path", "."),
flim = NULL,
col = "white",
width = 7,
height = 4,
res = 100,
label = TRUE,
fast.spec = FALSE,
srt = 0,
cex = 1,
...
)Arguments
- X
Object of class
data.frame,selection_table, orextended_selection_table(the last 2 classes are created bywarbleR::selection_table()from the warbleR package) with the test sound files' annotations. Must contain the following columns: 1)sound.files: name of the.wavfiles, 2)selec: unique selection identifier (within a sound file), 3)start: start time and 4)end: end time of selections, 5)bottom.freq: low frequency for bandpass, 6)top.freq: high frequency for bandpass, and 7)sound.id: ID of sounds used to identify counterparts across distances. Each sound must have a unique ID within a distance.- hop.size
Numeric vector of length 1 specifying the time window duration (in ms). Default
11.6ms, which is equivalent to 512wlfor a 44.1 kHz sampling rate. Ignored ifwlis supplied. Can be set globally for the current R session via the"hop.size"option (seeoptions()).- wl
Numeric vector of length 1 specifying the window length of the spectrogram. Default
NULL. Ignored ifbp = NULL. If supplied,hop.sizeis ignored.- ovlp
Numeric vector of length 1 specifying the percentage of overlap between two consecutive windows, as in
seewave::spectro(). Default0. Can be set globally for the current R session via the"ovlp"option (seeoptions()).- path
Character string containing the directory path where the sound files are found. Only needed when
Xis not an extended selection table. If not supplied the current working directory is used. Can be set globally for the current R session via the"sound.files.path"option (seeoptions()).- cores
Numeric vector of length 1. Controls whether parallel computing is applied by specifying the number of cores to be used. Default
1(i.e. no parallel computing). Can be set globally for the current R session via the"mc.cores"option (seeoptions()).- pb
Logical argument to control if progress bar is shown. Default
TRUE. Can be set globally for the current R session via the"pb"option (seeoptions()).- collevels
Numeric vector of length 3. Specifies levels to partition the amplitude range of the spectrogram (in dB). The more levels, the higher the resolution of the spectrogram. Default
seq(-40, 0, 1).seq(-115, 0, 1)will produce spectrograms similar to other acoustic analysis software packages.- palette
Color palette function for the spectrogram. Default
viridis::viridis(). Seeseewave::spectro()for more palettes. Palettes such asmonitoR::gray.2may work better whenfast.spec = TRUE.- duration
Numeric vector of length 1. Specifies the overall duration of the clip that will be plotted. Notice that only the initial part of the test files is plotted, as this is usually enough to tell the precision of the alignment.
- mar
Numeric vector of length 1. Specifies the margins adjacent to the start of the first annotation to be included in the plot.
- dest.path
Character string containing the directory path where the image files will be saved. If not supplied the current working directory will be used instead. Can be set globally for the current R session via the
"dest.path"option (seeoptions()).- flim
Numeric vector of length 2 indicating the highest and lowest frequency limits (kHz) of the spectrogram, as in
seewave::spectro(). DefaultNULL, which will plot spectrograms in the full frequency range (0 - nyquist frequency).- col
Character string controlling the color of lines and sound ID labels.
- width
Numeric vector of length 1. Single value (in inches) indicating the width of the output image files. Default
7.- height
Numeric vector of length 1. Single value (in inches) indicating the height of the output image files. Default
4.- res
Numeric argument of length 1. Controls image resolution. Default
100(faster), although 300-400 is recommended for publication/presentation quality.- label
Logical to control if labels (from the
sound.idcolumn inX) are plotted. DefaultTRUE.- fast.spec
Logical. If
TRUE, theimagefunction is used internally to create spectrograms, which substantially increases performance (much faster), although some options become unavailable, such ascollevels(amplitude scale). DefaultFALSE.- srt
Numeric argument of length 1. The rotation (in degrees) of the sound ID labels. Default
0.- cex
Numeric argument of length 1 controlling the size of sound ID text labels. Default
1.- ...
Additional arguments to be passed to the internal spectrogram-creating function for customizing graphical output. The function is a modified version of
seewave::spectro(), so it takes the same arguments.
Value
Image files in jpeg format with spectrograms in the working
directory, one for each sound file in X. It also returns the file
path of the images invisibly.
Details
This function aims to simplify the evaluation of the alignment of
test sound files from align_test_files(). The function creates a
single spectrogram for each sound file (saved at dest.path).
Spectrograms include the first few seconds of the sound files
(controlled by duration), which is usually enough to tell the
precision of the alignment. The plots include vertical lines
denoting the start and end of each sound, as well as the sound ID
(sound.id column in X). Note that no plot is created in the R
graphic device.
References
Araya-Salas, M., Grabarczyk, E. E., Quiroz-Oliva, M., Garcia-Rodriguez, A., & Rico-Guevara, A. (2025). Quantifying degradation in animal acoustic signals with the R package baRulho. Methods in Ecology and Evolution, 00, 1-12. https://doi.org/10.1111/2041-210X.14481
See also
manual_realign() and auto_realign(), for fixing
misalignments; find_markers() and align_test_files(), used
upstream to produce the input for this function.
Other test sound alignment:
align_test_files(),
auto_realign(),
find_markers(),
manual_realign()
Author
Marcelo Araya-Salas (marcelo.araya@ucr.ac.cr)