window

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2026 License: MIT Imports: 7 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package window computes window functions, matching scipy.signal.windows, using only the standard library. It has every SciPy window except dpss, and is checked against SciPy and MATLAB, which the tables in docs/accuracy.md measure.

w, err := window.Hann(1024, window.Periodic()) // for spectral analysis
h, err := window.Hamming(101)                  // for filter design

Each window returns n samples: symmetric by default, for filter design, or periodic, DFT-even, with Periodic, for spectral analysis.

Windows

tukey                SciPy                    MATLAB
Hann                 hann                     hann
Hamming              hamming                  hamming
Blackman             blackman                 blackman
BlackmanHarris       blackmanharris           blackmanharris
Nuttall              nuttall                  nuttallwin
FlatTop              flattop                  flattopwin
GeneralHamming       general_hamming
GeneralCosine        general_cosine
Boxcar               boxcar                   rectwin
Triang               triang                   triang
Bartlett             bartlett                 bartlett
Parzen               parzen                   parzenwin
Bohman               bohman                   bohmanwin
Barthann             barthann                 barthannwin
Tukey                tukey                    tukeywin
Cosine               cosine
Lanczos              lanczos
Gaussian             gaussian                 gausswin
GeneralGaussian      general_gaussian
Exponential          exponential
Kaiser               kaiser                   kaiser
KaiserBesselDerived  kaiser_bessel_derived
Taylor               taylor                   taylorwin
Chebwin              chebwin                  chebwin

Conventions

Every function is stateless: a new slice comes back, inputs are never modified, and all functions are safe for concurrent use. Invalid input returns an error wrapping ErrInvalidLength, ErrInvalidParameter or ErrInvalidOption, so check for one with errors.Is.

A NaN or infinite parameter returns ErrInvalidParameter. The one exception is Tukey's alpha, where at or below 0 is Boxcar and at or above 1 is Hann, infinities included. For a flat window, use Boxcar rather than an infinite std or tau.

Options

One Option type serves every window, and a window given an option that doesn't apply to it returns ErrInvalidOption:

  • Periodic gives the symmetric window of n+1 points without its last point, for every window but KaiserBesselDerived. It's SciPy's sym=False and MATLAB's 'periodic'.
  • Center moves Exponential's peak, with Periodic only.
  • Unnormalised leaves Taylor's peak where its formula puts it, rather than at 1. It's SciPy's norm=False, and MATLAB's taylorwin.

Where tukey differs

  • symmetric windows are computed for their first half and mirrored, so they're exactly symmetric, where SciPy's are symmetric to within a few ULP
  • MATLAB's rectwin, triang, bartlett, parzenwin, bohmanwin, barthannwin, tukeywin, gausswin, kaiser, taylorwin and chebwin have no periodic form
  • MATLAB's hanning is Hann without its zero ends, and Hann's doc shows how to build it
  • Gaussian takes the standard deviation in samples, same as SciPy, and MATLAB's gausswin(n, alpha) is Gaussian(n, (n-1)/(2*alpha))
  • Taylor takes its sidelobe level as positive dB, same as SciPy, and MATLAB's taylorwin(n, nbar, -sll) is Taylor(n, nbar, sll, Unnormalised()) for n > 1
  • every window is [1] at n = 1, same as SciPy, where MATLAB's taylorwin evaluates its formula
  • Tukey's alpha is unrestricted, same as SciPy and MATLAB, so at or below 0 is Boxcar, and at or above 1 is Hann
  • parameters with no meaning outside their range are checked where SciPy doesn't check them: std, p, sig, tau, sll and attenuation must be positive and finite, nbar at least 1, Kaiser's beta and the cosine sums' coefficients finite, and KaiserBesselDerived's n even, so n = 1 is an error rather than [1]
  • Chebwin below about 45 dB isn't suitable for spectral analysis, as SciPy warns

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidLength is a length that's wrong in itself.
	ErrInvalidLength = errors.New("window: invalid length")
	// ErrInvalidParameter is a value outside its domain.
	ErrInvalidParameter = errors.New("window: invalid parameter")
	// ErrInvalidOption is an option that's unknown or doesn't apply.
	ErrInvalidOption = errors.New("window: invalid option")
)

