Documentation
¶
Overview ¶
Package window is window functions for Go, matching scipy.signal.windows . Includes every SciPy window except dpss, plus MATLAB hanning. Standard library only
Windows ¶
Each window returns n samples: symmetric by default, for filter design, or periodic (DFT-even) with Periodic(), for spectral analysis
Cosine sums: Hann, Hamming, Blackman, BlackmanHarris, Nuttall, FlatTop, and the general forms GeneralHamming and GeneralCosine
Piecewise: Boxcar, Triang, Bartlett, Parzen, Bohman, Barthann, Tukey, and MATLAB's Hanning
Sine: Cosine, Lanczos
Exponential: Gaussian, GeneralGaussian, Exponential
Bessel: Kaiser, KaiserBesselDerived (the MDCT window of AAC and Vorbis)
Low sidelobe: Taylor, Chebwin (Dolph–Chebyshev)
Options ¶
One Option type serves every window, a window given an invalid option returns ErrInvalidParameter
Periodic()
every window except KaiserBesselDerived, the symmetric window of n+1 points without its last point. SciPy's sym=False, MATLAB's "periodic"
Center(c)
Exponential, with Periodic only: moves its peak to sample c
Unnormalised()
Taylor: leaves its peak where its formula puts it, rather than at 1. MATLAB's taylorwin, SciPy's norm=False
Compatibility ¶
Every window matches its scipy.signal.windows counterpart, and its MATLAB R2025a counterpart, within a measured tolerance, checked on identical inputs at n = 0, 1, 2, 3, 8, 9, 64 and 255:
Function SciPy MATLAB SciPy MATLAB Tested to Hann hann hann 4.4e-16 1.7e-16 1e-14 Hamming hamming hamming 3.3e-16 2.2e-16 1e-14 Blackman blackman blackman 5.6e-16 3.3e-16 1e-14 BlackmanHarris blackmanharris blackmanharris 6.7e-16 7.8e-16 1e-14 Nuttall nuttall nuttallwin 6.7e-16 8.9e-16 1e-14 FlatTop flattop flattopwin 6.9e-16 3.3e-16 1e-14 GeneralHamming general_hamming 4.4e-16 1e-14 GeneralCosine general_cosine 1.7e-15 1e-13 Boxcar boxcar rectwin exact exact exact Triang triang triang exact exact exact Bartlett bartlett bartlett 1.1e-16 exact 1e-14 Parzen parzen parzenwin 2.2e-16 1.1e-16 1e-14 Bohman bohman bohmanwin 4.4e-16 2.2e-16 1e-14 Barthann barthann barthannwin 3.3e-16 3.3e-16 1e-14 Tukey tukey tukeywin 6.7e-16 6.7e-16 1e-14 Hanning hann(n+2)[1:-1] hanning 3.3e-16 1.7e-16 1e-14 Cosine cosine 4.6e-16 1e-14 Lanczos lanczos 2.2e-16 1e-14 Gaussian gaussian gausswin 1.1e-16 1.7e-16 1e-14 GeneralGaussian general_gaussian 1.1e-16 1e-14 Exponential exponential 1.1e-16 1e-14 Kaiser kaiser kaiser 1.7e-15 2.0e-15 1e-13 KaiserBesselDerived kaiser_bessel_derived exact 1e-13 Taylor taylor taylorwin 5.6e-16 3.4e-16 1e-14 Chebwin chebwin chebwin 1.5e-12 3.7e-13 1e-11
Errors are the largest difference, relative to 1 or the window's peak if that's larger. 1e-16 is about 1ULP of a float64. MATLAB's rectwin, triang, bartlett, parzenwin, bohmanwin, barthannwin, tukeywin, gausswin, kaiser, taylorwin and chebwin have no periodic form, so checked symmetric only
Kaiser's error grows with beta, as I0's sensitivity to its argument does. Chebwin's comes from evaluating a Chebyshev polynomial of degree n-1 near +-1, where an ULP of difference in its argument is magnified. SciPy and MATLAB differ from each other by as much
Differences from SciPy and MATLAB ¶
Symmetric windows are computed for their first half and mirrored, so they're symmetric exactly. SciPy's are symmetric only to within a few ULP
Hanning is MATLAB's: Hann without its zero endpoints, Hann(n+2) less its first and last sample. periodic, it's Hann's periodic window. SciPy has none
Taylor takes its sidelobe level as positive dB, as SciPy. MATLAB's taylorwin takes it negative and doesn't normalise, so taylorwin(n, nbar, -sll) is Taylor(n, nbar, sll, Unnormalised()) for n > 1. At n = 1, taylorwin evaluates its formula. Taylor returns [1], as every window here and in SciPy does
Gaussian takes the standard deviation in samples, as SciPy. MATLAB's gausswin(n, alpha) is Gaussian(n, (n-1)/(2*alpha))
Tukey's alpha is unrestricted, as SciPy and MATLAB: 0 or less is Boxcar, 1 or more is Hann. Parameters with no meaning outside their range are checked where SciPy doesn't: std, p, sig, tau, sll and attenuation must be > 0, nbar >= 1, Kaiser's beta finite, GeneralCosine needs a coefficient, and KaiserBesselDerived needs an even n (n = 1 included, which isn't [1]), symmetric only, as SciPy
Algorithms ¶
Kaiser and KaiserBesselDerived use the modified Bessel function I0, ported from Cephes via SciPy's xsf (see THIRD_PARTY_NOTICES). Chebwin is the real part of the FFT of its Chebyshev polynomial, through tukey/fft
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 or ErrInvalidParameter, can easily be checked with errors.Is
Index ¶
- Variables
- func Barthann(n int, opts ...Option) (w []float64, err error)
- func Bartlett(n int, opts ...Option) (w []float64, err error)
- func Blackman(n int, opts ...Option) (w []float64, err error)
- func BlackmanHarris(n int, opts ...Option) (w []float64, err error)
- func Bohman(n int, opts ...Option) (w []float64, err error)
- func Boxcar(n int, opts ...Option) (w []float64, err error)
- func Chebwin(n int, attenuation float64, opts ...Option) (w []float64, err error)
- func Cosine(n int, opts ...Option) (w []float64, err error)
- func Exponential(n int, tau float64, opts ...Option) (w []float64, err error)
- func FlatTop(n int, opts ...Option) (w []float64, err error)
- func Gaussian(n int, std float64, opts ...Option) (w []float64, err error)
- func GeneralCosine(n int, a []float64, opts ...Option) (w []float64, err error)
- func GeneralGaussian(n int, p, sig float64, opts ...Option) (w []float64, err error)
- func GeneralHamming(n int, alpha float64, opts ...Option) (w []float64, err error)
- func Hamming(n int, opts ...Option) (w []float64, err error)
- func Hann(n int, opts ...Option) (w []float64, err error)
- func Hanning(n int, opts ...Option) (w []float64, err error)
- func Kaiser(n int, beta float64, opts ...Option) (w []float64, err error)
- func KaiserBesselDerived(n int, beta float64, opts ...Option) (w []float64, err error)
- func Lanczos(n int, opts ...Option) (w []float64, err error)
- func Nuttall(n int, opts ...Option) (w []float64, err error)
- func Parzen(n int, opts ...Option) (w []float64, err error)
- func Taylor(n, nbar int, sll float64, opts ...Option) (w []float64, err error)
- func Triang(n int, opts ...Option) (w []float64, err error)
- func Tukey(n int, alpha float64, opts ...Option) (w []float64, err error)
- type Option
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidLength = errors.New("window: invalid length") ErrInvalidParameter = errors.New("window: invalid parameter") )
Errors returned for invalid input each is wrapped with the specifics of what was wrong check with errors.Is
Functions ¶
func Barthann ¶
Barthann returns the n-point modified Bartlett-Hann window: a weighted combination of Bartlett and Hann
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.barthann(n), equivalent to MATLAB barthannwindow(n)
func Bartlett ¶
Barlett returns the n-point Bartlett window: a triangle, zero at both ends
returns ErrInvalidLength if n < 0
equivalent to scipy.windows.barlett(n), equivalent to MATLAB bartlett(n)
func Blackman ¶
Blackman returns the n-point Blackman window: three cosine terms, zero at both ends
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.blackman(n) equivalent to MATLAB's blackman(n)
func BlackmanHarris ¶
BlackmanHarris returns the n-point minimum 4-term Blackman–Harris window
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.blackmanharris(n) equivalent to MATLAB's blackmanharris(n)
func Bohman ¶
Bohman retuns the n-point Bohman window: a triangle multiplied by a cosine lobe, plus a sine term making its first derivative continuous
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.bohman(n), equivalent to MATLAB bohmanwin(n)
func Boxcar ¶
Boxcar returns the n-point rectangular window: all ones
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.boxcar(n), equivalent to MATLAB rectwin(n)
func Chebwin ¶
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. Normalised so its peak is 1
SciPy warns that below about 45 dB it isn't suitable for spectral analysis
returns ErrInvalidLength if n < 0, and ErrInvalidParameter unless attenuation > 0
equivalent to scipy.signal.windows.chebwin(n, attenuation), equivalent to MATLAB's chebwin(n, attenuation)
func Cosine ¶
Cosine returns the n-point cosine (sine) window: half a peroid of a sine, not reaching zero at its ends
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.cosine(n), no MATLAB equivalent
func Exponential ¶
Exponential returns the n-point exponential (Poisson) window, exp(-|x|/tau) for x samples from its peak, the middle unless Center moves it. The usual force and response window in impact testing
returns ErrInvalidLength if n < 0, and ErrInvalidParameter unless tau > 0, or if Center is given without Periodic or is NaN
equivalent to scipy.signal.windows.exponential(n, center, tau); MATLAB has none
func FlatTop ¶
FlatTop returns the n-point float top window, for measuring amp. of sins its passband is flat to within a fraction of a dB it dips below zero and peaks at 1.000000003
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.flattop(n) and MATLAB's flattopwin(n)
func Gaussian ¶
Gaussian returns the n-point Gaussian window with standard deviation std, in samples
returns ErrInvalidLength if n < 0, and ErrInvalidParameter unless std > 0
equivalent to scipy.signal.windows.gaussian(n, std), equivalent to MATLAB sgausswin(n, alpha) with alpha = (n-1)/(2*std)
func GeneralCosine ¶
GeneralCosine returns the n-point cosine-sum window with coefficients a: the sum of a[j] cos(j x), for x evenly spaced from -pi to pi it isn't normalised: its peak is the sum of a, as SciPy's
returns ErrInvalidLength if n < 0, and ErrInvalidParameter if a is empty
equivalent to scipy.signal.windows.general_cosine(n, a) no MATLAB equivalent
func GeneralGaussian ¶
GeneralGaussian returns the n-point generalised Gaussian window, exp(-0.5 |x/sig|^(2p)) for x samples from the centre p = 1 is Gaussian with std sig, larger p flattens the top
returns ErrInvalidLength if n < 0, and ErrInvalidParameter unless p and sig are > 0
equivalent to scipy.signal.windows.general_gaussian(n, p, sig); MATLAB has none
func GeneralHamming ¶
GeneralHamming returns the n-point generalised Hamming window alpha - (1 - alpha) cos(2 pi k / (n-1)) alpha 0.5 is Hann, 0.54 is Hamming
returns ErrInvalidLength if n < 0
equivalnet to scipy.signal.windows.general_hamming(n, alpha) no MATLAB equivalent
func Hamming ¶
Hamming returns the n-point Hamming window: a raised cosine on a pedestal of 0.08
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.hamming(n) equivalent to MATLAB's hamming(n)
func Hann ¶
Hann returns the n-point Hann window: a raised cosine, zero at both ends, or only at the start when periodic
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.hann(n) equivalnt to MATLAB's hann(n)
func Hanning ¶
Hanning returns MATLAB's n-point Hanning window: symmetric, it's the Hann window without the zero endpoints, Hann(n+2) less its first and last sample. periodic, it's Hann's periodic window, keeping it's first zero
returns ErrInvalidLength if n<0
equivalent to MATLAB's hanning(n), no SciPy equivalent
func Kaiser ¶
Kaiser returns the n-point Kaiser windows with shape beta: I0(beta sqrt(1 - x^2)) / I0(beta), for x from -1 to 1 beta 0 is Boxcar, and larger beta traders a wider main love for lower sidelobes: about 5 is like Hamming, 8.6 like Blackman, 14 for very low sidelobes a negative beta is the same window as |beta|, as I0 is even
returns ErrInvalidLength if n<0, and ErrInvalidParameter if beta is NaN or +=Inf
equivalent to scipy.signal.windows.kaiser(n, beta), equivalent to MATLAB kaiser(n, beta)
func KaiserBesselDerived ¶
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]^2 + w[k+n/2]^2 = 1 (the Princeton-Bradley condition, for perfect reconstruction) it's defined only for even n, and only symmetric
returns ErrInvalidLength if n < 0 or n is odd (including n = 1), returns ErrInvalidParameter if beta is NaN or +=Inf, or given any option
equivalent to scipy.signal.windows.kaiser_bessel_derived(n, beta), no MATLAB equivalent
func Lanczos ¶
Lanczos returns the n-point Lanczos (sinc) window: the central lobe of sin(pi x)/(pi x), for x from -1 to 1
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.lanczos(n), no MATLAB equivalent
func Nuttall ¶
Nuttall returns the n-point minimum 4-term Blackman–Harris window, Nuttall's version
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.nuttall(n) equivalent to MATLAB's nuttallwin(n)
func Parzen ¶
Parzen returns the n-point Parzen window: a piecewise cubic, the triangle convolved with itself twice
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.parzen(n), equivalent to MATLAB parzenwin(n)
func Taylor ¶
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. Normalised to 1 at the centre unless Unnormalised. nbar = 1 is Boxcar
returns ErrInvalidLength if n < 0 returns ErrInvalidParameter if nbar < 1 or unless sll > 0
equivalent to scipy.signal.windows.taylor(n, nbar, sll) equivalent to MATLAB taylorwin(n, nbar, -sll) is Taylor with Unnormalised, for n > 1
func Triang ¶
Triang returns the n-point trangular window unlike Bartlett, it doesn't reach zero: its ends are 1/n, or 2/(n+1) for odd n
returns ErrInvalidLength if n < 0
equivalent to scipy.signal.windows.trang(n), equivalent to MATLAB triang(n)
func Tukey ¶
Tukey returns the n-point tukey (cosine-tapered) window: flat in the middle with raised-cosine tapers at each end making up alpha of its length
alpha 0 or less is Boxcar, and 1 or more is Hann
returns ErrInvalidLength if n < 0, and ErrInvalidParameter if alpha is NaN
equivalent to scipy.signal.windows.tukey(n, alpha) and MATLAB's tukeywin(n, alpha)
Types ¶
type Option ¶
type Option func(*config)
Option configures a window (see Periodic, Center, Unnormalised) a window given an option that doesn't apply to it returns ErrInvalidParameter
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 default is symmetric, for filter design equivalent to SciPy's sym=False and MATLAB's "periodic"
func Unnormalised ¶
func Unnormalised() (option Option)
Unnormalised leaves Taylor's peak where its formula puts it, rather than sclaing it to 1 at the centre
equivalent to SciPy's norm=False, equivalent to MATLAB taylorwin, which doesn't normalise