C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
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-2024) *
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
26template <typename T>
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
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 " var frequencies[3] := { 100.0, 200.0, 300.0 }; /* Hz */ "
82 " var amplitudes [3] := { 10.0, 20.0, 30.0 }; /* Power */ "
83 " "
84 " for (var i := 0; i < N; i += 1) "
85 " { "
86 " var time := i / sampling_rate; "
87 " "
88 " for (var j := 0; j < frequencies[]; j += 1) "
89 " { "
90 " var frequency := frequencies[j]; "
91 " var amplitude := amplitudes [j]; "
92 " var theta := 2 * pi * frequency * time; "
93 " "
94 " input[i] += amplitude * sin(theta); "
95 " } "
96 " }; "
97 " "
98 " dft_1d_real(input[]); "
99 " "
100 " var freq_bin_size := sampling_rate / N; "
101 " var max_bin := ceil(N / 2); "
102 " var max_noise_level := 1e-5; "
103 " "
104 " /* Normalise amplitudes */ "
105 " output /= max_bin; "
106 " "
107 " println('1D Real DFT Frequencies'); "
108 " "
109 " for (var k := 0; k < max_bin; k += 1) "
110 " { "
111 " if (output[k] > max_noise_level) "
112 " { "
113 " var freq_begin := k * freq_bin_size; "
114 " var freq_end := freq_begin + freq_bin_size; "
115 " "
116 " println('Index: ', k,' ', "
117 " 'Freq. range: [', freq_begin, 'Hz, ', freq_end, 'Hz) ', "
118 " 'Amplitude: ', output[k]); "
119 " } "
120 " } "
121 " ";
122
123 expression_t expression;
124 expression.register_symbol_table(symbol_table);
125
126 parser_t parser;
127 parser.compile(dft_program,expression);
128
129 expression.value();
130}
131
132int main()
133{
134 real_1d_discrete_fourier_transform<double>();
135 return 0;
136}
void real_1d_discrete_fourier_transform()