Skip to contents

Apply gamma-tone filters to obtain auditory envelopes

Usage

gammatone_fast(
  x,
  sample_rate,
  center_frequencies,
  n_bands,
  use_hilbert = TRUE,
  downsample = NA
)

Arguments

x

a numeric vector or matrix; if x is a matrix, it should be column-major (each column is a sound track)

sample_rate

sampling frequency

center_frequencies

center frequencies at which the envelopes will be derived; can be either a length of two defining the lower and upper bound, and using n_bands to interpolate automatically, or a length of multiple, with the frequencies specified explicitly

n_bands

number of the center frequencies, can be missing if center_frequencies is explicit and no interpolation is needed; if specified, then the frequencies will be interpolated using equivalent rectangular bandwidth rate ('ERB')

use_hilbert

whether to apple 'Hilbert' transform; default is true, which calculates the magnitude; set to false when only the filter is needed

downsample

whether to down-sample the envelopes after the filters; default is NA (no down-sample). It is recommended when the signal sampling frequency is high to save time.

Value

A file-array object of filtered and potentially down-sampled data; see 'Examples' on how to use this function.

Examples



fs <- 2000
time <- seq_len(4000) / fs
x <- sin(160 * pi * time) +
  sin(1000 * pi * time) * dnorm(time, mean = 1, sd = 0.1) +
  0.5 * rnorm(length(time))

# envelope
result <- gammatone_fast(
  x,
  sample_rate = fs,
  center_frequencies = c(20, 1000),
  n_bands = 128,
  downsample = 20
)


oldpar <- par(mfrow = c(2, 1))

plot(
  time,
  x,
  type = "l",
  xlab = "Time",
  ylab = "",
  main = "Original mixed 80Hz and 500Hz"
)

# only one channel
envelope <- subset(result, Channel ~ Channel == 1, drop = TRUE)
dnames <-  dimnames(envelope)
image(
  x = as.numeric(dnames$Time),
  y = as.numeric(dnames$Frequency),
  z = envelope,
  xlab = "Time",
  ylab = "Frequency",
  main = "Envelope from 20Hz to 1000Hz"
)


par(oldpar) # reset graphics state