Skip to content

Instantly share code, notes, and snippets.

@florianbehrens
Created September 20, 2018 13:00
Show Gist options
  • Save florianbehrens/8db4cbcf8c13be6453ae8d775d91c2a9 to your computer and use it in GitHub Desktop.
Save florianbehrens/8db4cbcf8c13be6453ae8d775d91c2a9 to your computer and use it in GitHub Desktop.
Compile time (constexpr) sine table generator
#include <array>
#include <cmath>
/**
* @brief Compile-time sine table creation function.
*
* @tparam SZ Table size.
* @param fs Sample frequency.
* @param f Sine frequency.
*
* @note Not portable, works only with GCC!
*/
template<size_t SZ>
constexpr std::array<double, SZ> compute_sine_table(double fs, double f)
{
std::array<double, SZ> table{};
const auto pi = std::acos(-1);
for (size_t n = 0; n < SZ; ++n) {
table[n] = std::sin(2 * pi * f / fs * n);
}
return table;
}
constexpr auto sine_table = compute_sine_table<100000>(10.0, 1.0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment