-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal-generation.js
219 lines (201 loc) · 4.77 KB
/
signal-generation.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* (~) generate a random signal of a fixed length
* ( ) plot it
* ( ) add signal length to buffer class
*
*
*/
function plot1DArray(ary, x0 = 0.0, y0 = 0.0, xScale = 1.0, yScale = -1.0){
//could do this scaling with a map instead
push();
beginShape();
noFill();
ary.forEach((item,i)=>{
//vertex( (i+x0)*xScale, (item+y0)*yScale )
vertex( (i*xScale + x0), (item*yScale + y0) )
});
endShape();
pop();
}
function plotVecArray(vAry, v0=createVector(0,0))
{
beginShape();
noFill();
vAry.forEach((item, i) => {
//console.log(i)
vertex(item.x+v0.x, item.y+v0.y)
});
endShape();
}
class SignalBuffer{
constructor(len){
this.len = len;
this.signal = Array(len);
//have default x and y scales?
}
set_from_array(v_ary)
{
this.signal = v_ary;
this.len = v_ary.length;
}
plot(x0 = 0.0, y0 = 0.0 , xScale = 1.0 , yScale = -1.0, numpts = false){
//plotVecArray(this.signal, new p5.Vector(x0, y0));
let signal = this.signal;
if (numpts)
{
signal = get_last(this.signal, numpts);
}
plot1DArray(signal, x0, y0, xScale, yScale);
}
enque( val ) { this.signal.push(val); }
deque() { this.signal.shift(); }
cycle_in( val )
{
this.enque(val);
this.deque();
}
last() { return this.signal.at(-1); }
get_delayed(delay=1) {
if (delay>this.signal.length)
{
return this.signal[0];
}
return this.signal.at(-delay);
}
}
//does signal generator contain the whole network?
class SignalGenerator{
//consider turning this into an abstract base class:
//https://stackoverflow.com/questions/597769/how-do-i-create-an-abstract-base-class-in-javascript
constructor()
{
}
gen_sample(nt) {}
gen() {}
set_variance(v) {}
}
class GaussianGenerator extends SignalGenerator {
constructor(mu=0.0, sigma=1.0)
{
super();
this.mu = mu;
this.sigma = sigma;
this.type_str = 'Gaussian'
}
gen_sample()
{
return randomGaussian(this.mu, this.sigma)
}
gen(nt)
{
return Array(nt).fill().map( this.gen_sample , this)
}
set_variance( stdv )
{
this.sigma = stdv;
}
}
class PerlinGenerator extends SignalGenerator {
constructor(y_scale = 1.0, dx = 0.01, t_scale = 50.0, x0 = false){
super();
this.dx = dx;
this.t_scale = t_scale;
this.y_scale = y_scale;
this.y_bias = 0.0;
this.i = 0;
this.x0 = ( x0 ? x0 : this.rand_start() );
this.type_str = 'Perlin'
}
set_variance( y_scale )
{
this.y_scale = y_scale;
}
rand_start()
{
this.x0 = random(-100,100);
return this.x0;
}
gen_sample(i=false)
{
i = (i ? i : this.i)
i = this.i;
this.i++
let n = noise( (this.x0 + i*this.dx) * this.t_scale )
let scaled_noise = (n + this.y_bias)*this.y_scale;
return scaled_noise
}
gen(nt, do_rand_start = false)
{
if (do_rand_start) { this.rand_start() }
return Array(nt).fill().map( (_,i) => this.gen_sample(i), this )
}
}
class PoissonGenerator extends SignalGenerator {
constructor( lambda = 0.1 ) {
super();
this.lambda = lambda;
this.type_str = 'Poisson'
}
set_variance( lam )
{
this.lambda = constrain(lam, 0, 9999);
if (lam != this.lambda)
{
console.log( 'clipped lambda in construction of Poisson Generator' )
}
}
gen_sample()
{
//using inverse transform sampling algo. from wikipedia:
// https://en.wikipedia.org/wiki/Poisson_distribution#Generating_Poisson-distributed_random_variables
let x = 0;
let p = exp( -this.lambda );
let s = p;
let u = random()
while (u > s){
x++;
p *= this.lambda/x;
s += p;
}
return x;
}
gen( nt ){
return Array(nt).fill().map( this.gen_sample, this );
}
}
class MultiPoissonGenerator extends PoissonGenerator {
constructor( lambda = 0.1, n = 20, do_avg = true)
{
super(lambda);
this.n = n;
this.do_avg = do_avg;
this.type_str = `${n} Averaged Poisson`
}
gen_sample( do_avg = null)
{
if (do_avg == null) { do_avg = this.do_avg; };
let vals = Array(this.n).fill().map( super.gen_sample, this );
//compute the sum
let sum = vals.reduce( (a,b) => a+b, 0)
return ( do_avg ? sum/this.n : sum )
}
gen( nt , do_avg = null)
{
return Array(nt).fill().map( _=>this.gen_sample(do_avg), this);
}
}
// move this function into SignalGenerator?
function noise_gen_type_by_idx( idx = -1 )
{
//to avoid having to clone objects
//this only returns the function for generating the noise gen object
let noise_gen_funs = [
() => new GaussianGenerator(),
() => new PoissonGenerator(),
() => new MultiPoissonGenerator(),
() => new PerlinGenerator(),
];
idx = idx % noise_gen_funs.length;
let this_ngf = (idx<0 ? random(noise_gen_funs) : noise_gen_funs[idx] ) ;
return this_ngf;
}