2013-04-14 07:01:14 +00:00
|
|
|
/*
|
|
|
|
**************************************************************
|
|
|
|
* C++ Mathematical Expression Toolkit Library *
|
|
|
|
* *
|
2024-01-01 00:00:00 +00:00
|
|
|
* Simple Example 02 *
|
|
|
|
* Author: Arash Partow (1999-2024) *
|
2023-01-01 02:18:35 +00:00
|
|
|
* URL: https://www.partow.net/programming/exprtk/index.html *
|
2013-04-14 07:01:14 +00:00
|
|
|
* *
|
|
|
|
* Copyright notice: *
|
|
|
|
* Free use of the Mathematical Expression Toolkit Library is *
|
|
|
|
* permitted under the guidelines and in accordance with the *
|
2017-02-21 21:44:24 +00:00
|
|
|
* most current version of the MIT License. *
|
2023-01-01 02:18:35 +00:00
|
|
|
* https://www.opensource.org/licenses/MIT *
|
2024-01-01 00:00:00 +00:00
|
|
|
* SPDX-License-Identifier: MIT *
|
2013-04-14 07:01:14 +00:00
|
|
|
* *
|
|
|
|
**************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
2020-01-01 00:00:00 +00:00
|
|
|
|
2013-04-14 07:01:14 +00:00
|
|
|
#include "exprtk.hpp"
|
|
|
|
|
|
|
|
|
2014-07-15 13:09:08 +00:00
|
|
|
template <typename T>
|
2013-04-14 07:01:14 +00:00
|
|
|
void square_wave()
|
|
|
|
{
|
2015-03-28 12:21:55 +00:00
|
|
|
typedef exprtk::symbol_table<T> symbol_table_t;
|
2021-01-01 00:00:00 +00:00
|
|
|
typedef exprtk::expression<T> expression_t;
|
|
|
|
typedef exprtk::parser<T> parser_t;
|
2015-03-28 12:21:55 +00:00
|
|
|
|
2024-01-01 00:00:00 +00:00
|
|
|
const std::string expression_string =
|
|
|
|
"a *(4 / pi) * "
|
|
|
|
"((1 / 1) * sin( 2 * pi * f * t) + (1 / 3) * sin( 6 * pi * f * t) + "
|
|
|
|
" (1 / 5) * sin(10 * pi * f * t) + (1 / 7) * sin(14 * pi * f * t) + "
|
|
|
|
" (1 / 9) * sin(18 * pi * f * t) + (1 / 11) * sin(22 * pi * f * t) + "
|
|
|
|
" (1 / 13) * sin(26 * pi * f * t) + (1 / 15) * sin(30 * pi * f * t) + "
|
|
|
|
" (1 / 17) * sin(34 * pi * f * t) + (1 / 19) * sin(38 * pi * f * t) + "
|
|
|
|
" (1 / 21) * sin(42 * pi * f * t) + (1 / 23) * sin(46 * pi * f * t) + "
|
|
|
|
" (1 / 25) * sin(50 * pi * f * t) + (1 / 27) * sin(54 * pi * f * t)) ";
|
2014-04-19 11:10:15 +00:00
|
|
|
|
2016-09-05 22:27:19 +00:00
|
|
|
static const T pi = T(3.141592653589793238462643383279502);
|
2014-04-19 11:10:15 +00:00
|
|
|
|
2017-05-05 02:16:16 +00:00
|
|
|
const T f = pi / T(10);
|
|
|
|
const T a = T(10);
|
|
|
|
T t = T(0);
|
2013-04-14 07:01:14 +00:00
|
|
|
|
2015-03-28 12:21:55 +00:00
|
|
|
symbol_table_t symbol_table;
|
2013-04-14 07:01:14 +00:00
|
|
|
symbol_table.add_variable("t",t);
|
2017-05-05 02:16:16 +00:00
|
|
|
symbol_table.add_constant("f",f);
|
|
|
|
symbol_table.add_constant("a",a);
|
2013-04-14 07:01:14 +00:00
|
|
|
symbol_table.add_constants();
|
|
|
|
|
2015-03-28 12:21:55 +00:00
|
|
|
expression_t expression;
|
2013-04-14 07:01:14 +00:00
|
|
|
expression.register_symbol_table(symbol_table);
|
|
|
|
|
2015-03-28 12:21:55 +00:00
|
|
|
parser_t parser;
|
2024-01-01 00:00:00 +00:00
|
|
|
parser.compile(expression_string,expression);
|
2013-04-14 07:01:14 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
const T delta = (T(4) * pi) / T(1000);
|
2014-04-19 11:10:15 +00:00
|
|
|
|
2013-07-28 11:35:06 +00:00
|
|
|
for (t = (T(-2) * pi); t <= (T(+2) * pi); t += delta)
|
2013-04-14 07:01:14 +00:00
|
|
|
{
|
2020-01-01 00:00:00 +00:00
|
|
|
const T result = expression.value();
|
|
|
|
printf("%19.15f\t%19.15f\n", t, result);
|
2013-04-14 07:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
square_wave<double>();
|
|
|
|
return 0;
|
|
|
|
}
|