Skip to content

Commit

Permalink
add trapezium()
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Jul 5, 2024
1 parent 8300f51 commit cd7b819
Show file tree
Hide file tree
Showing 8 changed files with 625 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.7] - 2024-07-05
- add **float sinusDiode(float t)**
- add **float sinusRectified(float t)**
- add **float trapezium1(float t)** with duty cycle.
- add **float trapezium2(float t)** with duty cycle.
- update **functionGeneratorPerformance.ino**
- fix Arduino-lint.yml (long library name error)
- minor edits

## [0.2.6] - 2023-11-02
Expand Down
34 changes: 25 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ have become slightly slower.
| Arduino UNO | 16 MHz | square | 57 | 1000 Hz |
| Arduino UNO | 16 MHz | random_DC | 68 | 500 Hz |

See **functionGeneratorPerformance.ino**


#### Accuracy

Expand Down Expand Up @@ -130,10 +132,6 @@ The variable t == time in seconds.
- **float triangle(float t)** triangle form, duty cycle default 50%.
- **float square(float t)** square wave with duty cycle default 50%.
- **float sinus(float t)** sinus wave, has no duty cycle.
- **float sinusDiode(float t)** sinus wave, only positive pulses.
(Better name welcome).
- **float sinusRectified(float t)** sinus wave, with "abs(negative pulses)".
(Better name welcome).
- **float stair(float t, uint16_t steps = 8, uint8_t mode = 0)** defaults to 8 steps up.
- mode = 0 ==> steps up
- mode = 1 ==> steps down. Effectively equals inverting the amplitude.
Expand All @@ -146,6 +144,17 @@ The functions **line()** and **zero()** can be used to drive a constant voltage
from a DAC and can be used to calibrate the generator / DAC combination.


Experimental 0.2.7

- **float sinusDiode(float t)** sinus wave, only positive pulses.
(better name welcome).
- **float sinusRectified(float t)** sinus wave, with "abs(negative pulses)".
(better name welcome).
- **float trapezium1(float t)** trapezium wave, DutyCycle changes steepness falling rising.
- **float trapezium2(float t)** trapezium wave, DutyCycle changes period HIGH vs LOW

Note at 50% DC the two trapezium functions are identical.

#### Duty Cycle

Since 0.2.5 the library has **experimental** support for duty cycle.
Expand All @@ -164,7 +173,14 @@ with respect to previous value.
Implemented as a weighed average between new and previous value.
Made a separate function as handling the duty cycle slows performance substantial.
Initial starts at zero and can be adjusted with **YShift()**.
- **float trapezium1(float t)** The duty cycle determines the steepness of the rising
and falling edges. This changes the form from square wave to trapezium to triangle.
The length of the HIGH LOW level go from 0 to half a period.
- **float trapezium2(float t)** The duty cycle determines the length of the HIGH level,
which is 0 for 0% DC and half a period for 100% DC.
The rising and falling edges stay same.

#### No duty cycle

The other functions need to be investigated what duty cycle means.
Current ideas that are **NOT** implemented:
Expand All @@ -175,32 +191,33 @@ Think of it as the halve of the triangle wave.
- **stair()** like sawtooth??
- **line()** has no period so does not make sense (yet).
- **zero()** has no period so does not make sense (yet).
- **float sinusDiode(float t)**
- **float sinusRectified(float t)**


Feedback and ideas are welcome.


## Future


#### Must

- documentation
- quality of signals - after substantial time t
- max freq per wave form etc.
Should this be in the library?


#### Should

- smart reseed needed for random().
- initialize random generator with compile time.


#### Could

- waves
- trapezium wave (could merge square and triangle and sawtooth)
- white noise, pink noise etc.
- heart beat (quite complex but fun)
- multiMap interpolation point curves.
- RC function curve.
- external clock to synchronize two or more software function generators.
- stand-alone functions in separate .h
Expand All @@ -211,7 +228,6 @@ Feedback and ideas are welcome.
- add float variable for ```_perDC = _period * _dutyCycle```
- do we need **freq4** ? not since DC.


#### Examples

- Amplitude modulation ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ funcgen gen;
void setup()
{
Serial.begin(115200);
Serial.print("Start functionGeneratorPerformance - LIB VERSION: ");
Serial.println(__FILE__);
Serial.print("FUNCTIONGENERATOR_LIB_VERSION: ");
Serial.println(FUNCTIONGENERATOR_LIB_VERSION);
Serial.println();

Serial.println("func \t\tusec\tmax calls/sec");
Serial.println("func \t\tusec\tmax calls/sec (indicative)");
y = analogRead(A0) / 1024;
test_square();
delay(10);
Expand All @@ -43,9 +45,15 @@ void setup()
delay(10);
test_zero();
delay(10);
test_sinusDiode();
delay(10);
test_sinusRectified();
delay(10);
test_trapezium2();
delay(10);
Serial.println();

Serial.println("t \t sqr\t saw\t tri\t sin\t str\t rnd\t line\t zero");
Serial.println("t \t sqr\t saw\t tri\t sin\t str\t rnd\t line\t zero\t sinD\t sinR\t trap");
for (int i = -400; i < 400; i += 2)
{
float t = i * 0.01;
Expand All @@ -66,6 +74,12 @@ void setup()
Serial.print(gen.line());
Serial.print("\t");
Serial.print(gen.zero());
Serial.print("\t");
Serial.print(gen.sinusDiode(t));
Serial.print("\t");
Serial.print(gen.sinusRectified(t));
Serial.print("\t");
Serial.print(gen.trapezium2(t));
Serial.println();
}
Serial.println("\ndone...");
Expand Down Expand Up @@ -218,10 +232,57 @@ void test_zero()
}


void test_sinusDiode()
{
start = micros();
for (int i = 0; i < 10000; i++)
{
t = gen.sinusDiode(i);
}
stop = micros();
Serial.print(__FUNCTION__);
Serial.print(":\t");
Serial.print((stop - start) / 10000.0);
Serial.print("\t");
Serial.println(1000000.0 / ((stop - start) / 10000.0));
}


void test_sinusRectified()
{
start = micros();
for (int i = 0; i < 10000; i++)
{
t = gen.sinusRectified(i);
}
stop = micros();
Serial.print(__FUNCTION__);
Serial.print(":\t");
Serial.print((stop - start) / 10000.0);
Serial.print("\t");
Serial.println(1000000.0 / ((stop - start) / 10000.0));
}


void test_trapezium2()
{
start = micros();
for (int i = 0; i < 10000; i++)
{
t = gen.trapezium2(i);
}
stop = micros();
Serial.print(__FUNCTION__);
Serial.print(":\t");
Serial.print((stop - start) / 10000.0);
Serial.print("\t");
Serial.println(1000000.0 / ((stop - start) / 10000.0));
}


void loop()
{
}


// -- END OF FILE --

Loading

0 comments on commit cd7b819

Please sign in to comment.