diff --git a/lib/bartlett-hann.js b/lib/bartlett-hann.js new file mode 100644 index 0000000..84715fd --- /dev/null +++ b/lib/bartlett-hann.js @@ -0,0 +1,12 @@ +'use strict' + +function bartlettHann (i,N) { + var inm1 = i/(N-1), + a0 = 0.62, + a1 = 0.48, + a2 = 0.38 + + return a0 - a1 * Math.abs(inm1 - 0.5) - a2 * Math.cos(6.283185307179586*inm1) +} + +module.exports = bartlettHann diff --git a/lib/bartlett.js b/lib/bartlett.js new file mode 100644 index 0000000..1b00711 --- /dev/null +++ b/lib/bartlett.js @@ -0,0 +1,7 @@ +'use strict' + +function bartlett (i,N) { + return 1 - Math.abs( 2 * (i - 0.5*(N-1)) / (N-1) ) +} + +module.exports = bartlett diff --git a/lib/blackman-harris.js b/lib/blackman-harris.js new file mode 100644 index 0000000..1075917 --- /dev/null +++ b/lib/blackman-harris.js @@ -0,0 +1,13 @@ +'use strict' + +function blackmanHarris (i,N) { + var a0 = 0.35875, + a1 = 0.48829, + a2 = 0.14128, + a3 = 0.01168, + f = 6.283185307179586*i/(N-1) + + return a0 - a1*Math.cos(f) +a2*Math.cos(2*f) - a3*Math.cos(3*f) +} + +module.exports = blackmanHarris diff --git a/lib/blackman-nuttall.js b/lib/blackman-nuttall.js new file mode 100644 index 0000000..a99d4ef --- /dev/null +++ b/lib/blackman-nuttall.js @@ -0,0 +1,13 @@ +'use strict' + +function blackmanNuttall (i,N) { + var a0 = 0.3635819, + a1 = 0.4891775, + a2 = 0.1365995, + a3 = 0.0106411, + f = 6.283185307179586*i/(N-1) + + return a0 - a1*Math.cos(f) +a2*Math.cos(2*f) - a3*Math.cos(3*f) +} + +module.exports = blackmanNuttall diff --git a/lib/blackman.js b/lib/blackman.js new file mode 100644 index 0000000..152bcc7 --- /dev/null +++ b/lib/blackman.js @@ -0,0 +1,12 @@ +'use strict' + +function blackman (i,N) { + var a0 = 0.42, + a1 = 0.5, + a2 = 0.08, + f = 6.283185307179586*i/(N-1) + + return a0 - a1 * Math.cos(f) + a2*Math.cos(2*f) +} + +module.exports = blackman diff --git a/lib/cosine.js b/lib/cosine.js new file mode 100644 index 0000000..d0f377c --- /dev/null +++ b/lib/cosine.js @@ -0,0 +1,7 @@ +'use strict' + +function cosine (i,N) { + return Math.sin(3.141592653589793*i/(N-1)) +} + +module.exports = cosine diff --git a/lib/exact-blackman.js b/lib/exact-blackman.js new file mode 100644 index 0000000..9165a49 --- /dev/null +++ b/lib/exact-blackman.js @@ -0,0 +1,12 @@ +'use strict' + +function exactBlackman (i,N) { + var a0 = 0.42659, + a1 = 0.49656, + a2 = 0.076849, + f = 6.283185307179586*i/(N-1) + + return a0 - a1 * Math.cos(f) + a2*Math.cos(2*f) +} + +module.exports = exactBlackman diff --git a/lib/flat-top.js b/lib/flat-top.js new file mode 100644 index 0000000..2827a07 --- /dev/null +++ b/lib/flat-top.js @@ -0,0 +1,14 @@ +'use strict' + +function flatTop (i,N) { + var a0 = 1, + a1 = 1.93, + a2 = 1.29, + a3 = 0.388, + a4 = 0.028, + f = 6.283185307179586*i/(N-1) + + return a0 - a1*Math.cos(f) +a2*Math.cos(2*f) - a3*Math.cos(3*f) + a4 * Math.cos(4*f) +} + +module.exports = flatTop diff --git a/lib/gaussian.js b/lib/gaussian.js new file mode 100644 index 0000000..fa85e1d --- /dev/null +++ b/lib/gaussian.js @@ -0,0 +1,10 @@ +'use strict' + +function gaussian (i,N,sigma) { + var nm12 = 0.5*(N-1), + f = (i-nm12)/sigma/nm12 + + return Math.exp(-0.5*f*f) +} + +module.exports = gaussian diff --git a/lib/hamming.js b/lib/hamming.js new file mode 100644 index 0000000..c49f7d1 --- /dev/null +++ b/lib/hamming.js @@ -0,0 +1,7 @@ +'use strict' + +function hamming (i,N) { + return 0.54 - 0.46 * Math.cos(6.283185307179586*i/(N-1)) +} + +module.exports = hamming diff --git a/lib/hann.js b/lib/hann.js new file mode 100644 index 0000000..55398ff --- /dev/null +++ b/lib/hann.js @@ -0,0 +1,7 @@ +'use strict' + +function hann (i,N) { + return 0.5*(1 - Math.cos(6.283185307179586*i/(N-1))) +} + +module.exports = hann diff --git a/lib/index.js b/lib/index.js index 9703275..52fb6fb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,133 +1,26 @@ 'use strict' -var TWOPI = Math.PI * 2 - -function sinc (x) { - return x === 0 ? 1 : 0.3183098861837907 * Math.sin(Math.PI*x) / x -} - -function rectangular (i,N) { - return 1 -} - -function triangular (i,N) { - return 1 - Math.abs( 2 * (i - 0.5*(N-1)) / N ) -} - -function bartlett (i,N) { - return 1 - Math.abs( 2 * (i - 0.5*(N-1)) / (N-1) ) -} - -function welch (i,N) { - var nm12 = 0.5*(N-1), - f = (i - nm12)/nm12 - return 1 - f*f -} - -function hann (i,N) { - return 0.5*(1 - Math.cos(TWOPI*i/(N-1))) -} - -function hamming (i,N) { - return 0.54 - 0.46 * Math.cos(TWOPI*i/(N-1)) -} - -function blackman (i,N) { - var a0 = 0.42, - a1 = 0.5, - a2 = 0.08, - f = TWOPI*i/(N-1) - - return a0 - a1 * Math.cos(f) + a2*Math.cos(2*f) -} - -function exactBlackman (i,N) { - var a0 = 0.42659, - a1 = 0.49656, - a2 = 0.076849, - f = TWOPI*i/(N-1) - - return a0 - a1 * Math.cos(f) + a2*Math.cos(2*f) -} - -function nuttall (i,N) { - var a0 = 0.355768, - a1 = 0.487396, - a2 = 0.144232, - a3 = 0.012604, - f = TWOPI*i/(N-1) - - return a0 - a1*Math.cos(f) +a2*Math.cos(2*f) - a3*Math.cos(3*f) -} - -function blackmanNuttall (i,N) { - var a0 = 0.3635819, - a1 = 0.4891775, - a2 = 0.1365995, - a3 = 0.0106411, - f = TWOPI*i/(N-1) - - return a0 - a1*Math.cos(f) +a2*Math.cos(2*f) - a3*Math.cos(3*f) -} - -function blackmanHarris (i,N) { - var a0 = 0.35875, - a1 = 0.48829, - a2 = 0.14128, - a3 = 0.01168, - f = TWOPI*i/(N-1) - - return a0 - a1*Math.cos(f) +a2*Math.cos(2*f) - a3*Math.cos(3*f) -} - -function flatTop (i,N) { - var a0 = 1, - a1 = 1.93, - a2 = 1.29, - a3 = 0.388, - a4 = 0.028, - f = TWOPI*i/(N-1) - - return a0 - a1*Math.cos(f) +a2*Math.cos(2*f) - a3*Math.cos(3*f) + a4 * Math.cos(4*f) -} - -function bartlettHann (i,N) { - var inm1 = i/(N-1), - a0 = 0.62, - a1 = 0.48, - a2 = 0.38 - - return a0 - a1 * Math.abs(inm1 - 0.5) - a2 * Math.cos(TWOPI*inm1) -} - -function cosine (i,N) { - return Math.sin(Math.PI*i/(N-1)) -} - -function gaussian (i,N,sigma) { - var nm12 = 0.5*(N-1), - f = (i-nm12)/sigma/nm12 - - return Math.exp(-0.5*f*f) -} - -function tukey (i,N, alpha) { - var anm12 = 0.5*alpha*(N-1) - - if( i <= anm12 ) { - return 0.5*(1+Math.cos(Math.PI*(i/anm12 - 1))) - } else if ( i < (N-1)*(1-0.5*alpha) ) { - return 1 - } else { - return 0.5*(1+Math.cos(Math.PI*(i/anm12 - 2/alpha + 1))) - } -} - -function lanczos (i, N) { - return sinc(2*i/(N-1)-1) -} - - +module.exports = { + lanczos: require('./lanczos'), + rectangular: require('./rectangular'), + triangular: require('./triangular'), + bartlett: require('./bartlett'), + bartlettHann: require('./bartlett-hann'), + welch: require('./welch'), + hann: require('./hann'), + hamming: require('./hamming'), + blackman: require('./blackman'), + nuttall: require('./nuttall'), + blackmanNuttall: require('./blackman-nuttall'), + blackmanHarris: require('./blackman-harris'), + exactBlackman: require('./exact-blackman'), + flatTop: require('./flat-top'), + cosine: require('./cosine'), + gaussian: require('./gaussian'), + tukey: require('./tukey') +} + +/* function applyWindow(signal, func) { var i, n=signal.length, args=[0,n] @@ -157,26 +50,5 @@ function generate(func, N) { signal[i] = func.apply(null,args) } return signal; -} - -exports.rectangular = rectangular -exports.triangular = triangular -exports.bartlett = bartlett -exports.welch = welch -exports.hann = hann -exports.hamming = hamming -exports.blackman = blackman -exports.exactBlackman = exactBlackman -exports.nuttall = nuttall -exports.blackmanNuttall = blackmanNuttall -exports.blackmanHarris = blackmanHarris -exports.flatTop = flatTop -exports.bartlettHann = bartlettHann -exports.cosine = cosine -exports.gaussian = gaussian -exports.tukey = tukey -exports.lanczos = lanczos - -exports.window = applyWindow -exports.generate = generate +}*/ diff --git a/lib/lanczos.js b/lib/lanczos.js new file mode 100644 index 0000000..d95d0bc --- /dev/null +++ b/lib/lanczos.js @@ -0,0 +1,11 @@ +'use strict' + +function sinc (x) { + return x === 0 ? 1 : 0.3183098861837907 * Math.sin(Math.PI*x) / x +} + +function lanczos (i, N) { + return sinc(2*i/(N-1)-1) +} + +module.exports = lanczos diff --git a/lib/nuttall.js b/lib/nuttall.js new file mode 100644 index 0000000..8bdab5f --- /dev/null +++ b/lib/nuttall.js @@ -0,0 +1,15 @@ +'use strict' + +var TWOPI = Math.PI * 2 + +function nuttall (i,N) { + var a0 = 0.355768, + a1 = 0.487396, + a2 = 0.144232, + a3 = 0.012604, + f = TWOPI*i/(N-1) + + return a0 - a1*Math.cos(f) +a2*Math.cos(2*f) - a3*Math.cos(3*f) +} + +module.exports = nuttall diff --git a/lib/rectangular.js b/lib/rectangular.js new file mode 100644 index 0000000..ab6d0cd --- /dev/null +++ b/lib/rectangular.js @@ -0,0 +1,7 @@ +'use strict' + +function rectangular (i,N) { + return 1 +} + +module.exports = rectangular diff --git a/lib/sinc.js b/lib/sinc.js new file mode 100644 index 0000000..e69de29 diff --git a/lib/triangular.js b/lib/triangular.js new file mode 100644 index 0000000..e874661 --- /dev/null +++ b/lib/triangular.js @@ -0,0 +1,7 @@ +'use strict' + +function triangular (i,N) { + return 1 - Math.abs( 2 * (i - 0.5*(N-1)) / N ) +} + +module.exports = triangular diff --git a/lib/tukey.js b/lib/tukey.js new file mode 100644 index 0000000..111a1bc --- /dev/null +++ b/lib/tukey.js @@ -0,0 +1,15 @@ +'use strict' + +function tukey (i,N, alpha) { + var anm12 = 0.5*alpha*(N-1) + + if( i <= anm12 ) { + return 0.5*(1+Math.cos(Math.PI*(i/anm12 - 1))) + } else if ( i < (N-1)*(1-0.5*alpha) ) { + return 1 + } else { + return 0.5*(1+Math.cos(Math.PI*(i/anm12 - 2/alpha + 1))) + } +} + +module.exports = tukey diff --git a/lib/welch.js b/lib/welch.js new file mode 100644 index 0000000..0ff7261 --- /dev/null +++ b/lib/welch.js @@ -0,0 +1,9 @@ +'use strict' + +function welch (i,N) { + var nm12 = 0.5*(N-1), + f = (i - nm12)/nm12 + return 1 - f*f +} + +module.exports = welch diff --git a/package.json b/package.json index 0214cf3..2ea6584 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "scijs-window-functions", - "version": "1.2.2", + "version": "2.0.0", "description": "Window functions for spectral analysis", "main": "lib/index.js", "scripts": { diff --git a/test/test.js b/test/test.js index aeeefbc..b7837c4 100644 --- a/test/test.js +++ b/test/test.js @@ -1,9 +1,8 @@ 'use strict' -var wfunc = require('../lib/index.js'), +var wfunc = require('../lib'), assert = require('assert') - var windows = [ 'rectangular', 'triangular', @@ -27,11 +26,13 @@ describe('window functions return a finite number',function() { var i for(i=0; i