-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalizer.js
81 lines (71 loc) · 2.58 KB
/
normalizer.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function Normalizer (min, max, opts) {
opts = opts || {}
this.verbose = opts.verbose
this.old = opts.old
this.stabilization_rate = opts.stabilization_rate
this.min = min
this.max = max
this.observedMin = parseInt(max / 2) - 1
this.observedMax = parseInt(max / 2)
this.minCount = 0
this.maxCount = 0
}
Normalizer.prototype.average = function (u8IntArray) {
var total = 0
for (var i = 0; i < u8IntArray.length; ++i) {
total += u8IntArray[i]
}
var average = total / u8IntArray.length
//check invariants
// var local_max = this.observedMax
// var local_min = this.observedMin
this.update(average)
// if (local_max > this.observedMax) throw Error('invariant failed')
// if (local_min < this.observedMin) throw Error('invariant failed')
return average
}
Normalizer.prototype.normalize = function (val) {
if (this.verbose) console.log('value', val)
if (this.verbose) console.log('observedMax', this.observedMax);
if (this.verbose) console.log('observedMin', this.observedMin);
if (this.verbose) console.log('partial', ( (val - this.observedMin) ))
return ( ( (val - this.observedMin) * (this.max - this.min) ) / (this.observedMax - this.observedMin) ) + this.min
}
Normalizer.prototype.update = function (val) {
if (val === 0) return
if (this.old) {
if (this.observedMax < val) {
this.observedMax = ((this.observedMax * this.maxCount) + val) / ++this.maxCount
}
if (this.observedMin > val) {
this.observedMin = ((this.observedMin * this.minCount) + val) / ++this.minCount
}
} else {
if (this.observedMax < val) {
this.observedMax = val
}
else if (this.observedMin > val) {
this.observedMin = val
}
else if (val > ((this.observedMax - this.observedMin) / 2)) {
if (this.verbose) console.log('increasing', this.observedMin - val)
this.observedMin = this.observedMin + (Math.pow(val - this.observedMin, 1 / this.stabilization_rate))
}
else if (val < ((this.observedMax - this.observedMin) / 2)) {
if (this.verbose) console.log('decreasing', val - this.observedMax)
this.observedMax = this.observedMax - (Math.pow(this.observedMax - val, 1 / this.stabilization_rate))
}
}
return
}
Normalizer.prototype.getNormalizedAverage = function (u8IntArray) {
var result = parseInt(this.normalize(this.average(u8IntArray)))
// if (result > this.max || result < this.min) {
// console.log('this', this)
// console.log('result', result)
// throw new Error('postcondition failed')
// }
if (this.verbose) console.log(result)
return result
};
module.exports = Normalizer