Skip to content

Commit ff29dc8

Browse files
authored
Merge pull request #7 from scijs/apply-generate
Refactor tests, expose apply/generate entries
2 parents 22faffc + 871efb6 commit ff29dc8

7 files changed

Lines changed: 121 additions & 129 deletions

File tree

README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,30 @@ Among other uses, [window functions](http://en.wikipedia.org/wiki/Window_functio
1111

1212
## Usage
1313

14-
To require all window functions, include the top-level `window-function` module:
14+
Apply window to a signal:
1515

1616
```javascript
17-
var wfuncs = require('window-function')
17+
var blackmanHarris = require('window-function/blackman-harris')
18+
var applyWindow = require('window-function/apply')
1819

1920
var signal = [-1, 0, 1, 0, -1, 0]
2021

21-
var windowedSignal = wfuncs.applyWindow(signal, wfuncs.blackmanHarris)
22+
var windowedSignal = applyWindow(signal, blackmanHarris)
2223
```
2324

24-
You can also apply the window functions yourself:
25+
Apply the window functions yourself:
2526

2627
```javascript
2728
var wfuncs = require('window-function')
2829

2930
var value = wfuncs.blackmanHarris( 50, 101 )
3031
```
3132

32-
You can also require a single window function:
33+
## API
3334

34-
```javascript
35-
var blackmanHarris = require('window-function/blackman-harris')
35+
### `require('window-funciton/<type>')(i, total)`
3636

37-
var value = blackmanHarris( 50, 101 )
38-
```
39-
40-
To calculate the value of a window function, pass the sample number 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](http://en.wikipedia.org/wiki/Window_function) for more details.
37+
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](http://en.wikipedia.org/wiki/Window_function) for more details.
4138

4239
- [Bartlett-Hann](#bartletthann-i-n-)
4340
- [Bartlett](#bartlett-i-n-)
@@ -174,6 +171,13 @@ A tapered cosine window. Alpha controls the relative width of the flat section.
174171

175172
![Welch](docs/plots/welch.png)
176173

174+
### `require('window-function/apply')(array, fn)`
175+
176+
Apply a windowing function to an array, modifies an array in-place.
177+
178+
### `require('window-function/generate')(fn, n)`
179+
180+
Generate an array of `n` samples of the window function `fn`.
177181

178182
## Testing
179183

apply.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
module.exports = function applyWindow(signal, func) {
4+
var i, n=signal.length, args=[0,n]
5+
6+
// pass rest of args
7+
for(i=2; i<arguments.length; i++) {
8+
args[i] = arguments[i]
9+
}
10+
11+
for(i=n-1; i>=0; i--) {
12+
args[0] = i
13+
signal[i] *= func.apply(null,args)
14+
}
15+
16+
return signal;
17+
}

generate.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
module.exports = function generate(func, N) {
4+
var i, args=[0,N]
5+
var signal = new Array(N);
6+
7+
for(i=2; i<arguments.length; i++) {
8+
args[i] = arguments[i]
9+
}
10+
11+
for(i=N-1; i>=0; i--) {
12+
args[0] = i
13+
signal[i] = func.apply(null,args)
14+
}
15+
return signal;
16+
}
17+

index.js

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,5 @@ module.exports = {
1717
flatTop: require('./flat-top'),
1818
cosine: require('./cosine'),
1919
gaussian: require('./gaussian'),
20-
tukey: require('./tukey'),
21-
applyWindow: applyWindow
20+
tukey: require('./tukey')
2221
}
23-
24-
function applyWindow(signal, func) {
25-
var i, n=signal.length, args=[0,n]
26-
27-
for(i=2; i<arguments.length; i++) {
28-
args[i] = arguments[i]
29-
}
30-
31-
for(i=n-1; i>=0; i--) {
32-
args[0] = i
33-
signal[i] *= func.apply(null,args)
34-
}
35-
36-
return signal;
37-
}
38-
39-
/*
40-
function generate(func, N) {
41-
var i, args=[0,N]
42-
var signal = new Array(N);
43-
44-
for(i=2; i<arguments.length; i++) {
45-
args[i] = arguments[i]
46-
}
47-
48-
for(i=N-1; i>=0; i--) {
49-
args[0] = i
50-
signal[i] = func.apply(null,args)
51-
}
52-
return signal;
53-
}*/
54-

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Window functions for spectral analysis",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha",
7+
"test": "node test",
88
"generate-plots": "node docs/generate.js"
99
},
1010
"repository": {
@@ -24,7 +24,7 @@
2424
"license": "MIT",
2525
"devDependencies": {
2626
"jshint": "^2.7.0",
27-
"mocha": "^2.2.4",
28-
"plotly": "^1.0.2"
27+
"plotly": "^1.0.2",
28+
"tape": "^4.9.1"
2929
}
3030
}

test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict'
2+
3+
var wfunc = require('./')
4+
var applyWindow = require('./apply')
5+
var generateWindow = require('./generate')
6+
var t = require('tape')
7+
8+
9+
var windows = [
10+
'rectangular',
11+
'triangular',
12+
'bartlett',
13+
'welch',
14+
'hann',
15+
'hamming',
16+
'blackman',
17+
'exactBlackman',
18+
'nuttall',
19+
'blackmanNuttall',
20+
'blackmanHarris',
21+
'flatTop',
22+
'bartlettHann',
23+
'cosine',
24+
'lanczos'
25+
]
26+
27+
t('window functions return a finite number',function(t) {
28+
29+
var i
30+
31+
for(i=0; i<windows.length; i++) {
32+
(function(j) {
33+
34+
t.ok( isFinite(wfunc[windows[j]](50,101)), windows[j] )
35+
36+
})(i)
37+
38+
}
39+
40+
t.ok( isFinite(wfunc.gaussian(50,100,0.4)), 'gaussian (alpha = 0.4)')
41+
t.ok( isFinite(wfunc.tukey(50,100,0.4)), 'tukey (alpha = 0.5)')
42+
t.end()
43+
})
44+
45+
t('applies a window to a signal',function(t) {
46+
var x = [1, 1, 1, 1, 1]
47+
var y = applyWindow( x, wfunc.hamming )
48+
t.equal(y.length, x.length)
49+
t.end()
50+
})
51+
52+
t('passes extra arguments to a window function',function(t) {
53+
var x = [1, 1, 1, 1, 1]
54+
applyWindow( x, wfunc.gaussian, 0.1 )
55+
t.ok( isFinite(x[0]), 'samples are finite' )
56+
t.ok( x[0] < 1e-8, 'samples have been windowed' )
57+
t.end()
58+
})
59+
60+
t('constructs an window function array',function(t) {
61+
var x = generateWindow( wfunc.hamming, 100 )
62+
t.ok( Array.isArray(x) )
63+
t.equal( x.length, 100 )
64+
t.ok( isFinite(x[50]) )
65+
t.end()
66+
})
67+
68+

test/test.js

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)