Skip to content

Commit

Permalink
Reorganize to allow separate requires #4
Browse files Browse the repository at this point in the history
  • Loading branch information
rreusser committed May 15, 2015
1 parent da759f2 commit 6a50f17
Show file tree
Hide file tree
Showing 21 changed files with 210 additions and 159 deletions.
12 changes: 12 additions & 0 deletions lib/bartlett-hann.js
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/bartlett.js
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lib/blackman-harris.js
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions lib/blackman-nuttall.js
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions lib/blackman.js
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/cosine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

function cosine (i,N) {
return Math.sin(3.141592653589793*i/(N-1))
}

module.exports = cosine
12 changes: 12 additions & 0 deletions lib/exact-blackman.js
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions lib/flat-top.js
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions lib/gaussian.js
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/hamming.js
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/hann.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

function hann (i,N) {
return 0.5*(1 - Math.cos(6.283185307179586*i/(N-1)))
}

module.exports = hann
172 changes: 22 additions & 150 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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
}*/

11 changes: 11 additions & 0 deletions lib/lanczos.js
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions lib/nuttall.js
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/rectangular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

function rectangular (i,N) {
return 1
}

module.exports = rectangular
Empty file added lib/sinc.js
Empty file.
7 changes: 7 additions & 0 deletions lib/triangular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

function triangular (i,N) {
return 1 - Math.abs( 2 * (i - 0.5*(N-1)) / N )
}

module.exports = triangular
15 changes: 15 additions & 0 deletions lib/tukey.js
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions lib/welch.js
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
Loading

0 comments on commit 6a50f17

Please sign in to comment.