kaldi-math.h (1342B)
1 // kaldi-native-fbank/csrc/kaldi-math.h 2 // 3 // Copyright (c) 2024 Brno University of Technology (authors: Karel Vesely) 4 5 // This file is an excerpt from kaldi/src/base/kaldi-math.h 6 7 #ifndef KALDI_NATIVE_FBANK_CSRC_KALDI_MATH_H_ 8 #define KALDI_NATIVE_FBANK_CSRC_KALDI_MATH_H_ 9 10 #include <cmath> // logf, sqrtf, cosf 11 #include <cstdint> 12 #include <cstdlib> // RAND_MAX 13 14 #ifndef M_PI 15 #define M_PI 3.1415926535897932384626433832795 16 #endif 17 18 #ifndef M_2PI 19 #define M_2PI 6.283185307179586476925286766559005 20 #endif 21 22 #ifndef M_SQRT2 23 #define M_SQRT2 1.4142135623730950488016887 24 #endif 25 26 namespace knf { 27 28 inline float Log(float x) { return logf(x); } 29 30 // Returns a random integer between 0 and RAND_MAX, inclusive 31 int Rand(struct RandomState *state = NULL); 32 33 // State for thread-safe random number generator 34 struct RandomState { 35 RandomState(); 36 unsigned seed; 37 }; 38 39 /// Returns a random number strictly between 0 and 1. 40 inline float RandUniform(struct RandomState *state = NULL) { 41 return static_cast<float>((Rand(state) + 1.0) / (RAND_MAX + 2.0)); 42 } 43 44 inline float RandGauss(struct RandomState *state = NULL) { 45 return static_cast<float>(sqrtf(-2 * Log(RandUniform(state))) * 46 cosf(2 * M_PI * RandUniform(state))); 47 } 48 49 void Sqrt(float *in_out, int32_t n); 50 51 } // namespace knf 52 #endif // KALDI_NATIVE_FBANK_CSRC_KALDI_MATH_H_