Short-term Fourier transform using fftw and libsndfile C libraries

Share:

Description

This function performs the short-term Fourier transform externally, i. e. outside R, on an audio .wav file

Usage

1
2
stft.ext(file, wl = 512, ovlp = 0, mean = FALSE,
norm = FALSE, dB = FALSE, verbose = FALSE) 

Arguments

file

a character string vector of length 1, corresponding to the audio file name. Only .wav files are supported.

wl

a numeric vector of length 1, window length for the analysis (even number of points) (by default = 512).

ovlp

a numeric vector of length 1, overlap between two successive windows (in %).

mean

logical, if TRUE calculates the mean spectrum.

norm

logical, if TRUE the spectrogram matrix and the mean spectrum if mean is TRUE is/are normalized to a maximum of 1.

dB

logical, if TRUE the values are in deciBel with a maximum of 0.

verbose

logical, if TRUE displays audio file meta-information.

Value

If mean is FALSE, the function returns a two-column matrix corresponding to the amplitude values of the spectrogram. Each column is a discrete Fourier transform of length wl.

If mean is TRUE, the function returns a list with two elements:

mean

a vector of length wl/2 corresponding to the mean spectrum


amp

the spectrogram matrix.

Note

This function was developped to speed up the process of computing a spectrogram or a mean spectrum. The function should be faster than spectro and meanspec respectively as it does not import the .wav file into R and uses C fftw3 (http://www.fftw.org/) and libsndfile (http://www.mega-nerd.com/libsndfile/) libraries. The function is typically made for batch processing or long files. Gain in process time is significant for files > 10 minutes and for more thant 1000 files lasting one second.
A Hanning window shape is used by default.

Author(s)

Alicia Stotz

See Also

spectro, meanspec, fft.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# tico data
data(tico)
# write a local .wav file
savewav(tico, file="tico.wav")
# spectrogram - not normalised - linear scale - meta-information returned
res <- stft.ext(file="tico.wav", verbose=TRUE)
# spectrogram - normalised - linear scale - no meta-information
res <- stft.ext(file="tico.wav", norm=TRUE)
# spectrogram - dB scale - no meta-information
res <- stft.ext(file="tico.wav", dB=TRUE)
# see how it looks like (no scale)
filled.contour(t(res))
# spectrogram and mean spectrum - normalised - linear scale
res <- stft.ext(file="tico.wav", norm = TRUE, mean = TRUE)
# remove .wav file
unlink("tico.wav")

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.