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 ¶
- 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 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
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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