Skip to content

Commit cd7b819

Browse files
committed
add trapezium()
1 parent 8300f51 commit cd7b819

File tree

8 files changed

+625
-18
lines changed

8 files changed

+625
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99
## [0.2.7] - 2024-07-05
1010
- add **float sinusDiode(float t)**
1111
- add **float sinusRectified(float t)**
12+
- add **float trapezium1(float t)** with duty cycle.
13+
- add **float trapezium2(float t)** with duty cycle.
14+
- update **functionGeneratorPerformance.ino**
15+
- fix Arduino-lint.yml (long library name error)
1216
- minor edits
1317

1418
## [0.2.6] - 2023-11-02

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ have become slightly slower.
7272
| Arduino UNO | 16 MHz | square | 57 | 1000 Hz |
7373
| Arduino UNO | 16 MHz | random_DC | 68 | 500 Hz |
7474

75+
See **functionGeneratorPerformance.ino**
76+
7577

7678
#### Accuracy
7779

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

148146

147+
Experimental 0.2.7
148+
149+
- **float sinusDiode(float t)** sinus wave, only positive pulses.
150+
(better name welcome).
151+
- **float sinusRectified(float t)** sinus wave, with "abs(negative pulses)".
152+
(better name welcome).
153+
- **float trapezium1(float t)** trapezium wave, DutyCycle changes steepness falling rising.
154+
- **float trapezium2(float t)** trapezium wave, DutyCycle changes period HIGH vs LOW
155+
156+
Note at 50% DC the two trapezium functions are identical.
157+
149158
#### Duty Cycle
150159

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

183+
#### No duty cycle
168184

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

179198
Feedback and ideas are welcome.
180199

181200

182201
## Future
183202

184-
185203
#### Must
186204

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

192-
193210
#### Should
194211

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

198-
199215
#### Could
200216

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

214-
215231
#### Examples
216232

217233
- Amplitude modulation ?

examples/functionGeneratorPerformance/functionGeneratorPerformance.ino

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ funcgen gen;
2020
void setup()
2121
{
2222
Serial.begin(115200);
23-
Serial.print("Start functionGeneratorPerformance - LIB VERSION: ");
23+
Serial.println(__FILE__);
24+
Serial.print("FUNCTIONGENERATOR_LIB_VERSION: ");
2425
Serial.println(FUNCTIONGENERATOR_LIB_VERSION);
26+
Serial.println();
2527

26-
Serial.println("func \t\tusec\tmax calls/sec");
28+
Serial.println("func \t\tusec\tmax calls/sec (indicative)");
2729
y = analogRead(A0) / 1024;
2830
test_square();
2931
delay(10);
@@ -43,9 +45,15 @@ void setup()
4345
delay(10);
4446
test_zero();
4547
delay(10);
48+
test_sinusDiode();
49+
delay(10);
50+
test_sinusRectified();
51+
delay(10);
52+
test_trapezium2();
53+
delay(10);
4654
Serial.println();
4755

48-
Serial.println("t \t sqr\t saw\t tri\t sin\t str\t rnd\t line\t zero");
56+
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");
4957
for (int i = -400; i < 400; i += 2)
5058
{
5159
float t = i * 0.01;
@@ -66,6 +74,12 @@ void setup()
6674
Serial.print(gen.line());
6775
Serial.print("\t");
6876
Serial.print(gen.zero());
77+
Serial.print("\t");
78+
Serial.print(gen.sinusDiode(t));
79+
Serial.print("\t");
80+
Serial.print(gen.sinusRectified(t));
81+
Serial.print("\t");
82+
Serial.print(gen.trapezium2(t));
6983
Serial.println();
7084
}
7185
Serial.println("\ndone...");
@@ -218,10 +232,57 @@ void test_zero()
218232
}
219233

220234

235+
void test_sinusDiode()
236+
{
237+
start = micros();
238+
for (int i = 0; i < 10000; i++)
239+
{
240+
t = gen.sinusDiode(i);
241+
}
242+
stop = micros();
243+
Serial.print(__FUNCTION__);
244+
Serial.print(":\t");
245+
Serial.print((stop - start) / 10000.0);
246+
Serial.print("\t");
247+
Serial.println(1000000.0 / ((stop - start) / 10000.0));
248+
}
249+
250+
251+
void test_sinusRectified()
252+
{
253+
start = micros();
254+
for (int i = 0; i < 10000; i++)
255+
{
256+
t = gen.sinusRectified(i);
257+
}
258+
stop = micros();
259+
Serial.print(__FUNCTION__);
260+
Serial.print(":\t");
261+
Serial.print((stop - start) / 10000.0);
262+
Serial.print("\t");
263+
Serial.println(1000000.0 / ((stop - start) / 10000.0));
264+
}
265+
266+
267+
void test_trapezium2()
268+
{
269+
start = micros();
270+
for (int i = 0; i < 10000; i++)
271+
{
272+
t = gen.trapezium2(i);
273+
}
274+
stop = micros();
275+
Serial.print(__FUNCTION__);
276+
Serial.print(":\t");
277+
Serial.print((stop - start) / 10000.0);
278+
Serial.print("\t");
279+
Serial.println(1000000.0 / ((stop - start) / 10000.0));
280+
}
281+
282+
221283
void loop()
222284
{
223285
}
224286

225287

226288
// -- END OF FILE --
227-

0 commit comments

Comments
 (0)