C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_simple_example_23.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* Simple Example 23 *
6
* Author: Arash Partow (1999-2025) *
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 <string>
22
23
#include "
exprtk.hpp
"
24
25
26
template
<
typename
T>
27
void
real_1d_discrete_fourier_transform
()
28
{
29
typedef
exprtk::symbol_table<T>
symbol_table_t;
30
typedef
exprtk::expression<T>
expression_t;
31
typedef
exprtk::parser<T>
parser_t;
32
typedef
exprtk::function_compositor<T>
compositor_t;
33
typedef
typename
compositor_t::function function_t;
34
35
const
T sampling_rate = 1024.0;
// ~1KHz
36
const
T N = 8 * sampling_rate;
// 8 seconds worth of samples
37
38
std::vector<T> input (
static_cast<
std::size_t
>
(N),0.0);
39
std::vector<T> output(
static_cast<
std::size_t
>
(N),0.0);
40
41
exprtk::rtl::io::println<T>
println;
42
43
symbol_table_t symbol_table;
44
symbol_table.add_vector (
"input"
, input );
45
symbol_table.add_vector (
"output"
, output );
46
symbol_table.add_function (
"println"
, println );
47
symbol_table.add_constant (
"N"
, N );
48
symbol_table.add_constant (
"sampling_rate"
, sampling_rate);
49
symbol_table.add_pi();
50
51
compositor_t compositor(symbol_table);
52
compositor.load_vectors(
true
);
53
54
compositor.add(
55
function_t(
"dft_1d_real"
)
56
.var(
"N"
)
57
.expression
58
(
59
" for (var k := 0; k < N; k += 1) "
60
" { "
61
" var k_real := 0.0; "
62
" var k_imag := 0.0; "
63
" "
64
" for (var i := 0; i < N; i += 1) "
65
" { "
66
" var theta := 2pi * k * i / N; "
67
" k_real += input[i] * cos(theta); "
68
" k_imag -= input[i] * sin(theta); "
69
" }; "
70
" "
71
" output[k] := hypot(k_real,k_imag); "
72
" } "
73
));
74
75
const
std::string dft_program =
76
" "
77
" /* "
78
" Generate an aggregate waveform comprised of three "
79
" sine waves of varying frequencies and amplitudes. "
80
" */ "
81
" const var num_waves := 3; "
82
" "
83
" var frequencies[num_waves] := { 111.125, 222.250, 333.375 }; /* Hz */ "
84
" var amplitudes [num_waves] := { 11.111, 22.222, 33.333 }; /* Power */ "
85
" "
86
" for (var i := 0; i < N; i += 1) "
87
" { "
88
" var time := i / sampling_rate; "
89
" "
90
" for (var j := 0; j < frequencies[]; j += 1) "
91
" { "
92
" var frequency := frequencies[j]; "
93
" var amplitude := amplitudes [j]; "
94
" var theta := 2 * pi * frequency * time; "
95
" "
96
" input[i] += amplitude * sin(theta); "
97
" } "
98
" }; "
99
" "
100
" dft_1d_real(input[]); "
101
" "
102
" var freq_bin_size := sampling_rate / N; "
103
" var max_bin := ceil(N / 2); "
104
" var max_noise_level := 1e-6; "
105
" "
106
" /* Normalise amplitudes */ "
107
" output /= max_bin; "
108
" "
109
" println('1D Real DFT Frequencies'); "
110
" "
111
" for (var k := 0; k < max_bin; k += 1) "
112
" { "
113
" if (output[k] > max_noise_level) "
114
" { "
115
" var freq_begin := k * freq_bin_size; "
116
" var freq_end := freq_begin + freq_bin_size; "
117
" "
118
" println('Index: ', k,' ', "
119
" 'Freq. range: [', freq_begin, 'Hz, ', freq_end, 'Hz) ', "
120
" 'Amplitude: ', output[k]); "
121
" } "
122
" } "
123
" "
;
124
125
expression_t expression;
126
expression.register_symbol_table(symbol_table);
127
128
parser_t parser;
129
parser.compile(dft_program,expression);
130
131
expression.value();
132
}
133
134
int
main
()
135
{
136
real_1d_discrete_fourier_transform<double>
();
137
return
0;
138
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::function_compositor
Definition
exprtk.hpp:43057
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
real_1d_discrete_fourier_transform
void real_1d_discrete_fourier_transform()
Definition
exprtk_simple_example_23.cpp:27
main
int main()
Definition
exprtk_simple_example_23.cpp:134
exprtk::rtl::io::println
Definition
exprtk.hpp:44273
Generated by
1.17.0