Skip to contents

This function generates a binary spectrogram from an audio wave object. The spectrogram is calculated using the Fast Fourier Transform (FFT) and then converted into a binary matrix based on a specified dB cutoff. Optionally, the function can plot the binary spectrogram.

Usage

spectrogram_binary(
  wave,
  channel = "left",
  freq.res = 100,
  cutoff = -50,
  plot = FALSE,
  plot.title = NULL,
  ggplot = FALSE,
  verbose = FALSE
)

Arguments

wave

A Wave object from the tuneR package representing the audio signal.

channel

A character string indicating which channel to use for stereo audio. Options are 'left', 'right', or 'mix' (for combining both channels). If NULL, the left channel is used by default.

freq.res

A numeric value specifying the frequency resolution (in Hz) of the spectrogram. Default is 100 Hz.

cutoff

A numeric value specifying the dB cutoff threshold for the binary transformation. Amplitudes below this value will be set to 0. Default is -50 dB.

plot

A logical value indicating whether to plot the binary spectrogram. Default is TRUE.

plot.title

An optional character string to use as the title of the plot. Default is NULL.

ggplot

A logical value indicating whether to use ggplot2 to plot the spectrogram. If FALSE (default), R's base plot function is used instead, rendering much faster.

verbose

A logical value indicating whether to print additional information (such as the amplitude range and cutoff) to the console. Default is FALSE.

Value

Returns a binary matrix representing the spectrogram, where 1 represents amplitude above the cutoff and 0 represents amplitude below the cutoff.

Examples

if (FALSE) { # \dontrun{
  # Load a wave file
  wave <- tuneR::readWave("path_to_file.wav")

  # Generate and plot a binary spectrogram
  binary_spec <- spectrogram_binary(wave, cutoff = -40, verbose = TRUE)
} # }