-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctionGenerator.cpp
626 lines (514 loc) · 12.4 KB
/
functionGenerator.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//
// FILE: functionGenerator.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// PURPOSE: wave form generating functions (use with care)
// URL: https://github.com/RobTillaart/FunctionGenerator
#include "functionGenerator.h"
funcgen::funcgen(float period, float amplitude, float phase, float yShift)
{
setPeriod(period);
setAmplitude(amplitude);
setPhase(phase);
setYShift(yShift);
setDutyCycle(50); // TODO param?
}
/////////////////////////////////////////////////////////////
//
// CONFIGURATION
//
void funcgen::setPeriod(float period)
{
_period = period;
_freq1 = 1 / period;
_freq2 = 2 * _freq1;
_freq4 = 4 * _freq1;
_freq0 = TWO_PI * _freq1;
}
float funcgen::getPeriod()
{
return _period;
}
void funcgen::setFrequency(float freq)
{
setPeriod(1.0 / freq);
}
float funcgen::getFrequency()
{
return _freq1;
}
void funcgen::setAmplitude(float ampl)
{
_amplitude = ampl;
}
float funcgen::getAmplitude()
{
return _amplitude;
}
void funcgen::setPhase(float phase)
{
_phase = phase;
}
float funcgen::getPhase()
{
return _phase;
}
void funcgen::setYShift(float yShift)
{
_yShift = yShift;
}
float funcgen::getYShift()
{
return _yShift;
}
void funcgen::setDutyCycle(float dutyCycle)
{
// negative dutyCycle? => 1-dc? or abs()?
if (dutyCycle < 0) _dutyCycle = 0.0;
else if (dutyCycle > 100) _dutyCycle = 1.0;
else _dutyCycle = dutyCycle * 0.01;
}
float funcgen::getDutyCycle()
{
return _dutyCycle * 100.0;
}
void funcgen::setRandomSeed(uint32_t a, uint32_t b)
{
// prevent zero loops in random() function.
if (a == 0) a = 123;
if (b == 0) b = 456;
_m_w = a;
_m_z = b;
}
/////////////////////////////////////////////////////////////
//
// FUNCTIONS
//
float funcgen::line()
{
return _yShift + _amplitude;
}
float funcgen::zero()
{
return 0;
}
float funcgen::sawtooth(float t, uint8_t mode)
{
float rv;
t += _phase;
if (t >= 0.0)
{
t = fmod(t, _period);
if (mode == 1) t = _period - t;
rv = _amplitude * (-1.0 + t *_freq2);
}
else
{
t = -t;
t = fmod(t, _period);
if (mode == 1) t = _period - t;
rv = _amplitude * ( 1.0 - t * _freq2);
}
rv += _yShift;
return rv;
}
float funcgen::triangle(float t)
{
float rv;
t += _phase;
if (t < 0.0)
{
t = -t;
}
t = fmod(t, _period);
if (t < (_period * _dutyCycle))
{
rv = _amplitude * (-1.0 + t * _freq2 / _dutyCycle);
}
else
{
// mirror math
t = _period - t;
rv = _amplitude * (-1.0 + t * _freq2 /(1 - _dutyCycle));
}
rv += _yShift;
return rv;
}
float funcgen::square(float t)
{
float rv;
t += _phase;
if (t >= 0)
{
t = fmod(t, _period);
if (t < (_period * _dutyCycle)) rv = _amplitude;
else rv = -_amplitude;
}
else
{
t = -t;
t = fmod(t, _period);
if (t < (_period * _dutyCycle)) rv = -_amplitude;
else rv = _amplitude;
}
rv += _yShift;
return rv;
}
float funcgen::sinus(float t)
{
float rv;
t += _phase;
rv = _amplitude * sin(t * _freq0);
rv += _yShift;
return rv;
}
float funcgen::stair(float t, uint16_t steps, uint8_t mode)
{
t += _phase;
if (t >= 0)
{
t = fmod(t, _period);
if (mode == 1) t = _period - t;
int level = steps * t / _period;
return _yShift + _amplitude * (-1.0 + 2.0 * level / (steps - 1));
}
t = -t;
t = fmod(t, _period);
if (mode == 1) t = _period - t;
int level = steps * t / _period;
return _yShift + _amplitude * (1.0 - 2.0 * level / (steps - 1));
}
float funcgen::random()
{
float rv = _yShift + _amplitude * _random() * 0.2328306436E-9; // div 0xFFFFFFFF
return rv;
}
// duty cycle variant takes more than twice as much time.
float funcgen::random_DC()
{
static float rv = 0;
float next = _yShift + _amplitude * _random() * 0.2328306436E-9; // div 0xFFFFFFFF
rv += (next - rv) * _dutyCycle;
return rv;
}
/////////////////////////////////////////////////////////////
//
// EXPERIMENTAL 0.2.7
//
float funcgen::sinusDiode(float t)
{
float rv = sinus(t);
if (rv < _yShift) return _yShift;
return rv;
// float rv;
// t += _phase;
// rv = sin(t * _freq0);
// if (rv < 0) return _yShift;
// rv *= amplitude;
// rv += _yShift;
// return rv;
}
float funcgen::sinusRectified(float t)
{
// float rv = sinus(t);
// if (rv < _yShift) return _yShift - rv;
// return rv;
float rv;
t += _phase;
rv = _amplitude * sin(t * _freq0);
if (rv < 0) rv = -rv;
rv += _yShift;
return rv;
}
float funcgen::trapezium1(float t)
{
t += _phase + _period * _dutyCycle / 4; // zero point for t = 0
if (t < 0)
{
t = -t;
}
t = fmod(t, _period);
if (t < _period * 0.5 * _dutyCycle) // rising part
{
return _yShift + -_amplitude + 2 * _amplitude * (t * 2 / (_period * _dutyCycle));
}
else if (t < _period * 0.5) // high part
{
return _yShift + _amplitude;
}
else if (t < _period * (0.5 + 0.5 * _dutyCycle)) // falling part
{
return _yShift + _amplitude - 2 * _amplitude * ( (t * 2 - _period) / (_period * _dutyCycle));
}
else // low part
{
return _yShift + -_amplitude;
}
}
float funcgen::trapezium2(float t)
{
t += _phase + _period * _dutyCycle / 4; // zero point for t = 0
if (t < 0)
{
t = -t;
}
t = fmod(t, _period);
if (t < _period * 0.25) // rising part
{
return _yShift + -_amplitude + 2 * _amplitude * (t * 4 / _period);
}
else if (t < _period * (0.25 + 0.5 * _dutyCycle)) // high part
{
return _yShift + _amplitude;
}
else if (t < _period * (0.5 + 0.5 * _dutyCycle)) // falling part
{
return _yShift + _amplitude - 2 * _amplitude * ((t - _period * (0.25 + 0.5 * _dutyCycle)) * 4 / _period);
}
else // low part
{
return _yShift + -_amplitude;
}
}
/*
// no DC version (50%
float funcgen::trapezium(float t)
{
t += _phase;
if (t < 0)
{
t = -t;
}
t = fmod(t, _period);
if (t < _period * 0.25) // rising part
{
return -_amplitude + 2 * _amplitude * (t * 4 / _period);
}
else if (t < _period * 0.5) // high part
{
return _amplitude;
}
else if (t < _period * 0.75) // high part
{
return _amplitude - 2 * _amplitude * ((t - _period/2) * 4 / _period);
}
else // low part
{
return -_amplitude;
}
}
*/
//
// EXPERIMENTAL HEARTBEAT
// => setFrequency(72.0 / 60.0); // BPM/60 = BPS.
float funcgen::heartBeat(float t)
{
int16_t out[32] = {
0, 0, 1000, 2500,
1000, 1000, -50, 10000,
-2500, 2000, 2500, 3000,
3000, 2000, 0, 0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0,
};
// use duty cycle to determine zero level duration.
int pts = map(_dutyCycle * 100, 0, 100, 31, 15);
return freeWave(t, out, pts);
}
/*
// points need to be optimized,
// 0.2.7 uses 160 bytes for the two arrays.
// wrapper around freeWave?
float funcgen::heartBeat(float t)
{
// based upon MultiMap in[] array is normalized to 0.0 - 1.0
// Heart beat phase P Q R S T U
float in[21] = { 0.0, 0.07, 0.13, 0.20, 0.27, 0.33, 0.40, 0.46, 0.53, 0.60, 0.66, 0.73, 0.80, 0.86, 0.93, 1.00 };
float out[21] = { 0.0, 0.00, 0.10, 0.25, 0.10, 0.10, -0.05, 1.00, -0.25, 0.20, 0.25, 0.30, 0.30, 0.20, 0.00, 0.00 };
t += _phase;
t = fmod(t, _period);
// normalize t to 0.0 - 1.0
t *= _freq1;
// search interval
int idx = 0;
while (t > in[idx]) idx++;
if (t == in[idx]) return _yShift + _amplitude * out[idx];
idx--;
// interpolate.
float factor = (t - in[idx]) / (in[idx+1] - in[idx]);
return _yShift + _amplitude * (out[idx] + factor * (out[idx+1] - out[idx]));
}
*/
float funcgen::freeWave(float t, int16_t * arr, int16_t size)
{
t += _phase;
// normalize t to 0.0 - 1.0
t = fmod(t, _period);
t *= _freq1;
// search interval, as arr is based upon N equidistant points,
// we can easily calculate the points for direct access
float factor = t * size;
int idx = factor; // truncate to get index of output array.
factor = factor - idx; // remainder is interpolate factor.
// interpolate.
return _yShift + _amplitude * 1e-4 * (arr[idx] + factor * (arr[idx+1] - arr[idx]));
}
/////////////////////////////////////////////////////////////
//
// PRIVATE
//
// An example of a simple pseudo-random number generator is the
// Multiply-with-carry method invented by George Marsaglia.
// two initializers (not null)
uint32_t funcgen::_random()
{
_m_z = 36969L * (_m_z & 65535L) + (_m_z >> 16);
_m_w = 18000L * (_m_w & 65535L) + (_m_w >> 16);
return (_m_z << 16) + _m_w; /* 32-bit result */
}
/////////////////////////////////////////////////////////////
//
// INTEGER VERSIONS FOR 8 BIT DAC
//
// 8 bits version
// t = 0..9999 period 10000 in millis, returns 0..255
/*
uint8_t ifgsaw(uint16_t t, uint16_t period = 1000)
{
return 255L * t / period;
}
uint8_t ifgtri(uint16_t t, uint16_t period = 1000)
{
if (t * 2 < period) return 510L * t / period;
return 255L - 510L * t / period;
}
uint8_t ifgsqr(uint16_t t, uint16_t period = 1000)
{
if (t * 2 < period) return 510L * t / period;
return 255L - 510L * t / period;
}
uint8_t ifgsin(uint16_t t, uint16_t period = 1000)
{
return sin(355L * t / period / 113); // LUT
}
uint8_t ifgstr(uint16_t t, uint16_t period = 1000, uint16_t steps = 8)
{
int level = 1L * steps * t / period;
return 255L * level / (steps - 1);
}
*/
/////////////////////////////////////////////////////////////
//
// SIMPLE float ONES
//
// t = 0..period
// period = 0.001 ... 10000 ?
/*
float fgsaw(float t, float period = 1.0)
{
if (t >= 0) return -1.0 + 2 * t / period;
return 1.0 + 2 * t / period;
}
float fgtri(float t, float period = 1.0)
{
if (t < 0) t = -t;
if (t * 2 < period) return -1.0 + 4 * t / period;
return 3.0 - 4 * t / period;
}
float fgsqr(float t, float period = 1.0)
{
if (t >= 0)
{
if ( 2 * t < period) return 1.0;
return -1.0;
}
t = -t;
if (2 * t < period) return -1.0;
return 1.0;
}
float fgsin(float t, float period = 1.0)
{
return sin(TWO_PI * t / period);
}
float fgstr(float t, float period = 1.0, uint16_t steps = 8)
{
if (t >= 0)
{
int level = steps * t / period;
return -1.0 + 2.0 * level / (steps - 1);
}
t = -t;
int level = steps * t / period;
return 1.0 - 2.0 * level / (steps - 1);
}
*/
/////////////////////////////////////////////////////////////
//
// FULL floatS ONES
//
// SAWTOOTH
float fgsaw(float t, float period = 1.0, float amplitude = 1.0, float phase = 0.0, float yShift = 0.0)
{
t += phase;
if (t >= 0)
{
if (t >= period) t = fmod(t, period);
return yShift + amplitude * (-1.0 + 2 * t / period);
}
t = -t;
if (t >= period) t = fmod(t, period);
return yShift + amplitude * ( 1.0 - 2 * t / period);
}
// TRIANGLE
float fgtri(float t, float period = 1.0, float amplitude = 1.0, float phase = 0.0, float yShift = 0.0, float dutyCycle = 0.50)
{
t += phase;
if (t < 0) t = -t;
if (t >= period) t = fmod(t, period);
// 50 % dutyCycle = faster
// if (t * 2 < period) return yShift + amplitude * (-1.0 + 4 * t / period);
// return yShift + amplitude * (3.0 - 4 * t / period);
if (t < dutyCycle * period) return yShift + amplitude * (-1.0 + 2 * t / (dutyCycle * period));
// return yShift + amplitude * (-1.0 + 2 / (1 - dutyCycle) - 2 * t / ((1 - dutyCycle) * period));
return yShift + amplitude * (-1.0 + 2 / (1 - dutyCycle) * ( 1 - t / period));
}
// SQUARE
float fgsqr(float t, float period = 1.0, float amplitude = 1.0, float phase = 0.0, float yShift = 0.0, float dutyCycle = 0.50)
{
t += phase;
if (t >= 0)
{
if (t >= period) t = fmod(t, period);
if (t < dutyCycle * period) return yShift + amplitude;
return yShift - amplitude;
}
t = -t;
if (t >= period) t = fmod(t, period);
if (t < dutyCycle * period) return yShift - amplitude;
return yShift + amplitude;
}
// SINUS
float fgsin(float t, float period = 1.0, float amplitude = 1.0, float phase = 0.0, float yShift = 0.0)
{
t += phase;
float rv = yShift + amplitude * sin(TWO_PI * t / period);
return rv;
}
// STAIR
float fgstr(float t, float period = 1.0, float amplitude = 1.0, float phase = 0.0, float yShift = 0.0, uint16_t steps = 8)
{
t += phase;
if (t >= 0)
{
if (t >= period) t = fmod(t, period);
int level = steps * t / period;
return yShift + amplitude * (-1.0 + 2.0 * level / (steps - 1));
}
t = -t;
if (t >= period) t = fmod(t, period);
int level = steps * t / period;
return yShift + amplitude * (1.0 - 2.0 * level / (steps - 1));
}
// -- END OF FILE --