Errors returned for invalid input. Each is wrapped with the detail of what was wrong, so check for one with errors.Is. They mean the same in every tukey package.

Functions

func Barthann

func Barthann(n int, opts ...Option) (w []float64, err error)

Barthann returns the n-point modified Bartlett-Hann window: a weighted combination of Bartlett and Hann.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.barthann(n)
  • MATLAB barthannwin(n), which has no periodic form

func Bartlett

func Bartlett(n int, opts ...Option) (w []float64, err error)

Bartlett returns the n-point Bartlett window, a triangle that's 0 at both ends.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.bartlett(n)
  • MATLAB bartlett(n), which has no periodic form

func Blackman

func Blackman(n int, opts ...Option) (w []float64, err error)

Blackman returns the n-point Blackman window: three cosine terms, zero at both ends.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.blackman(n)
  • MATLAB blackman(n)

func BlackmanHarris

func BlackmanHarris(n int, opts ...Option) (w []float64, err error)

BlackmanHarris returns the n-point minimum 4-term Blackman-Harris window, whose sidelobes are about 92 dB down.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.blackmanharris(n)
  • MATLAB blackmanharris(n)

func Bohman

func Bohman(n int, opts ...Option) (w []float64, err error)

Bohman returns the n-point Bohman window: a triangle multiplied by a cosine lobe, plus a sine term that makes its first derivative continuous.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.bohman(n)
  • MATLAB bohmanwin(n), which has no periodic form

func Boxcar

func Boxcar(n int, opts ...Option) (w []float64, err error)

Boxcar returns the n-point rectangular window, all ones.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.boxcar(n)
  • MATLAB rectwin(n), which has no periodic form

func Chebwin

func Chebwin(n int, attenuation float64, opts ...Option) (w []float64, err error)

Chebwin returns the n-point Dolph-Chebyshev window: for its main-lobe width it has the lowest possible sidelobes, all equal, at attenuation dB below the main lobe. It's normalised so its peak is 1. SciPy warns that below about 45 dB it isn't suitable for spectral analysis.

w, err := window.Chebwin(64, 100)

Units:

  • n is a number of samples
  • attenuation is in dB, positive
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidParameter for an attenuation that isn't positive and finite.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.chebwin(n, attenuation)
  • MATLAB chebwin(n, attenuation), which has no periodic form

func Cosine

func Cosine(n int, opts ...Option) (w []float64, err error)

Cosine returns the n-point cosine, or sine, window: half a period of a sine, not reaching 0 at its ends.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.cosine(n)
  • MATLAB has none

func Exponential

func Exponential(n int, tau float64, opts ...Option) (w []float64, err error)

Exponential returns the n-point exponential, or Poisson, window:

w(x) = e^(-|x|/tau), for x samples from its peak

The peak is in the middle, unless Center moves it. It's the usual force and response window in impact testing.

w, err := window.Exponential(1024, 200, window.Periodic(), window.Center(0)) // decaying from the first sample

Units:

  • n is a number of samples
  • tau is in samples, the distance over which w falls by a factor of e
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidParameter for a tau that isn't positive and finite, or a NaN or infinite Center.
  • ErrInvalidOption for an option other than Periodic and Center, or Center without Periodic.

Equivalent to:

  • scipy.signal.windows.exponential(n, center, tau)
  • MATLAB has none

func FlatTop

func FlatTop(n int, opts ...Option) (w []float64, err error)

FlatTop returns the n-point flat top window, for measuring a sine's amplitude. Its passband is flat to within a fraction of a dB, so a sine between two bins reads its true height. It dips below 0, and peaks at 1.000000003, the sum of its published coefficients.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.flattop(n)
  • MATLAB flattopwin(n)

func Gaussian

func Gaussian(n int, std float64, opts ...Option) (w []float64, err error)

Gaussian returns the n-point Gaussian window with standard deviation std:

w(x) = e^(-x²/(2·std²)), for x samples from the centre

Units:

  • n is a number of samples
  • std is in samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidParameter for a std that isn't positive and finite.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.gaussian(n, std)
  • MATLAB gausswin(n, (n-1)/(2*std)), which has no periodic form

func GeneralCosine

func GeneralCosine(n int, a []float64, opts ...Option) (w []float64, err error)

