mirror of
https://github.com/Xaymar/obs-StreamFX
synced 2024-11-24 12:25:11 +00:00
util-math: Add templated gaussian function
This commit is contained in:
parent
48b76d2ba6
commit
10f7047042
1 changed files with 31 additions and 5 deletions
|
@ -135,9 +135,14 @@ namespace util {
|
|||
{ \
|
||||
return is_power_of_two_loop(v); \
|
||||
}
|
||||
is_power_of_two_as_loop(int8_t) is_power_of_two_as_loop(uint8_t) is_power_of_two_as_loop(int16_t)
|
||||
is_power_of_two_as_loop(uint16_t) is_power_of_two_as_loop(int32_t) is_power_of_two_as_loop(uint32_t)
|
||||
is_power_of_two_as_loop(int64_t) is_power_of_two_as_loop(uint64_t)
|
||||
is_power_of_two_as_loop(int8_t);
|
||||
is_power_of_two_as_loop(uint8_t);
|
||||
is_power_of_two_as_loop(int16_t);
|
||||
is_power_of_two_as_loop(uint16_t);
|
||||
is_power_of_two_as_loop(int32_t);
|
||||
is_power_of_two_as_loop(uint32_t);
|
||||
is_power_of_two_as_loop(int64_t);
|
||||
is_power_of_two_as_loop(uint64_t);
|
||||
#undef is_power_of_two_as_loop
|
||||
#pragma pop_macro("is_power_of_two_as_loop")
|
||||
|
||||
|
@ -152,5 +157,26 @@ namespace util {
|
|||
{
|
||||
return uint64_t(ceil(log10(T(v)) / log10(2.0)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline T gaussian(T x, T o /*, T u = 0*/)
|
||||
{
|
||||
// u/µ can be simulated by subtracting that value from x.
|
||||
static const double_t pi = 3.1415926535897932384626433832795;
|
||||
static const double_t two_pi = pi * 2.;
|
||||
static const double_t two_pi_sqroot = 2.506628274631000502415765284811; //sqrt(two_pi);
|
||||
|
||||
if (o == 0) {
|
||||
return T(std::numeric_limits<double_t>::infinity());
|
||||
}
|
||||
|
||||
// g(x) = (1 / o√(2Π)) * e(-(1/2) * ((x-u)/o)²)
|
||||
double_t left_e = 1. / (o * two_pi_sqroot);
|
||||
double_t mid_right_e = ((x /* - u*/) / o);
|
||||
double_t right_e = -0.5 * mid_right_e * mid_right_e;
|
||||
double_t final = left_e * exp(right_e);
|
||||
|
||||
return T(final);
|
||||
}
|
||||
} // namespace math
|
||||
} // namespace util
|
||||
|
|
Loading…
Reference in a new issue