Created
September 20, 2018 13:00
-
-
Save florianbehrens/8db4cbcf8c13be6453ae8d775d91c2a9 to your computer and use it in GitHub Desktop.
Compile time (constexpr) sine table generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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