Skip to content

Commit

Permalink
porting the dithering function from kaldi
Browse files Browse the repository at this point in the history
  • Loading branch information
KarelVesely84 committed Mar 13, 2024
1 parent cd20baa commit 926fa47
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
16 changes: 12 additions & 4 deletions kaldi-native-fbank/csrc/feature-window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
// This file is copied/modified from kaldi/src/feat/feature-window.cc

#include "kaldi-native-fbank/csrc/feature-window.h"
#include "kaldi-native-fbank/csrc/kaldi-math.h"

#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>

#ifndef M_2PI
#define M_2PI 6.283185307179586476925286766559005
#endif

namespace knf {

std::ostream &operator<<(std::ostream &os, const FrameExtractionOptions &opts) {
Expand Down Expand Up @@ -205,6 +202,14 @@ float InnerProduct(const float *a, const float *b, int32_t n) {
return sum;
}

void Dither(float *d, int32_t n, float dither_value) {
if (dither_value == 0.0)
return;
RandomState rstate;
for (int32_t i = 0; i < n; i++)
d[i] += RandGauss(&rstate) * dither_value;
}

static void Preemphasize(float *d, int32_t n, float preemph_coeff) {
if (preemph_coeff == 0.0) {
return;
Expand All @@ -223,6 +228,9 @@ void ProcessWindow(const FrameExtractionOptions &opts,
float *log_energy_pre_window /*= nullptr*/) {
int32_t frame_length = opts.WindowSize();

if (opts.dither != 0.0)
Dither(window, frame_length, opts.dither);

if (opts.remove_dc_offset) {
RemoveDcOffset(window, frame_length);
}
Expand Down
43 changes: 43 additions & 0 deletions kaldi-native-fbank/csrc/kaldi-math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// kaldi-native-fbank/csrc/feature-window.h
//
// Copyright (c) 2022 Xiaomi Corporation (authors: Fangjun Kuang)

// This file is copied/modified from kaldi/src/feat/feature-window.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

0 comments on commit 926fa47

Please sign in to comment.