-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsignalGenerator.js
145 lines (125 loc) · 4.58 KB
/
signalGenerator.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class Generator {
constructor() {
if (this.constructor === Generator) {
const error = new Error("Can't instantiate abstract class!");
console.log(error);
}
}
generateValues(t) {
const error = new Error("This method can't be called!");
console.log(error);
}
}
class SineGenerator extends Generator {
constructor(mean, amplitude, wave_period, phase) {
super();
this.mean = mean;
this.amplitude = amplitude;
this.wave_period = wave_period;
this.phase = phase;
}
generateValues(t) {
let tempData = parseFloat(this.mean) + Math.sin(t * (2 * Math.PI) / (parseFloat(this.wave_period) / 1000) + parseFloat(this.phase)) * parseFloat(this.amplitude);
return tempData;
}
}
class ConstantGenerator extends Generator {
constructor(mean) {
super();
this.mean = mean;
}
generateValues(t) {
let tempData = parseFloat(this.mean);
return tempData;
}
}
class IncreasingGenerator extends Generator {
constructor(slope) {
super();
this.slope = slope;
}
generateValues(t) {
let tempData = parseFloat(this.slope) * t;
return tempData;
}
}
class RandomGenerator extends Generator {
constructor(min, max) {
super();
this.min = min;
this.max = max;
}
generateValues(t) {
const mean = (parseFloat(this.min) + parseFloat(this.max)) / 2;
const constObj = new ConstantGenerator(mean);
let tempData = constObj.generateValues(t);
const noise_magnitude = (Math.random() * (parseFloat(this.max) - parseFloat(this.min))) / 2;
const noiseSd = 0.45;
const noiseObj = new NoiseGenerator(noise_magnitude, noiseSd);
tempData += noiseObj.generateValues(t);
return tempData;
}
}
class BooleanGenerator extends Generator {
constructor() {
super();
}
generateValues(t) {
let tempData = (Math.random() >= 0.5 ? 0 : 1);
return tempData;
}
}
class NoiseGenerator extends Generator {
constructor(noise_magnitude, noiseSd) {
super();
this.noise_magnitude = noise_magnitude;
this.noiseSd = noiseSd;
}
generateValues(t) {
if (this.noise_magnitude !== undefined && this.noiseSd !== undefined) {
let mean = 0;
let standard_deviation = parseFloat(this.noiseSd);
// Taken reference from this link for range of x->https://en.wikipedia.org/wiki/Normal_distribution#/media/File:Normal_Distribution_PDF.svg
let x = (Math.random() - 0.5) * 2;
// Taken reference from this link for noise value->https://en.wikipedia.org/wiki/Normal_distribution
let noise = (1 / (Math.sqrt(2 * Math.PI) * standard_deviation)) * Math.pow(Math.E, -1 * Math.pow((x - mean) / standard_deviation, 2) / 2);
let noise_magnitude = parseFloat(this.noise_magnitude) * Math.sign((Math.random() - 0.5) * 2);
return noise * noise_magnitude;
}
return 0;
}
}
class TriangularGenerator extends Generator {
constructor(amplitude, wave_period) {
super();
this.amplitude = amplitude;
this.wave_period = wave_period;
}
generateValues(t) {
let tempData = 4 * parseFloat(this.amplitude) / (parseFloat(this.wave_period) / 1000) * Math.abs((((t - (parseFloat(this.wave_period) / 1000) / 4) % (parseFloat(this.wave_period) / 1000)) + (parseFloat(this.wave_period) / 1000)) % (parseFloat(this.wave_period) / 1000) - (parseFloat(this.wave_period) / 1000) / 2) - parseFloat(this.amplitude);
return tempData;
}
}
class SawtoothGenerator extends Generator {
constructor(amplitude, wave_period) {
super();
this.amplitude = amplitude;
this.wave_period = wave_period;
}
generateValues(t) {
let tempData = 2 * parseFloat(this.amplitude) * (t / (parseFloat(this.wave_period) / 1000) - Math.floor(1 / 2 + t / (parseFloat(this.wave_period) / 1000)));
return tempData;
}
}
class SquareGenerator extends Generator {
constructor(amplitude, wave_period) {
super();
this.amplitude = amplitude;
this.wave_period = wave_period;
}
generateValues(t) {
let tempData = parseFloat(this.amplitude) * Math.sign(Math.sin((2 * Math.PI * t) / (parseFloat(this.wave_period) / 1000)));
return tempData;
}
}
module.exports = { SineGenerator, ConstantGenerator, IncreasingGenerator, NoiseGenerator, TriangularGenerator, SawtoothGenerator, SquareGenerator, RandomGenerator, BooleanGenerator }