OpcUaCanOpen
CANopen OPC-UA server
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-2019) *
7  * URL: http://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  * http://www.opensource.org/licenses/MIT *
14  * *
15  **************************************************************
16 */
17 
18 
19 #include <cstdio>
20 #include <cstdlib>
21 #include <ctime>
22 #include <string>
23 
24 #include "exprtk.hpp"
25 
26 
27 template <typename T>
29 {
33 
34  std::string sgfilter_program =
35  " var weight[9] := "
36  " { "
37  " -21, 14, 39, "
38  " 54, 59, 54, "
39  " 39, 14, -21 "
40  " }; "
41  " "
42  " if (v_in[] >= weight[]) "
43  " { "
44  " var lower_bound := trunc(weight[] / 2); "
45  " var upper_bound := v_in[] - lower_bound; "
46  " "
47  " v_out := 0; "
48  " "
49  " for (var i := lower_bound; i < upper_bound; i += 1) "
50  " { "
51  " for (var j := -lower_bound; j <= lower_bound; j += 1) "
52  " { "
53  " v_out[i] += weight[j + lower_bound] * v_in[i + j]; "
54  " }; "
55  " }; "
56  " "
57  " v_out /= sum(weight); "
58  " } ";
59 
60  const std::size_t n = 1024;
61 
62  std::vector<T> v_in;
63  std::vector<T> v_out;
64 
65  const T pi = T(3.141592653589793238462643383279502);
66 
67  srand(static_cast<unsigned int>(time(0)));
68 
69  // Generate a signal with noise.
70  for (T t = T(-5); t <= T(+5); t += T(10.0 / n))
71  {
72  T noise = T(0.5 * (rand() / (RAND_MAX + 1.0) - 0.5));
73  v_in.push_back(sin(2.0 * pi * t) + noise);
74  }
75 
76  v_out.resize(v_in.size());
77 
78  symbol_table_t symbol_table;
79  symbol_table.add_vector("v_in" , v_in);
80  symbol_table.add_vector("v_out",v_out);
81 
82  expression_t expression;
83  expression.register_symbol_table(symbol_table);
84 
86  parser.compile(sgfilter_program,expression);
87 
88  expression.value();
89 
90  for (std::size_t i = 0; i < v_out.size(); ++i)
91  {
92  printf("%10.6f\t%10.6f\n",v_in[i],v_out[i]);
93  }
94 }
95 
96 int main()
97 {
98  savitzky_golay_filter<double>();
99  return 0;
100 }