cfdlp.c
// on the fly, energy based VAD computation
on the fly, energy based VAD computation
on the fly energy based VAD computation
int *check_VAD(short *x, int N, int Fs, int *Nindices)
{
    int NB_FRAME_THRESHOLD_LTE = 10;
    float LAMBDA_LTE = 0.97;
    int SNR_THRESHOLD_UPD_LTE = 20;
    int ENERGY_FLOOR = 80;
    int MIN_FRAME = 10;
    float lambdaLTEhigherE = 0.99;
    int SNR_THRESHOLD_VAD = 15;
    int MIN_SPEECH_FRAME_HANGOVER = 4;
    int HANGOVER = 15;
    int nbSpeechFrame = 0;
    float meanEN = 0.;
    int hangOver = 0;
    int flen = DEFAULT_SHORTTERM_WINLEN_MS * Fs;
    float *w = hann(flen);
    float SP = DEFAULT_SHORTTERM_SHIFT_PERCENTAGE;
    int Ncopy = (Fs == 16000 ? N / 2 : N);
    float *copy = MALLOC(sizeof(float) * Ncopy);
    if (Fs == 8000)
    {
	for (int i = 0; i < Ncopy; i++) {
	    copy[i] = x[i];
	}
    }
    else
    {
	for (int i = 0; i < Ncopy; i++) {
	    copy[i] = x[i*2];
	}
    }
    int Nframes = 0;
    int overlap = flen - (int)round((float)flen * SP);
    float *x_fr = fconstruct_frames(&copy, &Ncopy, flen, overlap, &Nframes);
    for (int f = 0; f < Nframes; f++)
    {
	for (int n = 0; n < flen; n++)
	{
	    x_fr[f * flen + n] *= w[n];
	}
    }
    int *indices = MALLOC(sizeof(int) * Nframes);
    *Nindices = 0;
    for (int t = 0; t < Nframes; t++)
    {
	float *x_cur = x_fr + t * flen;
	float lambdaLTE = LAMBDA_LTE;
	if (t < NB_FRAME_THRESHOLD_LTE - 1)
	{
	    lambdaLTE = 1. - (1. / (float)(t+1));
	}
	double sum = 0.;
	for (int i = flen - 80; i < flen; i++)
	{
	    sum += x_cur[i] * x_cur[i];
	}
	double frameEN = 0.5 + 16 / (log(2.)) * (log((64. + sum) / 64.));
	if ((frameEN - meanEN) < SNR_THRESHOLD_UPD_LTE || t < MIN_FRAME - 1)
	{
	    if (frameEN < meanEN || t < MIN_FRAME - 1)
	    {
		meanEN = meanEN + (1 - lambdaLTE) * (frameEN - meanEN);
	    }
	    else
	    {
		meanEN = meanEN + (1 - lambdaLTEhigherE) * (frameEN - meanEN);
	    }
	    if (meanEN < ENERGY_FLOOR)
	    {
		meanEN = ENERGY_FLOOR;
	    }
	}
	if (t > 3)
	{
	    if (frameEN - meanEN > SNR_THRESHOLD_VAD)
	    {
		nbSpeechFrame++;
	    }
	    else
	    {
		if (nbSpeechFrame > MIN_SPEECH_FRAME_HANGOVER)
		{
		    hangOver = HANGOVER;
		}
		nbSpeechFrame = 0;
		if (hangOver != 0)
		{
		    hangOver--;
		}
		else
		{
		    indices[(*Nindices)++] = t;
		}
	    }
	}
	else
	{
	    indices[(*Nindices)++] = t;
	}
    }
    FREE(w);
    FREE(x_fr);
    FREE(copy);
    return indices;
}
int * check_VAD ( short * x , int N , int Fs , int * Nindices ) { int NB_FRAME_THRESHOLD_LTE = 10 ; float LAMBDA_LTE = 0.97 ; int SNR_THRESHOLD_UPD_LTE = 20 ; int ENERGY_FLOOR = 80 ; int MIN_FRAME = 10 ; float lambdaLTEhigherE = 0.99 ; int SNR_THRESHOLD_VAD = 15 ; int MIN_SPEECH_FRAME_HANGOVER = 4 ; int HANGOVER = 15 ; int nbSpeechFrame = 0 ; float meanEN = 0. ; int hangOver = 0 ; int flen = DEFAULT_SHORTTERM_WINLEN_MS * Fs ; float * w = hann ( flen ) ; float SP = DEFAULT_SHORTTERM_SHIFT_PERCENTAGE ; int Ncopy = ( Fs == 16000 ? N / 2 : N ) ; float * copy = MALLOC ( sizeof ( float ) * Ncopy ) ; if ( Fs == 8000 ) { for ( int i = 0 ; i < Ncopy ; i ++ ) { copy [ i ] = x [ i ] ; } } else { for ( int i = 0 ; i < Ncopy ; i ++ ) { copy [ i ] = x [ i * 2 ] ; } } int Nframes = 0 ; int overlap = flen - ( int ) round ( ( float ) flen * SP ) ; float * x_fr = fconstruct_frames ( & copy , & Ncopy , flen , overlap , & Nframes ) ; for ( int f = 0 ; f < Nframes ; f ++ ) { for ( int n = 0 ; n < flen ; n ++ ) { x_fr [ f * flen + n ] *= w [ n ] ; } } int * indices = MALLOC ( sizeof ( int ) * Nframes ) ; * Nindices = 0 ; for ( int t = 0 ; t < Nframes ; t ++ ) { float * x_cur = x_fr + t * flen ; float lambdaLTE = LAMBDA_LTE ; if ( t < NB_FRAME_THRESHOLD_LTE - 1 ) { lambdaLTE = 1. - ( 1. / ( float ) ( t + 1 ) ) ; } double sum = 0. ; for ( int i = flen - 80 ; i < flen ; i ++ ) { sum += x_cur [ i ] * x_cur [ i ] ; } double frameEN = 0.5 + 16 / ( log ( 2. ) ) * ( log ( ( 64. + sum ) / 64. ) ) ; if ( ( frameEN - meanEN ) < SNR_THRESHOLD_UPD_LTE || t < MIN_FRAME - 1 ) { if ( frameEN < meanEN || t < MIN_FRAME - 1 ) { meanEN = meanEN + ( 1 - lambdaLTE ) * ( frameEN - meanEN ) ; } else { meanEN = meanEN + ( 1 - lambdaLTEhigherE ) * ( frameEN - meanEN ) ; } if ( meanEN < ENERGY_FLOOR ) { meanEN = ENERGY_FLOOR ; } } if ( t > 3 ) { if ( frameEN - meanEN > SNR_THRESHOLD_VAD ) { nbSpeechFrame ++ ; } else { if ( nbSpeechFrame > MIN_SPEECH_FRAME_HANGOVER ) { hangOver = HANGOVER ; } nbSpeechFrame = 0 ; if ( hangOver != 0 ) { hangOver -- ; } else { indices [ ( * Nindices ) ++ ] = t ; } } } else { indices [ ( * Nindices ) ++ ] = t ; } } FREE ( w ) ; FREE ( x_fr ) ; FREE ( copy ) ; return indices ; }
on the fly, energy based VAD computation
on the fly energy based VAD computation
// initialization // frame signal inte 25ms frames with 10ms shift // resample...