| spectrum | R Documentation |
spectrum computes the frequency spectrum of a sound using the Fast
Fourier Transform (FFT). For a smoother appearance and faster processing of
long sounds, meanSpectrum computes the time-averaged spectrum
of successive windows. See the "spec" and "meanspec" functions in the seewave
package for more plotting options. NB: soundgen::spectrum() masks
stats::spectrum().
spectrum(
x,
samplingRate = NULL,
from = NULL,
to = NULL,
zp = NULL,
yScale = c("linear", "power", "dB", "max0"),
plot = TRUE,
savePlots = FALSE,
embed = FALSE,
main = NULL,
xlab = NULL,
ylab = NULL,
width = 900,
height = 500,
units = "px",
res = NA,
reportEvery = NULL,
cores = 1,
...
)
meanSpectrum(
x,
samplingRate = NULL,
from = NULL,
to = NULL,
windowLength = 50,
step = windowLength/2,
overlap = NULL,
wn = "gaussian",
zp = NULL,
reportEvery = NULL,
cores = 1,
plot = TRUE,
yScale = c("linear", "power", "dB", "max0"),
savePlots = FALSE,
embed = FALSE,
main = NULL,
xlab = NULL,
ylab = NULL,
width = 900,
height = 500,
units = "px",
res = NA,
...
)
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 |
from, to |
if NULL (default), analyzes the whole sound, otherwise from...to (s) |
zp |
window length after zero padding, samples. No padding is performed
if |
yScale |
scale of the y-axis: |
plot |
if TRUE, plots the spectrum |
savePlots |
if TRUE, creates a subdirectory in the input directory (if input is a file or folder) or in the working directory (if input is a vector etc), named after the function (eg "spectrogram/"). All plots and audio files (if any) are saved in this new directory. If there are multiple inputs, an html notebook is also created for easy viewing and listening |
embed |
if TRUE and savePlots is set and there are multiple inputs, all saved images and audio (if any) are embedded in the exported html notebook for easy sharing; if FALSE, the html file links to separate images and audio files (but separate files are still saved). NB: for this to work, package "base64enc" must be installed |
xlab, ylab, main |
graphical parameters for plotting |
width, height, units, res |
graphical parameters for saving plots passed to
|
reportEvery |
when processing multiple inputs, report estimated time
left every |
cores |
number of cores for parallel processing |
... |
other graphical parameters passed to |
windowLength |
length of the analysis window, ms |
step |
step between successive windows, ms; if provided, overrides
|
overlap |
overlap between successive windows, % |
wn |
window type accepted by |
A dataframe with two columns: freq (frequency in kHz) and
ampl (amplitude, in units determined by yScale).
# 500 Hz tone
sound = cos(2 * pi * 500 * (1:4000) / 16000) + rnorm(4000, 0, .05)
# Spectrum on linear scale
spectrum(sound, samplingRate = 16000, yScale = 'linear')
meanSpectrum(sound, samplingRate = 16000, yScale = 'linear')
# dB scale with custom labels
spectrum(sound, samplingRate = 16000, yScale = 'dB', col = 'blue',
xlab = 'Frequency (kHz)', ylab = 'Amplitude (dB)')
# max0 scale with custom y-limits and extra graphical pars
meanSpectrum(sound, samplingRate = 16000, yScale = 'max0',
xlim = c(0, 2), ylim = c(-50, 5), lty = 2, lwd = 3, col = 'blue')
# Return data without plotting
ms = meanSpectrum(sound, samplingRate = 16000, plot = FALSE)
head(ms)
# If windowLength is longer than the sound, meanSpectrum() = spectrum()
spectrum(sound, 16000)
meanSpectrum(sound, 16000, windowLength = 5000)
## Not run:
# Process all .wav files in a folder
spectrum('~/Downloads/temp', savePlots = TRUE, yScale = 'dB')
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.