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