winFun: Windowing functions for the Fourier transform

View source: R/windowFunctions.R

winFunR Documentation

Windowing functions for the Fourier transform

Description

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.

Usage

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)

Arguments

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):

character string

name of a predefined window

function

a function that takes n and returns a window vector, e.g. a constructor such as winFun_gauss(0.3) or a user-defined function

numeric vector

a user-supplied window of length n

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. sigma = 0.3 for "gaussian" (see "Predefined windows" for per-window arguments). Used only when wn is a character string; when wn is a constructor function, parameters are passed to the constructor directly. Do not pass graphical arguments here.

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 1/\sqrt{24} \approx 0.204, matching the Praat/seewave Gaussian window.

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. beta = 0 gives a rectangular window; beta = 5 is similar to Hamming; beta = 8.6 is similar to Blackman. Defaults to 5.

Value

A numeric vector of length n with values typically between 0 and 1 for normalize = "none" (or user-supplied values).

Predefined windows

"rectangle"

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" / "hanning"

Hann window (raised cosine). Smooth ends, moderate sidelobe roll-off (-31.5 dB first sidelobe). Standard for general audio work.

"hamming"

Hamming window. Almost cancels the first sidelobe of the Hann window (-43 dB). Popular in speech processing.

"blackman"

Blackman window (3-term cosine sum). Lower sidelobes (-58 dB) than Hann/Hamming, slightly wider main lobe.

"flattop"

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"

Bartlett (triangular) window – linearly tapering to zero. Simple, reasonably low sidelobes (-25 dB).

"welch"

Welch (parabolic) window. Tapers to zero smoothly, sidelobe fall-off is asymptotic, close to sine window.

"parzen"

Parzen window (cubic spline). Very smooth, continuous second derivative, excellent sidelobe roll-off. Common in non-parametric spectral density estimation.

"bspline"

Cubic B-spline window (order 4). Extremely smooth; good when sidelobe structure must be suppressed heavily.

"bohman"

Bohman window (time-domain convolution of two half-cosines). Sidelobes decay as 1/f^3, flat at zero, very low leakage.

"blackmanharris"

Blackman–Harris 4-term window (minimum sidelobe design). Very low sidelobes (-92 dB), suitable when dynamic range is important.

"nuttall"

Nuttall 4-term window (symmetric, continuous first derivative). Slightly better sidelobes than Blackman–Harris in some metrics (-93 dB).

"barthannwin"

Bartlett–Hann window (linear + cosine taper). Zero at edges, good compromise between Bartlett and Hann.

"sine" or "cosine"

Half-cycle sine window \sin(\pi k/(n-1)). Simple, zero at edges, often used in audio coding (e.g., MP3).

"lanczos"

Lanczos window (main lobe of \mathrm{sinc}). Smooth, zero at edges, used in interpolation and image resampling.

"gaussian"

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"

Tukey (tapered cosine) window. "fraction" = fraction of the window devoted to cosine tapering (0 = rectangular, 1 = Hann). Constructor: winFun_tukey(fraction).

"poisson"

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"

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"

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).

Examples

# "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')

soundgen documentation built on Sept. 20, 2026, 5:07 p.m.