📄 fe_noise.cpp
字号:
///////////////////////////////////////////////////////////////////////////////
// This is a part of the Feature program.
// Version: 1.0
// Date: February 22, 2003
// Programmer: Oh-Wook Kwon
// Copyright(c) 2003 Oh-Wook Kwon. All rights reserved. owkwon@ucsd.edu
///////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "FE_feature.h"
static const long A=16807L;static const long M=2147483647L;static long g_noise_In[16] = { 0L,1973272912L, 747177549L, 20464843L,640830765L, 1098742207L, 78126602L,84743774L, 831312807L, 124667236L,1172177002L, 1124933064L, 1223960546L,1878892440L, 1449793615L, 553303732L};static int g_noise_strm = 2;static double RandomLE();
static double RandomBE();
double Fe::Random()
{
if(m_byteOrder==MY_LITTLE_ENDIAN) return RandomLE();
else return RandomBE();
}
double RandomLE(){ short *p,*q,k; long Hi,Lo; p = (short *)&g_noise_In[g_noise_strm]; Hi = *(p+1) * A; *(p+1) = 0; Lo = g_noise_In[g_noise_strm]*A; p = (short *)&Lo; Hi += *(p+1); q = (short *)&Hi; *(p+1) = *q&0x7fff; k = *(q+1)<<1; if(*q&0x8000) k++; Lo -= M; Lo += k; if(Lo<0) Lo+=M; g_noise_In[g_noise_strm] = Lo; return((double)Lo*4.656612875E-10);}
double RandomBE(){ short *p,*q,k; long Hi,Lo; p = (short *)&g_noise_In[g_noise_strm]; Hi = *(p)*A; *(p) = 0; Lo = g_noise_In[g_noise_strm]*A; p = (short *)&Lo; Hi += *(p); q = (short *)&Hi; *(p) = *(q+1)&0x7fff; k = *(q)<<1; if(*(q+1)&0x8000) k++; Lo -= M; Lo += k; if(Lo<0) Lo += M; g_noise_In[g_noise_strm] = Lo; return((double)Lo*4.656612875E-10);}
double Fe::GaussianNoise(double x, double s){ double v1,v2,w,z1; static double z2=0.0; if (z2 != 0.0) { z1=z2; z2=0.0; } else { do { v1 = 2.0*Random()-1.0; v2 = 2.0*Random()-1.0; w = v1*v1 + v2*v2; } while (w >= 1.0); w = sqrt((-2.0*log(w))/w); z1 = v1*w; z2 = v2*w; } return(x+z1*s);}
/**********************************************************************
* add additive white Gaussian noise(AWGN) into speech signal
**********************************************************************/
int Fe::AddNoise(float *sample, int num_of_samples, float *waveform, int insert_noise, float SNR)
{
int j;
float Ps, Pn;
if (insert_noise) {
Ps = 0.0;
for (j=0; j<num_of_samples; j++)
Ps += sample[j]*sample[j];
Ps /= (float) num_of_samples;
Pn = Ps / EXP10( 0.1 * SNR );
for (j=0; j<num_of_samples; j++)
waveform[j] = sample[j] + (float) GaussianNoise( 0.0, sqrt((double) Pn) );
}
return(1);
}
/* ------------------------------------------------------------------
When using ulaw data, often entire frames will contain only zeros.
This is bad for spectral calculation, so we should add a dither
signal to make sure the signal is always at least some quantization noise.
12-Oct-94
Now using rand48 routines instead for hpux compatibility
------------------------------------------------------------------ */
/* add 1-bit dither to signal */
int Fe::Dither(float *buf, int n)
{
int i;
srand(1);
for (i=0;i<n;i++) buf[i] += ((rand()%2)?1:0);
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -