FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
exprtk/exprtk_simple_example_23.cpp at master · ArashPartow/exprtk · GitHub
ArashPartow
/
exprtk
Public
Notifications
You must be signed in to change notification settings
Fork
322
Star
1k
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
exprtk
/
exprtk_simple_example_23.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
138 lines (122 loc) · 7.05 KB
Breadcrumbs
exprtk
/
exprtk_simple_example_23.cpp
Copy path
File metadata and controls
138 lines (122 loc) · 7.05 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
**************************************************************
* C++ Mathematical Expression Toolkit Library *
* *
* Simple Example 23 *
* Author: Arash Partow (1999-2025) *
* URL: https://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. *
* https://www.opensource.org/licenses/MIT *
* SPDX-License-Identifier: MIT *
* *
**************************************************************
*/
#
include
<
cstdio
>
#
include
<
string
>
#
include
"
exprtk.hpp
"
template
<
typename
T>
void
real_1d_discrete_fourier_transform
()
{
typedef
exprtk::symbol_table<T>
symbol_table_t
;
typedef
exprtk::expression<T>
expression_t
;
typedef
exprtk::parser<T>
parser_t
;
typedef
exprtk::function_compositor<T>
compositor_t
;
typedef
typename
compositor_t
::function
function_t
;
const
T sampling_rate =
1024.0
;
//
~1KHz
const
T N =
8
* sampling_rate;
//
8 seconds worth of samples
std::vector<T>
input
(
static_cast
<std::
size_t
>(N),
0.0
);
std::vector<T>
output
(
static_cast
<std::
size_t
>(N),
0.0
);
exprtk::rtl::io::println<T> println;
symbol_table_t
symbol_table;
symbol_table.
add_vector
(
"
input
"
, input );
symbol_table.
add_vector
(
"
output
"
, output );
symbol_table.
add_function
(
"
println
"
, println );
symbol_table.
add_constant
(
"
N
"
, N );
symbol_table.
add_constant
(
"
sampling_rate
"
, sampling_rate);
symbol_table.
add_pi
();
compositor_t
compositor
(symbol_table);
compositor.
load_vectors
(
true
);
compositor.
add
(
function_t
(
"
dft_1d_real
"
)
.
var
(
"
N
"
)
.
expression
(
"
for (var k := 0; k < N; k += 1)
"
"
{
"
"
var k_real := 0.0;
"
"
var k_imag := 0.0;
"
"
"
"
for (var i := 0; i < N; i += 1)
"
"
{
"
"
var theta := 2pi * k * i / N;
"
"
k_real += input[i] * cos(theta);
"
"
k_imag -= input[i] * sin(theta);
"
"
};
"
"
"
"
output[k] := hypot(k_real,k_imag);
"
"
}
"
));
const
std::string dft_program =
"
"
"
/*
"
"
Generate an aggregate waveform comprised of three
"
"
sine waves of varying frequencies and amplitudes.
"
"
*/
"
"
const var num_waves := 3;
"
"
"
"
var frequencies[num_waves] := { 111.125, 222.250, 333.375 }; /* Hz */
"
"
var amplitudes [num_waves] := { 11.111, 22.222, 33.333 }; /* Power */
"
"
"
"
for (var i := 0; i < N; i += 1)
"
"
{
"
"
var time := i / sampling_rate;
"
"
"
"
for (var j := 0; j < frequencies[]; j += 1)
"
"
{
"
"
var frequency := frequencies[j];
"
"
var amplitude := amplitudes [j];
"
"
var theta := 2 * pi * frequency * time;
"
"
"
"
input[i] += amplitude * sin(theta);
"
"
}
"
"
};
"
"
"
"
dft_1d_real(input[]);
"
"
"
"
var freq_bin_size := sampling_rate / N;
"
"
var max_bin := ceil(N / 2);
"
"
var max_noise_level := 1e-6;
"
"
"
"
/* Normalise amplitudes */
"
"
output /= max_bin;
"
"
"
"
println('1D Real DFT Frequencies');
"
"
"
"
for (var k := 0; k < max_bin; k += 1)
"
"
{
"
"
if (output[k] > max_noise_level)
"
"
{
"
"
var freq_begin := k * freq_bin_size;
"
"
var freq_end := freq_begin + freq_bin_size;
"
"
"
"
println('Index: ', k,' ',
"
"
'Freq. range: [', freq_begin, 'Hz, ', freq_end, 'Hz) ',
"
"
'Amplitude: ', output[k]);
"
"
}
"
"
}
"
"
"
;
expression_t
expression;
expression.
register_symbol_table
(symbol_table);
parser_t
parser;
parser.
compile
(dft_program,expression);
expression.
value
();
}
int
main
()
{
real_1d_discrete_fourier_transform<
double
>();
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL