forked from ArashPartow/exprtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
C++ Mathematical Expression Library (ExprTk) http://www.partow.net/pr…
- Loading branch information
1 parent
6b86ec0
commit 1bf1073
Showing
8 changed files
with
145 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
************************************************************** | ||
* C++ Mathematical Expression Toolkit Library * | ||
* * | ||
* Simple Example 13 * | ||
* Author: Arash Partow (1999-2014) * | ||
* 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 Common Public License. * | ||
* http://www.opensource.org/licenses/cpl1.0.php * | ||
* * | ||
************************************************************** | ||
*/ | ||
|
||
|
||
#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; | ||
|
||
std::string gsfilter_program = | ||
" var weight[9] := " | ||
" { " | ||
" -21, 14, 39, " | ||
" 54, 59, 54, " | ||
" 39, 14, -21 " | ||
" }; " | ||
" " | ||
" var lower_bound := trunc(weight[] / 2); " | ||
" var upper_bound := v_in[] - lower_bound; " | ||
" " | ||
" if (v_in[] >= weight[]) " | ||
" { " | ||
" for (i := lower_bound; i < upper_bound; i += 1) " | ||
" { " | ||
" v_out[i] := 0; " | ||
" for (j := 0; j < weight[]; j += 1) " | ||
" { " | ||
" v_out[i] += weight[j] * v_in[i + j]; " | ||
" }; " | ||
" v_out[i] /= 231; " | ||
" }; " | ||
" } "; | ||
|
||
const std::size_t n = 1024; | ||
|
||
std::vector<T> v_in; | ||
std::vector<T> v_out; | ||
|
||
const T pi = T(3.141592653589793238462); | ||
|
||
srand(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(gsfilter_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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters