// File: /workspace/github_cpp_makefile_star2_O2/ArashPartow[P]exprtk/exprtk_simple_example_23.cpp
// Function: void real_1d_discrete_fourier_transform<double>()
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();
}

// Assembly:
pushq %rbx
subq $0x1560, %rsp           # imm = 0x1560
movabsq $0x4090000000000000, %rax # imm = 0x4090000000000000
movq %rax, 0x60(%rsp)
movabsq $0x40c0000000000000, %rax # imm = 0x40C0000000000000
movq %rax, 0x58(%rsp)
leaq 0x250(%rsp), %rbx
andq $0x0, (%rbx)
leaq 0x80(%rsp), %rdi
leaq 0xc38(%rsp), %rcx
movl $0x2000, %esi           # imm = 0x2000
movq %rbx, %rdx
callq 0x1138c8
andq $0x0, (%rbx)
leaq 0x68(%rsp), %rdi
leaq 0x250(%rsp), %rdx
leaq 0xc38(%rsp), %rcx
movl $0x2000, %esi           # imm = 0x2000
callq 0x1138c8
leaq 0x131d91(%rip), %rsi    # 0x24511d
leaq 0x250(%rsp), %rdi
leaq 0xc38(%rsp), %rdx
callq 0x1132d8
leaq 0x98(%rsp), %rdi
leaq 0x250(%rsp), %rsi
callq 0x11391e
leaq 0x250(%rsp), %rdi
callq 0x1115d8
leaq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
callq 0x1139b0
leaq 0x131d4d(%rip), %rsi    # 0x245124
leaq 0x250(%rsp), %rdi
leaq 0xc38(%rsp), %rdx
callq 0x1132d8
leaq 0x8(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x80(%rsp), %rdx
callq 0x1139d4
leaq 0x250(%rsp), %rdi
callq 0x1115d8
leaq 0x131d10(%rip), %rsi    # 0x24512a
leaq 0x250(%rsp), %rdi
leaq 0xc38(%rsp), %rdx
callq 0x1132d8
leaq 0x8(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x1139d4
leaq 0x250(%rsp), %rdi
callq 0x1115d8
leaq 0x131cd7(%rip), %rsi    # 0x245131
leaq 0x250(%rsp), %rdi
leaq 0xc38(%rsp), %rdx
callq 0x1132d8
leaq 0x8(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x98(%rsp), %rdx
callq 0x113a3e
leaq 0x250(%rsp), %rdi
callq 0x1115d8
leaq 0x13343f(%rip), %rsi    # 0x2468dc
leaq 0x250(%rsp), %rdi
leaq 0xc38(%rsp), %rdx
callq 0x1132d8
leaq 0x8(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x113b06
leaq 0x250(%rsp), %rdi
callq 0x1115d8
leaq 0x131c5c(%rip), %rsi    # 0x245139
leaq 0x250(%rsp), %rdi
leaq 0xc38(%rsp), %rdx
callq 0x1132d8
leaq 0x8(%rsp), %rdi
leaq 0x250(%rsp), %rsi
leaq 0x60(%rsp), %rdx
callq 0x113b06
leaq 0x250(%rsp), %rdi
callq 0x1115d8
leaq 0x8(%rsp), %rdi
callq 0x113b82
leaq 0x250(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x113c1c
movb $0x1, 0xc31(%rsp)
leaq 0x131c06(%rip), %rsi    # 0x245147
leaq 0x100(%rsp), %rdi
leaq 0x17(%rsp), %rdx
callq 0x1132d8
leaq 0xc38(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x113d3a
leaq 0x13336d(%rip), %rsi    # 0x2468dc
leaq 0x38(%rsp), %rdi
leaq 0x16(%rsp), %rdx
callq 0x1132d8
leaq 0xc78(%rsp), %rdi
leaq 0x38(%rsp), %rsi
callq 0x11e984
leaq 0x131bbc(%rip), %rsi    # 0x245153
leaq 0x18(%rsp), %rdi
leaq 0x15(%rsp), %rdx
callq 0x1132d8
leaq 0xc58(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x111550
leaq 0x250(%rsp), %rdi
leaq 0xc38(%rsp), %rsi
xorl %edx, %edx
callq 0x113d2a
leaq 0x18(%rsp), %rdi
callq 0x1115d8
leaq 0x38(%rsp), %rdi
callq 0x1115d8
leaq 0xc38(%rsp), %rdi
callq 0x113d86
leaq 0x100(%rsp), %rdi
callq 0x1115d8
leaq 0x131d80(%rip), %rsi    # 0x245384
leaq 0x38(%rsp), %rdi
leaq 0xc38(%rsp), %rdx
callq 0x1132d8
leaq 0x18(%rsp), %rdi
callq 0x113da6
leaq 0x18(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x113e06
leaq 0x100(%rsp), %rdi
pushq $0x7f
popq %rsi
callq 0x113e62
leaq 0xc38(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x113f72
leaq 0x100(%rsp), %rdi
callq 0x11458a
leaq 0xc38(%rsp), %rdi
leaq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %rdx
callq 0x1145d2
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rdi
movq (%rdi), %rax
callq *0x18(%rax)
leaq 0xc38(%rsp), %rdi
callq 0x114a54
leaq 0x18(%rsp), %rdi
callq 0x114b8e
leaq 0x38(%rsp), %rdi
callq 0x1115d8
leaq 0x250(%rsp), %rdi
callq 0x114ba4
leaq 0x8(%rsp), %rdi
callq 0x114bf8
leaq 0x98(%rsp), %rdi
callq 0x114c3a
leaq 0x68(%rsp), %rdi
callq 0x114c7c
leaq 0x80(%rsp), %rdi
callq 0x114c7c
addq $0x1560, %rsp           # imm = 0x1560
popq %rbx
retq
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x11458a
jmp 0x1137a2
jmp 0x113703
movq %rax, %rbx
jmp 0x1137a2
movq %rax, %rbx
jmp 0x1137ac
jmp 0x11372f
jmp 0x113717
movq %rax, %rbx
jmp 0x1137c0
movq %rax, %rbx
jmp 0x1137ca
movq %rax, %rbx
jmp 0x1137d7
movq %rax, %rbx
jmp 0x1137e4
jmp 0x113760
jmp 0x113760
jmp 0x11374b
jmp 0x113760
jmp 0x11374b
jmp 0x113760
jmp 0x11374b
jmp 0x113760
jmp 0x11374b
jmp 0x113760
movq %rax, %rbx
leaq 0x250(%rsp), %rdi
callq 0x1115d8
jmp 0x1137f1
movq %rax, %rbx
jmp 0x1137f1
movq %rax, %rbx
jmp 0x1137fb
movq %rax, %rbx
leaq 0x250(%rsp), %rdi
callq 0x1115d8
jmp 0x113808
movq %rax, %rbx
jmp 0x113808
movq %rax, %rbx
jmp 0x113812
movq %rax, %rbx
leaq 0xc38(%rsp), %rdi
callq 0x114a54
leaq 0x18(%rsp), %rdi
callq 0x114b8e
leaq 0x38(%rsp), %rdi
jmp 0x1137df
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x1115d8
leaq 0x38(%rsp), %rdi
callq 0x1115d8
leaq 0xc38(%rsp), %rdi
callq 0x113d86
leaq 0x100(%rsp), %rdi
callq 0x1115d8
leaq 0x250(%rsp), %rdi
callq 0x114ba4
leaq 0x8(%rsp), %rdi
callq 0x114bf8
leaq 0x98(%rsp), %rdi
callq 0x114c3a
leaq 0x68(%rsp), %rdi
callq 0x114c7c
leaq 0x80(%rsp), %rdi
callq 0x114c7c
movq %rbx, %rdi
callq 0x111310