C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_simple_example_13.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * Simple Example 13 *
6 * Author: Arash Partow (1999-2024) *
7 * URL: https://www.partow.net/programming/exprtk/index.html *
8 * *
9 * Copyright notice: *
10 * Free use of the Mathematical Expression Toolkit Library is *
11 * permitted under the guidelines and in accordance with the *
12 * most current version of the MIT License. *
13 * https://www.opensource.org/licenses/MIT *
14 * SPDX-License-Identifier: MIT *
15 * *
16 **************************************************************
17*/
18
19
20#include <cstdio>
21#include <cstdlib>
22#include <ctime>
23#include <string>
24
25#include "exprtk.hpp"
26
27
28template <typename T>
30{
31 typedef exprtk::symbol_table<T> symbol_table_t;
32 typedef exprtk::expression<T> expression_t;
33 typedef exprtk::parser<T> parser_t;
34
35 const std::string sgfilter_program =
36 " var weight[9] := "
37 " { "
38 " -21, 14, 39, "
39 " 54, 59, 54, "
40 " 39, 14, -21 "
41 " }; "
42 " "
43 " if (v_in[] >= weight[]) "
44 " { "
45 " const var lower_bound := trunc(weight[] / 2); "
46 " const var upper_bound := v_in[] - lower_bound; "
47 " "
48 " v_out := 0; "
49 " "
50 " for (var i := lower_bound; i < upper_bound; i += 1) "
51 " { "
52 " for (var j := -lower_bound; j <= lower_bound; j += 1) "
53 " { "
54 " v_out[i] += weight[j + lower_bound] * v_in[i + j]; "
55 " }; "
56 " }; "
57 " "
58 " v_out /= sum(weight); "
59 " } ";
60
61 const std::size_t n = 1024;
62
63 std::vector<T> v_in;
64 std::vector<T> v_out;
65
66 const T pi = T(3.141592653589793238462643383279502);
67
68 srand(static_cast<unsigned int>(time(0)));
69
70 // Generate a signal with noise.
71 for (T t = T(-5); t <= T(+5); t += T(10.0 / n))
72 {
73 const T noise = T(0.5 * (rand() / (RAND_MAX + 1.0) - 0.5));
74 v_in.push_back(sin(2.0 * pi * t) + noise);
75 }
76
77 v_out.resize(v_in.size());
78
79 symbol_table_t symbol_table;
80 symbol_table.add_vector("v_in" , v_in );
81 symbol_table.add_vector("v_out", v_out);
82
83 expression_t expression;
84 expression.register_symbol_table(symbol_table);
85
86 parser_t parser;
87 parser.compile(sgfilter_program,expression);
88
89 expression.value();
90
91 for (std::size_t i = 0; i < v_out.size(); ++i)
92 {
93 printf("%10.6f\t%10.6f\n", v_in[i], v_out[i]);
94 }
95}
96
97int main()
98{
99 savitzky_golay_filter<double>();
100 return 0;
101}
const double pi
void savitzky_golay_filter()