kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
Loading...
Searching...
No Matches
kaldi-math.h
Go to the documentation of this file.
1// kaldi-native-fbank/csrc/kaldi-math.h
2//
3// Copyright 2009-2011 Ondrej Glembek; Microsoft Corporation; Yanmin Qian;
4// Jan Silovsky; Saarland University
5// Copyright (c) 2024 Brno University of Technology (authors: Karel Vesely)
6//
7// This file is an excerpt from kaldi/src/base/kaldi-math.h
8
15
16#ifndef KALDI_NATIVE_FBANK_CSRC_KALDI_MATH_H_
17#define KALDI_NATIVE_FBANK_CSRC_KALDI_MATH_H_
18
19#include <cmath> // logf, sqrtf, cosf
20#include <cstdint>
21#include <cstdlib> // RAND_MAX
22
23#ifndef M_PI
24#define M_PI 3.1415926535897932384626433832795
25#endif
26
27#ifndef M_2PI
28#define M_2PI 6.283185307179586476925286766559005
29#endif
30
31#ifndef M_SQRT2
32#define M_SQRT2 1.4142135623730950488016887
33#endif
34
35namespace knf {
36
38inline float Log(float x) { return logf(x); }
39
41inline double Log(double x) { return log(x); }
42
44inline float Exp(float x) { return expf(x); }
45
47inline double Exp(double x) { return exp(x); }
48
57template <typename I>
58inline I Gcd(I m, I n) {
59 while (n != 0) {
60 I tmp = m % n;
61 m = n;
62 n = tmp;
63 }
64 return m;
65}
66
74template <typename I>
75inline I Lcm(I m, I n) {
76 I tmp = Gcd(m, n);
77 return tmp == 0 ? 0 : (m / tmp * n);
78}
79
90template <typename Real>
91inline bool ApproxEqual(Real a, Real b, Real tol = 0.01) {
92 return (std::abs(a - b) <= tol * (std::abs(a) + std::abs(b)));
93}
94
96int Rand(struct RandomState *state = nullptr);
97
102
103 unsigned seed;
104};
105
109inline float RandUniform(struct RandomState *state = nullptr) {
110 return static_cast<float>((Rand(state) + 1.0) / (RAND_MAX + 2.0));
111}
112
120inline float RandGauss(struct RandomState *state = nullptr) {
121 return static_cast<float>(sqrtf(-2 * Log(RandUniform(state))) *
122 cosf(2 * M_PI * RandUniform(state)));
123}
124
129void Sqrt(float *in_out, int32_t n);
130
131} // namespace knf
132#endif // KALDI_NATIVE_FBANK_CSRC_KALDI_MATH_H_
#define M_PI
Definition kaldi-math.h:24
This file is part of kaldi-native-fbank.
Definition feature-cmvn.h:42
void Sqrt(float *in_out, int32_t n)
Compute the square root of each element of an array in place.
float Exp(float x)
Exponential of x (float overload; matches kaldi's Exp).
Definition kaldi-math.h:44
int Rand(struct RandomState *state=nullptr)
Return a random integer between 0 and RAND_MAX, inclusive.
float RandUniform(struct RandomState *state=nullptr)
Returns a random number strictly between 0 and 1.
Definition kaldi-math.h:109
bool ApproxEqual(Real a, Real b, Real tol=0.01)
Approximate floating-point equality test.
Definition kaldi-math.h:91
I Gcd(I m, I n)
Greatest common divisor of two integers.
Definition kaldi-math.h:58
float RandGauss(struct RandomState *state=nullptr)
Return a random sample from the standard normal distribution.
Definition kaldi-math.h:120
float Log(float x)
Natural logarithm of x (float overload; matches kaldi's Log).
Definition kaldi-math.h:38
I Lcm(I m, I n)
Least common multiple of two integers.
Definition kaldi-math.h:75
State for the thread-safe random number generator.
Definition kaldi-math.h:99
RandomState()
Construct; initializes seed from the current time.
unsigned seed
Seed of the random number generator.
Definition kaldi-math.h:103