Sawtooth and triangle wave generator
8 sierpnia 2010 13:52
TriangleGenerator class can be used to create sawtooth and triangle waveforms. The default behaviour is to generate a sawtooth wave, where a rising slope fills the entire period of a signal. However, it can be changed by calling setWidth(), which sets width of the rising slope as a fraction of period. For example, setting slope width of 0.5 results in a symmetrical triangle wave.
#include "aquila/source/generator/TriangleGenerator.h" #include "aquila/tools/TextPlot.h" int main(int argc, char *argv[]) { Aquila::TriangleGenerator generator(1000); generator.setFrequency(25).setAmplitude(255).generate(64); Aquila::TextPlot plot("Sawtooth wave"); plot.plot(generator); generator.setWidth(0.5).generate(64); plot.setTitle("Triangle wave, slope width = 50%"); plot.plot(generator); generator.setWidth(0.18).generate(64); plot.setTitle("Triangle wave, slope width = 18%"); plot.plot(generator); return 0; }
Output:
Sawtooth wave
***
**
***
**
***
**
*** **
** **
** **
*** ***
** **
*** ***
** **
*** ***
** **
*** ***
Triangle wave, slope width = 50%
*** ***
* * * *
* * * *
* * *
** ** **
* * *
* * *
* * *
** ** **
* * *
* * *
* * *
** ** **
* * *
* * *
** ***
Triangle wave, slope width = 18%
*** ***
** **
* ** * **
** **
* ** * **
** **
* ** * **
** **
**
* ** *
**
* ** *
**
* ** *
**
* ***