-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
porting the dithering function from kaldi (#40)
* porting the dithering function from kaldi * removing the debug message * change the default value for dithering - 1.0 is too much, and would break the systems
- Loading branch information
1 parent
cd20baa
commit 2c7ecc6
Showing
6 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ dist | |
*.egg-info | ||
**/__pycache__ | ||
build-arm-linux-gnueabihf | ||
|
||
# vim | ||
*.sw? |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// kaldi-native-fbank/csrc/kaldi-math.cc | ||
// | ||
// Copyright (c) 2024 Brno University of Technology (authors: Karel Vesely) | ||
|
||
// This file is an excerpt from kaldi/src/feat/kaldi-math.cc | ||
|
||
#include "kaldi-math.h" | ||
|
||
namespace knf { | ||
|
||
int Rand(struct RandomState* state) { | ||
#if !defined(_POSIX_THREAD_SAFE_FUNCTIONS) | ||
// On Windows and Cygwin, just call Rand() | ||
return rand(); | ||
#else | ||
if (state) { | ||
return rand_r(&(state->seed)); | ||
} else { | ||
std::lock_guard<std::mutex> lock(_RandMutex); | ||
return rand(); | ||
} | ||
#endif | ||
} | ||
|
||
RandomState::RandomState() { | ||
// we initialize it as Rand() + 27437 instead of just Rand(), because on some | ||
// systems, e.g. at the very least Mac OSX Yosemite and later, it seems to be | ||
// the case that rand_r when initialized with rand() will give you the exact | ||
// same sequence of numbers that rand() will give if you keep calling rand() | ||
// after that initial call. This can cause problems with repeated sequences. | ||
// For example if you initialize two RandomState structs one after the other | ||
// without calling rand() in between, they would give you the same sequence | ||
// offset by one (if we didn't have the "+ 27437" in the code). 27437 is just | ||
// a randomly chosen prime number. | ||
seed = unsigned(Rand()) + 27437; | ||
} | ||
|
||
} // namespace knf |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// kaldi-native-fbank/csrc/kaldi-math.h | ||
// | ||
// Copyright (c) 2024 Brno University of Technology (authors: Karel Vesely) | ||
|
||
// This file is an excerpt from kaldi/src/feat/kaldi-math.h | ||
|
||
#pragma once | ||
|
||
#include <cmath> // logf, sqrtf, cosf | ||
#include <cstdlib> // RAND_MAX | ||
|
||
#ifndef M_PI | ||
#define M_PI 3.1415926535897932384626433832795 | ||
#endif | ||
|
||
#ifndef M_2PI | ||
#define M_2PI 6.283185307179586476925286766559005 | ||
#endif | ||
|
||
namespace knf { | ||
|
||
inline float Log(float x) { return logf(x); } | ||
|
||
// Returns a random integer between 0 and RAND_MAX, inclusive | ||
int Rand(struct RandomState* state = NULL); | ||
|
||
// State for thread-safe random number generator | ||
struct RandomState { | ||
RandomState(); | ||
unsigned seed; | ||
}; | ||
|
||
/// Returns a random number strictly between 0 and 1. | ||
inline float RandUniform(struct RandomState* state = NULL) { | ||
return static_cast<float>((Rand(state) + 1.0) / (RAND_MAX+2.0)); | ||
} | ||
|
||
inline float RandGauss(struct RandomState* state = NULL) { | ||
return static_cast<float>(sqrtf (-2 * Log(RandUniform(state))) | ||
* cosf(2*M_PI*RandUniform(state))); | ||
} | ||
|
||
} // namespace knf |