Skip to content
Snippets Groups Projects
Commit 800a659c authored by Jean-Marc Valin's avatar Jean-Marc Valin
Browse files

Using log approximations

parent 677182fc
No related branches found
No related tags found
No related merge requests found
...@@ -12,6 +12,26 @@ ...@@ -12,6 +12,26 @@
float lpc_from_cepstrum(float *lpc, const float *cepstrum); float lpc_from_cepstrum(float *lpc, const float *cepstrum);
#define LOG256 5.5451774445f
static RNN_INLINE float log2_approx(float x)
{
int integer;
float frac;
union {
float f;
int i;
} in;
in.f = x;
integer = (in.i>>23)-127;
in.i -= integer<<23;
frac = in.f - 1.5f;
frac = -0.41445418f + frac*(0.95909232f
+ frac*(-0.33951290f + frac*0.16541097f));
return 1+integer+frac;
}
#define log_approx(x) (0.69315f*log2_approx(x))
static RNN_INLINE float ulaw2lin(float u) static RNN_INLINE float ulaw2lin(float u)
{ {
float s; float s;
...@@ -19,7 +39,7 @@ static RNN_INLINE float ulaw2lin(float u) ...@@ -19,7 +39,7 @@ static RNN_INLINE float ulaw2lin(float u)
u = u - 128; u = u - 128;
s = u >= 0 ? 1 : -1; s = u >= 0 ? 1 : -1;
u = fabs(u); u = fabs(u);
return s*scale_1*(exp(u/128.*log(256))-1); return s*scale_1*(exp(u/128.*LOG256)-1);
} }
static RNN_INLINE int lin2ulaw(float x) static RNN_INLINE int lin2ulaw(float x)
...@@ -28,7 +48,7 @@ static RNN_INLINE int lin2ulaw(float x) ...@@ -28,7 +48,7 @@ static RNN_INLINE int lin2ulaw(float x)
float scale = 255.f/32768.f; float scale = 255.f/32768.f;
int s = x >= 0 ? 1 : -1; int s = x >= 0 ? 1 : -1;
x = fabs(x); x = fabs(x);
u = (s*(128*log(1+scale*x)/log(256))); u = (s*(128*log_approx(1+scale*x)/LOG256));
u = 128 + u; u = 128 + u;
if (u < 0) u = 0; if (u < 0) u = 0;
if (u > 255) u = 255; if (u > 255) u = 255;
......
...@@ -219,7 +219,7 @@ void write_audio(DenoiseState *st, const short *pcm, float noise_std, FILE *file ...@@ -219,7 +219,7 @@ void write_audio(DenoiseState *st, const short *pcm, float noise_std, FILE *file
/* Excitation out. */ /* Excitation out. */
data[4*i+3] = e; data[4*i+3] = e;
/* Simulate error on excitation. */ /* Simulate error on excitation. */
noise = (int)floor(.5 + noise_std*.707*(log((float)rand()/RAND_MAX)-log((float)rand()/RAND_MAX))); noise = (int)floor(.5 + noise_std*.707*(log_approx((float)rand()/RAND_MAX)-log_approx((float)rand()/RAND_MAX)));
e += noise; e += noise;
e = IMIN(255, IMAX(0, e)); e = IMIN(255, IMAX(0, e));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment