View source: R/windowFunctions.R
| winFun | R Documentation |
Generates an n-point window vector for the discrete Fourier transform and
short-time Fourier analysis. Windows control spectral leakage by tapering the
input signal, trading off main lobe width against sidelobe level. See the
signal-package for more windows and/or different
implementations. External and user-defined windowing functions can also be
passed to all soundgen functions that call winFun - see examples.
winFun(n, wn, normalize = c("none", "amplitude", "energy"), ...)
winFun_tukey(fraction = 0.5)
winFun_gauss(sigma = 1/sqrt(24))
winFun_poisson(alpha = 1)
winFun_cauchy(alpha = 1)
winFun_kaiser(beta = 5)
n |
window length in samples (integer >= 3) |
wn |
window type (strings and functions can be passed to other soundgen functions that have a "wn" argument):
|
normalize |
"none" = no normalization; "amplitude" = divide by sum of weights; "energy" = divide by sum of squared weights |
... |
extra arguments specific to parameterized windows, e.g.
|
fraction |
(winFun_tukey) fraction of the window that is tapered (0 = rectangular, 1 = Hann). Defaults to 0.5. |
sigma |
(winFun_gauss) standard deviation in units of half-window
(positive). Defaults to |
alpha |
(winFun_poisson, winFun_cauchy) shape parameter controlling the width. Defaults to 1. |
beta |
(winFun_kaiser) shape parameter controlling the trade-off
between main lobe width and sidelobe level. |
A numeric vector of length n with values typically between 0 and 1
for normalize = "none" (or user-supplied values).
Rectangular (uniform) window – no tapering. Minimizes mean square error, narrowest main lobe (-13 dB sidelobe). Good for transients or when leakage is not critical.
Hann window (raised cosine). Smooth ends, moderate sidelobe roll-off (-31.5 dB first sidelobe). Standard for general audio work.
Hamming window. Almost cancels the first sidelobe of the Hann window (-43 dB). Popular in speech processing.
Blackman window (3-term cosine sum). Lower sidelobes (-58 dB) than Hann/Hamming, slightly wider main lobe.
Flat-top window (5-term cosine sum). Very wide main lobe but low sidelobes and excellent amplitude accuracy. Useful when precise amplitude measurement matters more than frequency resolution.
Bartlett (triangular) window – linearly tapering to zero. Simple, reasonably low sidelobes (-25 dB).
Welch (parabolic) window. Tapers to zero smoothly, sidelobe fall-off is asymptotic, close to sine window.
Parzen window (cubic spline). Very smooth, continuous second derivative, excellent sidelobe roll-off. Common in non-parametric spectral density estimation.
Cubic B-spline window (order 4). Extremely smooth; good when sidelobe structure must be suppressed heavily.
Bohman window (time-domain convolution of two
half-cosines). Sidelobes decay as 1/f^3, flat at zero, very low
leakage.
Blackman–Harris 4-term window (minimum sidelobe design). Very low sidelobes (-92 dB), suitable when dynamic range is important.
Nuttall 4-term window (symmetric, continuous first derivative). Slightly better sidelobes than Blackman–Harris in some metrics (-93 dB).
Bartlett–Hann window (linear + cosine taper). Zero at edges, good compromise between Bartlett and Hann.
Half-cycle sine window
\sin(\pi k/(n-1)). Simple, zero at edges, often used in audio coding
(e.g., MP3).
Lanczos window (main lobe of \mathrm{sinc}).
Smooth, zero at edges, used in interpolation and image resampling.
Gaussian window. Minimizes time–frequency uncertainty;
never quite reaches zero at the edges. The default sigma =
1/sqrt(24) (~= 0.204) reproduces the Praat/seewave window.
Constructor: winFun_gauss(sigma).
Tukey (tapered cosine) window. "fraction" = fraction of
the window devoted to cosine tapering (0 = rectangular, 1 = Hann).
Constructor: winFun_tukey(fraction).
Poisson (exponential) window:
\exp(-\alpha\,|k - N/2|\,/\,(N/2)). Sharp central peak, quick decay.
Good for isolating fast transients. Constructor:
winFun_poisson(alpha).
Cauchy (Lorentzian) window: 1 / (1 +
(\alpha\,(k-N/2)/(N/2))^2). Smooth and heavy-tailed, with a parameter
controlling the width. Constructor: winFun_cauchy(alpha).
Kaiser window based on the modified Bessel function of
the first kind. "beta" controls the trade-off between main lobe width and
sidelobe level (0 = rectangular, ~5 ~= Hamming, ~8.6 ~= Blackman).
Constructor: winFun_kaiser(beta).
# "wn" as a character string
wns = c('rectangle', 'hann', 'hamming', 'blackman', 'flattop', 'bartlett',
'welch', 'parzen', 'bspline', 'bohman', 'blackmanharris', 'nuttall',
'barthannwin', 'sine', 'lanczos', 'gaussian', 'tukey', 'poisson',
'cauchy', 'kaiser')
op = par(c('mfrow', 'mar')); par(mfrow = c(5, 4), mar = c(0, 0, 3, 0))
for (w in wns)
plot(winFun(256, w), xlab='', ylab='', bty='n', xaxt='n', yaxt='n', main=w)
par(op)
# Passing window-specific parameters via ...
plot(winFun(256, 'gaussian', sigma = 0.2), main = 'Gaussian, sigma = 0.2')
plot(winFun(256, 'gaussian', sigma = 0.5), main = 'Gaussian, sigma = 0.5')
plot(winFun(256, 'tukey', fraction = 0.8), main = 'Tukey, fraction = 0.8')
plot(winFun(256, 'kaiser', beta = 8.6), main = 'Kaiser, beta = 8.6')
# Equivalent: passing a constructor (useful when forwarding through
# higher-level functions like meanSpectrum(), spectrogram(), etc.)
plot(winFun(256, winFun_gauss(0.2)), main = 'Gaussian, sigma = 0.2')
plot(winFun(256, winFun_tukey(0.8)), main = 'Tukey, fraction = 0.8')
plot(winFun(256, winFun_kaiser(8.6)), main = 'Kaiser, beta = 8.6')
# use "wn" in other soundgen functions:
s = cos(2 * pi * 440 * (1:2000) / 2000) + 0.3 +
cos(2 * pi * 880 * (1:2000) / 2000)
meanSpectrum(s, 2000, wn = 'blackman', yScale = 'dB')
meanSpectrum(s, 2000, wn = winFun_cauchy(1.2), yScale = 'dB')
meanSpectrum(s, 2000, wn = winFun_kaiser(8), yScale = 'dB')
# "wn" as a user-supplied function
plot(signal::kaiser(100, 4))
meanSpectrum(s, 2000, wn = function(n) signal::kaiser(n, 4), yScale = 'dB')
meanSpectrum(s, 2000, wn = function(n) signal::kaiser(n, 16), yScale = 'dB')
plot(winFun(256, signal::chebwin(256, 80)),
main = 'Dolph-Chebyshev, -80 dB sidelobes')
meanSpectrum(s, 2000, wn = function(n) signal::chebwin(n, 80), yScale = 'dB')
halfsine = function(n) sin(pi * (0:(n-1)) / (n-1))
plot(winFun(25, halfsine), main = 'Half-sine')
meanSpectrum(s, 2000, wn = halfsine, yScale = 'dB')
# "wn" as a user-supplied numeric vector
custom = sin(pi * (0:255) / 255) # half sine again
plot(winFun(256, custom), main = 'Half-sine')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.