GeneralCosine returns the n-point cosine-sum window with coefficients a:

w(x) = Σ a[j]·cos(j·x), for x evenly spaced from -π to π

It isn't normalised, so its peak is the sum of a, same as SciPy's. Hann, Hamming, Blackman, BlackmanHarris, Nuttall and FlatTop are all cosine sums.

w, err := window.GeneralCosine(64, []float64{1, 1.942604, 1.340318, 0.440811, 0.043097}) // HFT90D

Units:

  • n is a number of samples
  • a and w are dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidParameter for an empty a, or one with a NaN or infinite coefficient.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.general_cosine(n, a)
  • MATLAB has none

func GeneralGaussian

func GeneralGaussian(n int, p, sig float64, opts ...Option) (w []float64, err error)

GeneralGaussian returns the n-point generalised Gaussian window:

w(x) = e^(-|x/sig|^(2p)/2), for x samples from the centre

p = 1 is Gaussian with std sig, and a larger p flattens the top.

Units:

  • n is a number of samples
  • p is dimensionless
  • sig is in samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidParameter for a p or sig that isn't positive and finite.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.general_gaussian(n, p, sig)
  • MATLAB has none

func GeneralHamming

func GeneralHamming(n int, alpha float64, opts ...Option) (w []float64, err error)

GeneralHamming returns the n-point generalised Hamming window:

w[k] = alpha - (1 - alpha)·cos(2π·k/(n - 1))

alpha 0.5 is Hann, and 0.54 is Hamming.

Units:

  • n is a number of samples
  • alpha and w are dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidParameter for a NaN or infinite alpha.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.general_hamming(n, alpha)
  • MATLAB has none

func Hamming

func Hamming(n int, opts ...Option) (w []float64, err error)

Hamming returns the n-point Hamming window: a raised cosine on a pedestal of 0.08.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.hamming(n)
  • MATLAB hamming(n)

func Hann

func Hann(n int, opts ...Option) (w []float64, err error)

Hann returns the n-point Hann window: a raised cosine, zero at both ends, or only at the start when periodic.

w, err := window.Hann(64, window.Periodic()) // for spectral analysis

MATLAB's hanning(n) is Hann without its zero ends, Hann(n+2) less its first and last sample:

w, err := window.Hann(n + 2)
w = w[1 : n+1]

MATLAB's hanning(n, 'periodic') is Hann(n, Periodic()).

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.hann(n), and numpy.hanning(n)
  • MATLAB hann(n)
Example
package main

import (
	"fmt"
	"log"

	"github.com/cxd309/tukey/window"
)

func main() {
	// symmetric, for filter design: zero at both ends
	symmetric, err := window.Hann(5)
	if err != nil {
		log.Fatal(err)
	}
	// periodic, for spectral analysis: the symmetric window one longer, less its last point
	periodic, err := window.Hann(4, window.Periodic())
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("symmetric:", symmetric)
	fmt.Println("periodic: ", periodic)
}
Output:
symmetric: [0 0.5 1 0.5 0]
periodic:  [0 0.5 1 0.5]

func Kaiser

func Kaiser(n int, beta float64, opts ...Option) (w []float64, err error)

Kaiser returns the n-point Kaiser window with shape beta:

w(x) = I0(beta·√(1 - x²))/I0(beta), for x from -1 to 1

beta 0 is Boxcar, and a larger beta trades a wider main lobe for lower sidelobes: about 5 is like Hamming, 8.6 like Blackman, and 14 gives very low sidelobes. A negative beta is the same window as |beta|, as I0 is even.

w, err := window.Kaiser(64, 8.6)

Units:

  • n is a number of samples
  • beta and w are dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidParameter for a NaN or infinite beta.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.kaiser(n, beta)
  • MATLAB kaiser(n, beta), which has no periodic form

func KaiserBesselDerived

func KaiserBesselDerived(n int, beta float64, opts ...Option) (w []float64, err error)

KaiserBesselDerived returns the n-point Kaiser-Bessel-derived (KBD) window, the MDCT window of audio codecs such as AAC and Vorbis: the square root of the running sum of a Kaiser window, so that:

w[k]² + w[k + n/2]² = 1

which is the Princen-Bradley condition, for perfect reconstruction. It's defined only for even n, and only symmetric.

