-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
58 lines (50 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var createNdarray = require('ndarray')
module.exports = createNdsamples
function createNdsamples (opts) {
opts = typeof opts != 'object' ? {} : opts
// get options
var data = opts.data
var shape = opts.shape
var stride = opts.stride
var format = opts.format == null ? {} : opts.format
// cast to float32 typed array
if (
data && data.constructor &&
data.constructor.name !== 'Float32Array'
) {
data = toFloat32Array(data)
}
// create ndarray
var arr = createNdarray(
data, shape, stride
)
// assign ndsamples-specific properties
Object.defineProperties(arr, {
// values
format: {
get: function () {
return format
},
enumerable: true
},
// getters
length: {
get: function () {
return this.shape[0]
}
},
numberOfChannels: {
get: function () {
return this.shape[1]
}
}
})
return arr
}
function toFloat32Array (data) {
var newData = new Float32Array(data.length)
for (var i = 0; i < data.length; i++) {
newData[i] = data[i]
}
return newData
}