Learn R Programming

soundgen (version 2.4.0)

resample: Resample a vector

Description

Changes the sampling rate without introducing artefacts like aliasing. Algorithm: to downsample, applies a low-pass filter, then decimates with approx; to upsample, performs linear interpolation with approx, then applies a low-pass filter. NAs can be interpolated or preserved in the output. The length of output is determined, in order of precedence, by len / mult / samplingRate_new.

Usage

resample(
  x,
  samplingRate = NULL,
  samplingRate_new = NULL,
  mult = NULL,
  len = NULL,
  lowPass = TRUE,
  na.rm = FALSE,
  reportEvery = NULL,
  saveAudio = NULL,
  plot = FALSE,
  savePlots = NULL,
  width = 900,
  height = 500,
  units = "px",
  res = NA,
  ...
)

Arguments

x

path to a folder, one or more wav or mp3 files c('file1.wav', 'file2.mp3'), Wave object, numeric vector, or a list of Wave objects or numeric vectors

samplingRate

sampling rate of x (only needed if x is a numeric vector)

samplingRate_new

an alternative to mult provided that the old samplingRate is know (NB: mult takes precedence)

mult

multiplier of sampling rate: new sampling rate = old sampling rate x mult, so 1 = no effect, >1 = upsample, <1 = downsample

len

if specified, overrides mult and samplingRate_new and simply returns a vector of length len

lowPass

if TRUE, applies a low-pass filter before decimating or after upsampling to avoid aliasing

na.rm

if TRUE, NAs are interpolated, otherwise they are preserved in the output

reportEvery

when processing multiple inputs, report estimated time left every ... iterations (NULL = default, NA = don't report)

saveAudio

full path to the folder in which to save audio files (one per detected syllable)

plot

should a spectrogram be plotted? TRUE / FALSE

savePlots

full path to the folder in which to save the plots (NULL = don't save, '' = same folder as audio)

width

graphical parameters for saving plots passed to png

height

graphical parameters for saving plots passed to png

units

graphical parameters for saving plots passed to png

res

graphical parameters for saving plots passed to png

...

other graphical parameters

Examples

Run this code
# NOT RUN {
## Example 1: a short vector with NAs
x = c(NA, 1, 2, 3, NA, NA, 6, 7, 8, NA)

# upsample
soundgen::resample(x, mult = 3.5, lowPass = FALSE, plot = TRUE)  # just approx
soundgen::resample(x, mult = 3.5, lowPass = TRUE, plot = TRUE) # low-pass + approx
soundgen::resample(x, mult = 3.5, lowPass = FALSE, na.rm = TRUE, plot = TRUE)

# downsample
soundgen::resample(x, mult = 0.5, lowPass = TRUE, plot = TRUE)
soundgen::resample(x, mult = 0.5, na.rm = TRUE, plot = TRUE)
soundgen::resample(x, len = 5, na.rm = TRUE, plot = TRUE) # same


## Example 2: a sound
silence = rep(0, 10)
samplingRate = 1000
fr = seq(100, 300, length.out = 400)
x = c(silence, sin(cumsum(fr) * 2 * pi / samplingRate), silence)
spectrogram(x, samplingRate)

# downsample
x1 = soundgen:::resample(x, mult = 1 / 2.5)
spectrogram(x1, samplingRate / 2.5)  # no aliasing
# cf:
x1bad = soundgen:::resample(x, mult = 1 / 2.5, lowPass = FALSE)
spectrogram(x1bad, samplingRate / 2.5)  # aliasing

# upsample
x2 = soundgen:::resample(x, mult = 3)
spectrogram(x2, samplingRate * 3)  # nothing above the old Nyquist
# cf:
x2bad = soundgen:::resample(x, mult = 3, lowPass = FALSE)
spectrogram(x2bad, samplingRate * 3)  # high-frequency artefacts

# }
# NOT RUN {
# Example 3: resample all audio files in a folder to 8000 Hz
resample('~/Downloads/temp', saveAudio = '~/Downloads/temp/sr8000/',
         samplingRate_new = 8000, savePlots = '~/Downloads/temp/sr8000/')
# }

Run the code above in your browser using DataLab