bandpass: Bandpass/stop filters

View source: R/filters.R

bandpassR Documentation

Bandpass/stop filters


Filtering in the frequency domain with FFT-iFFT: low-pass, high-pass, bandpass, and bandstop filters. Similar to ffilter, but here we use FFT instead of STFT - that is, the entire sound is processed at once. This works best for relatively short sounds (seconds), but gives us maximum precision (e.g., for precise notch filtering) and doesn't affect the attack and decay. NAs are accepted and can be interpolated or preserved in the output. Because we don't do STFT, arbitrarily short vectors are also fine as input - for example, we can apply a low-pass filter prior to decimation when changing the sampling rate without aliasing. Note that, unlike pitchSmoothPraat, bandpass applies an abrupt cutoff instead of a smooth gaussian filter.


  samplingRate = NULL,
  lwr = NULL,
  upr = NULL,
  action = c("pass", "stop")[1],
  dB = Inf,
  na.rm = TRUE,
  from = NULL,
  to = NULL,
  normalize = FALSE,
  reportEvery = NULL,
  cores = 1,
  saveAudio = NULL,
  plot = FALSE,
  savePlots = NULL,
  width = 900,
  height = 500,
  units = "px",
  res = NA,



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


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

lwr, upr

cutoff frequencies, Hz. Specifying just lwr gives a high-pass filter, just upr low-pass filter with action = 'pass' (or vice versa with action = 'stop'). Specifying both lwr and upr a bandpass/bandstop filter, depending on 'action'


"pass" = preserve the selected frequency range (bandpass), "stop" = remove the selected frequency range (bandstop)


a positive number giving the strength of effect in dB (defaults to Inf - complete removal of selected frequencies)


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

from, to

if NULL (default), analyzes the whole sound, otherwise (s)


if TRUE, resets the output to the original scale (otherwise filtering often reduces the amplitude)


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


number of cores for parallel processing


full path to the folder in which to save the processed audio


should a spectrogram be plotted? TRUE / FALSE


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

width, height, units, res

graphical parameters for saving plots passed to png


other graphical parameters


Algorithm: fill in NAs with constant interpolation at the edges and linear interpolation in the middle; perform FFT; set the frequency ranges to be filtered out to 0; perform inverse FFT; set to the original scale; put the NAs back in.


# Filter white noise
s1 = fade(c(runif(2000, -1, 1)), samplingRate = 16000)
bandpass(s1, 16000, upr = 2000, plot = TRUE)    # low-pass
bandpass(s1, 16000, lwr = 2000, dB = 40, plot = TRUE)  # high-pass by 40 dB
bandpass(s1, 16000, lwr = 1000, upr = 1100, action = 'stop', plot = TRUE) # bandstop
s2 = bandpass(s1, 16000, lwr = 2000, upr = 2100, plot = TRUE) # bandpass
# playme(rep(s2, 5))
spectrogram(s2, 16000)  # more accurate than plotting the spectrum with plot = TRUE

# a short vector with some NAs
x = rnorm(150, 10) + 3 * sin((1:50) / 5)
x[sample(1:length(x), 50)] = NA
plot(x, type = 'l')
points(bandpass(x, samplingRate = 100, upr = 10), type = 'l', col = 'blue')

## Not run: 
# precise notch filtering is possible, even in low frequencies
whiteNoise = runif(8000, -1, 1)
s3 = bandpass(whiteNoise, 16000, lwr = 30, upr = 40,
              plot = TRUE, xlim = c(0, 500))
playme(rep(s3, 5))
spectrogram(s3, 16000, windowLength = 150, yScale = 'log')

# compare the same with STFT
s4 = seewave::ffilter(whiteNoise, f = 16000, from = 30, to = 40)
spectrogram(s4, 16000, windowLength = 150, yScale = 'log')
# (note: works better as wl approaches length(s4))

# high-pass all audio files in a folder
bandpass('~/Downloads/temp', saveAudio = '~/Downloads/temp/hp2000/',
         lwr = 2000, savePlots = '~/Downloads/temp/hp2000/')

## End(Not run)

soundgen documentation built on Aug. 14, 2022, 5:05 p.m.