-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
113 lines (86 loc) · 2.87 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var Readable = require('readable-stream').Readable
var util = require('util')
module.exports = RandomSignal
function RandomSignal (opt) {
if (!opt) opt = {}
if (!(this instanceof RandomSignal))
return new RandomSignal(opt);
Readable.call(this, opt)
var self = this
self._destroyed = false
self.iv = null
var tdelta = opt.tdelta || 50 // milliseconds
, hz = opt.hz || 1000 / (20 * tdelta) // seconds
, amp = opt.amp || 1
, noiseHz = opt.noiseHz || 4 * hz
, noiseAmp = opt.noiseAmp || 0.3 * amp
, trendIV = opt.trendIV || (1000 * 1/hz) // milliseconds
, lowtrend = opt.lowtrend || -amp
, hightrend = opt.hightrend || amp
, sep = (typeof(opt.sep) === 'string') ? opt.sep : "\n"
, timeFormatter = opt.timeFormatter || getTimeString
hz *= 2 * Math.PI // compute once here for later ts computations
noiseHz *= 2 * Math.PI
var randTrendGen = randGenerator(lowtrend, hightrend)
, randNoiseAmp = randGenerator(0, noiseAmp)
if (self.trendIV < 2*self.tdelta)
throw new Error("trendIV must be greater than 2*tdelta")
startSignal()
function startSignal () {
var last = 0
, trend = 0
, start = Date.now()
self.iv = setInterval( function () {
var now = new Date()
, ts = now.getTime()
if ((ts - last) > trendIV) {
trend = randTrendGen()
last = ts
}
var x = timeFormatter(now)
var y = generateSignal((ts - start) / 1000, trend)
var data = {x:x, y:y}
if (!opt.objectMode) {
data = JSON.stringify(data) + sep
data = new Buffer(data, 'utf8');
}
self.push(data);
}, tdelta)
}
function generateSignal(ts, trend) {
var y = amp*Math.sin(hz*ts)
, noise = randNoiseAmp() * Math.sin(noiseHz*ts)
return y + noise + trend
}
}
util.inherits(RandomSignal, Readable)
RandomSignal.prototype._read = function () {
if (this._destroyed) {
this.push(null)
}
}
RandomSignal.prototype.destroy = function () {
clearInterval(this.iv)
this._destroyed = true
this.emit("end")
}
/*
* Helper functions
*/
function getTimeString (now) {
var year = "" + now.getFullYear();
var month = "" + (now.getMonth() + 1); if (month.length == 1) { month = "0" + month }
var day = "" + now.getDate(); if (day.length == 1) { day = "0" + day }
var hour = "" + now.getHours(); if (hour.length == 1) { hour = "0" + hour }
var minute = "" + now.getMinutes(); if (minute.length == 1) { minute = "0" + minute }
var second = "" + now.getSeconds(); if (second.length == 1) { second = "0" + second }
var ms = "" + now.getMilliseconds(); while (ms.length < 3) { ms = "0"+ms }
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + "." + ms;
}
function randGenerator(min, max) {
if (isNaN(min)) min = -1
if (isNaN(max)) max = 1
return function () {
return Math.random() * (max - min + 1) + min
}
}