Skip to content

Commit 7990ec3

Browse files
committed
add mm()
1 parent afd0218 commit 7990ec3

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

examples/functionGeneratorPlotter/functionGeneratorPlotter.ino

+8-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ void setup()
2121
// Serial.println(FUNCTIONGENERATOR_LIB_VERSION);
2222
// Serial.println();
2323

24-
gen.setAmplitude(50);
25-
gen.setFrequency(1);
26-
gen.setDutyCycle(100);
24+
gen.setAmplitude(80);
25+
gen.setFrequency(65.0/60.0); // HB = BPM/60.0
26+
gen.setDutyCycle(50);
2727
}
2828

2929

@@ -54,9 +54,11 @@ void loop()
5454
// Serial.print("\t");
5555
// Serial.print(gen.sinusRectified(t));
5656
// Serial.print("\t");
57-
Serial.print(gen.trapezium1(t));
58-
Serial.print("\t");
59-
Serial.print(gen.trapezium2(t));
57+
// Serial.print(gen.trapezium1(t));
58+
// Serial.print("\t");
59+
// Serial.print(gen.trapezium2(t));
60+
// Serial.print("\t");
61+
Serial.print(gen.mm(t));
6062
// Serial.print("\t");
6163
// Serial.print(gen.stair(t, 16, 0)); // step up
6264
// Serial.print("\t");

functionGenerator.cpp

+40-10
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ float funcgen::random_DC()
273273

274274
float funcgen::trapezium1(float t)
275275
{
276-
t += _phase + _period / 8; // zero point for t = 0
276+
t += _phase + _period * _dutyCycle / 4; // zero point for t = 0
277277
if (t < 0)
278278
{
279279
t = -t;
@@ -282,26 +282,26 @@ float funcgen::trapezium1(float t)
282282

283283
if (t < _period * 0.5 * _dutyCycle) // rising part
284284
{
285-
return -_amplitude + 2 * _amplitude * (t * 2 / (_period * _dutyCycle));
285+
return _yShift + -_amplitude + 2 * _amplitude * (t * 2 / (_period * _dutyCycle));
286286
}
287287
else if (t < _period * 0.5) // high part
288288
{
289-
return _amplitude;
289+
return _yShift + _amplitude;
290290
}
291291
else if (t < _period * (0.5 + 0.5 * _dutyCycle)) // falling part
292292
{
293-
return _amplitude - 2 * _amplitude * ( (t * 2 - _period) / (_period * _dutyCycle));
293+
return _yShift + _amplitude - 2 * _amplitude * ( (t * 2 - _period) / (_period * _dutyCycle));
294294
}
295295
else // low part
296296
{
297-
return -_amplitude;
297+
return _yShift + -_amplitude;
298298
}
299299
}
300300

301301

302302
float funcgen::trapezium2(float t)
303303
{
304-
t += _phase + _period / 8; // zero point for t = 0
304+
t += _phase + _period * _dutyCycle / 4; // zero point for t = 0
305305
if (t < 0)
306306
{
307307
t = -t;
@@ -310,19 +310,19 @@ float funcgen::trapezium2(float t)
310310

311311
if (t < _period * 0.25) // rising part
312312
{
313-
return -_amplitude + 2 * _amplitude * (t * 4 / _period);
313+
return _yShift + -_amplitude + 2 * _amplitude * (t * 4 / _period);
314314
}
315315
else if (t < _period * (0.25 + 0.5 * _dutyCycle)) // high part
316316
{
317-
return _amplitude;
317+
return _yShift + _amplitude;
318318
}
319319
else if (t < _period * (0.5 + 0.5 * _dutyCycle)) // falling part
320320
{
321-
return _amplitude - 2 * _amplitude * ((t - _period * (0.25 + 0.5 * _dutyCycle)) * 4 / _period);
321+
return _yShift + _amplitude - 2 * _amplitude * ((t - _period * (0.25 + 0.5 * _dutyCycle)) * 4 / _period);
322322
}
323323
else // low part
324324
{
325-
return -_amplitude;
325+
return _yShift + -_amplitude;
326326
}
327327
}
328328

@@ -331,6 +331,7 @@ float funcgen::trapezium2(float t)
331331
// no DC version (50%
332332
float funcgen::trapezium(float t)
333333
{
334+
t += _phase;
334335
if (t < 0)
335336
{
336337
t = -t;
@@ -357,6 +358,35 @@ float funcgen::trapezium(float t)
357358
*/
358359

359360

361+
//
362+
// EXPERIMENTAL HEARTBEAT (FREQ = 72.0 / 60.0 ~ 1.2
363+
// based upon MultiMap
364+
// in array is normalized to 0.0 - 1.0
365+
//
366+
// Heart beat phase P Q R S T U
367+
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 };
368+
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 };
369+
370+
371+
float funcgen::mm(float t)
372+
{
373+
t += _phase;
374+
t = fmod(t, _period);
375+
376+
// normalize t to 0.0 - 1.0
377+
t *= _freq1;
378+
// search interval
379+
int idx = 0;
380+
while (t > in[idx]) idx++;
381+
if (t == in[idx]) return _yShift + _amplitude * out[idx];
382+
idx--;
383+
// interpolate.
384+
float factor = (t - in[idx]) / (in[idx+1] - in[idx]);
385+
return _yShift + _amplitude * (out[idx] + factor * (out[idx+1] - out[idx]));
386+
}
387+
388+
389+
360390
/////////////////////////////////////////////////////////////
361391
//
362392
// PRIVATE

functionGenerator.h

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class funcgen
6262
float stair(float t, uint16_t steps = 8, uint8_t mode = 0);
6363
float trapezium1(float t); // Experimental.
6464
float trapezium2(float t); // Experimental.
65+
float mm(float t); // Experimental.
6566

6667
float random();
6768
float random_DC(); // duty cycle variant. Experimental.

0 commit comments

Comments
 (0)