Window functions for digital signal processing
Among other uses, window functions help control spectral leakage when doing Fourier Analysis. This collection of window functions is copied directly from Wikipedia. Caveat emptor.
Apply window to a signal:
var blackmanHarris = require('window-function/blackman-harris')
var applyWindow = require('window-function/apply')
var signal = [-1, 0, 1, 0, -1, 0]
var windowedSignal = applyWindow(signal, blackmanHarris)
Apply the window functions yourself:
var wfuncs = require('window-function')
var value = wfuncs.blackmanHarris( 50, 101 )
To calculate the value of a window function, pass the sample number i
and total
number of samples to one of the window functions listed below, along with any additional parameters it may require. The plots below are calculated from the npm module and plotted with Fourier transform to illustrate the spectral leakage. See the Wikipedia page on window functions for more details.
- Bartlett-Hann
- Bartlett
- Blackman-Harris
- Blackman-Nuttall
- Cosine
- Exact Blackman
- Flat top
- Gaussian
- Hamming
- Hann
- Lanczos
- Nuttall
- Rectangular
- Triangular
- Tukey
- Welch
The same as the Blackman window, except a0 = 0.42659 a1 = 0.49656, and a2 = 0.076849. These place zeros at the third and fourth sidelobes.
Sigma controls the width of the window.
A tapered cosine window. Alpha controls the relative width of the flat section. Alpha=0 is rectangular, alpha=1 is Hann.
Apply a windowing function to an array, modifies an array in-place.
Generate an array of n
samples of the window function fn
.
The tests ensure the window functions aren't returning NaN, but it's hard to have confidence in the accuracy with tests. Instead, I opted to focus on visual testing via plots and Fourier Transforms. To generate the plots, run
$ npm run generate-plots
It pipes them through matplotlib. Don't worry. I realize the irony.
Window function definitions and equation images from Wikipedia: Window Function.
(c) 2015 Ricky Reusser. MIT License