Units:

  • n is a number of samples
  • beta and w are dimensionless

Returns:

  • ErrInvalidLength for a negative or odd n, including 1.
  • ErrInvalidParameter for a NaN or infinite beta.
  • ErrInvalidOption for any option.

Equivalent to:

  • scipy.signal.windows.kaiser_bessel_derived(n, beta)
  • MATLAB has none

func Lanczos

func Lanczos(n int, opts ...Option) (w []float64, err error)

Lanczos returns the n-point Lanczos, or sinc, window: the central lobe of sin(π·x)/(π·x), for x from -1 to 1.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.lanczos(n)
  • MATLAB has none

func Nuttall

func Nuttall(n int, opts ...Option) (w []float64, err error)

Nuttall returns the n-point minimum 4-term Blackman-Harris window, in Nuttall's version.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.nuttall(n)
  • MATLAB nuttallwin(n)

func Parzen

func Parzen(n int, opts ...Option) (w []float64, err error)

Parzen returns the n-point Parzen window: a piecewise cubic, a triangle convolved with itself.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.parzen(n)
  • MATLAB parzenwin(n), which has no periodic form

func Taylor

func Taylor(n, nbar int, sll float64, opts ...Option) (w []float64, err error)

Taylor returns the n-point Taylor window, for radar and array processing: its first nbar-1 sidelobes are nearly level, at sll dB below the main lobe, and the rest fall away. It's normalised to 1 at the centre, unless Unnormalised. nbar = 1 is Boxcar.

w, err := window.Taylor(64, 4, 30)

Units:

  • n is a number of samples
  • nbar is a number of sidelobes
  • sll is in dB, positive
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidParameter for an nbar below 1, or an sll that isn't positive and finite.
  • ErrInvalidOption for an option other than Periodic and Unnormalised.

Equivalent to:

  • scipy.signal.windows.taylor(n, nbar, sll)
  • MATLAB taylorwin(n, nbar, -sll), with Unnormalised, for n > 1, which has no periodic form

func Triang

func Triang(n int, opts ...Option) (w []float64, err error)

Triang returns the n-point triangular window. Unlike Bartlett it doesn't reach 0, as its ends are 1/n, or 2/(n+1) for odd n.

Units:

  • n is a number of samples
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.triang(n)
  • MATLAB triang(n), which has no periodic form

func Tukey

func Tukey(n int, alpha float64, opts ...Option) (w []float64, err error)

Tukey returns the n-point Tukey, or cosine-tapered, window: flat in the middle, with raised-cosine tapers at each end making up alpha of its length. alpha at or below 0 is Boxcar, and at or above 1 is Hann, infinities included.

w, err := window.Tukey(64, 0.25)

Units:

  • n is a number of samples
  • alpha is the fraction of the window that tapers
  • w is dimensionless

Returns:

  • ErrInvalidLength for a negative n.
  • ErrInvalidParameter for a NaN alpha.
  • ErrInvalidOption for an option other than Periodic.

Equivalent to:

  • scipy.signal.windows.tukey(n, alpha)
  • MATLAB tukeywin(n, alpha), which has no periodic form

Types

type Option

type Option func(*config)

Option configures a window: Periodic, Center or Unnormalised. A window given an option that doesn't apply to it returns ErrInvalidOption.

func Center

func Center(c float64) (option Option)

Center moves Exponential's peak to sample c, which may be fractional. It needs Periodic, as a symmetric window is centred.

Units: c is a sample index

Equivalent to:

  • scipy.signal.windows.exponential's center, with sym=False
  • MATLAB has none

func Periodic

func Periodic() (option Option)

Periodic gives the DFT-even window, for spectral analysis: the symmetric window of length n+1 without its last point. Without it a window is symmetric, for filter design. Every window but KaiserBesselDerived takes it.

Equivalent to:

  • SciPy's sym=False
  • MATLAB's 'periodic', for the windows that have it

func Unnormalised

func Unnormalised() (option Option)

Unnormalised leaves Taylor's peak where its formula puts it, rather than scaling it to 1 at the centre.

Equivalent to:

  • scipy.signal.windows.taylor's norm=False
  • MATLAB taylorwin, which doesn't normalise

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL