⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 melp_sub.c

📁 MELPe 1200 bps, fixed point
💻 C
📖 第 1 页 / 共 3 页
字号:

/* Name: gain_ana.c                                                           */
/*	Description: analyze gain level for input signal                          */
/*	Inputs:                                                                   */
/*	  sigin[] - input signal                                                  */
/*	  pitch - pitch value (for pitch synchronous window)                      */
/*	  minlength - minimum window length                                       */
/*	  maxlength - maximum window length                                       */
/*	Outputs:                                                                  */
/*	Returns: log gain in dB                                                   */
/*                                                                            */
/*  Copyright (c) 1997 by Texas Instruments, Inc.  All rights reserved.       */
/*                                                                            */
/*	Q values                                                                  */
/*	--------                                                                  */
/*	pitch - Q7                                                                */
/*	sigin (speech) - Q0                                                       */

Shortword gain_ana(Shortword sigin[], Shortword pitch, Shortword minlength,
				   Shortword maxlength)
{
	Shortword	length;                                                 /* Q0 */
	Shortword	flength;                                                /* Q6 */
	Shortword	gain;                                                   /* Q8 */
	Shortword	pitch_Q6;                                               /* Q6 */
	Shortword	scale;
	Shortword	tmp_minlength;
	Shortword	sigbegin;
	Shortword	*temp_buf, *tmp_sig;
	Shortword	S_temp1, S_temp2;
	Longword	L_temp;


	/* initialize scaled pitch and minlength */
	pitch_Q6 = shr(pitch, 1);                                       /* Q7->Q6 */

	tmp_minlength = shl(minlength, 6);

	/* Find shortest pitch multiple window length (floating point) */
	flength = pitch_Q6;
	while (flength < tmp_minlength){
		flength = add(flength, pitch_Q6);
	}

	/* Convert window length to integer and check against maximum */
	length = shr(add(flength, X05_Q6), 6);

	if (length > maxlength){
		/* divide by 2 */
		length = shr(length, 1);
	}

	/* Calculate RMS gain in dB */
	/*	gain = 10.0*log10(0.01 + */
	/*		   (v_magsq(&sigin[-(length/2)], length)/length)); */

	/* use this adaptive scaling if more precision needed at low levels.      */
	/* Seemed like it wasn't worth the mips */

	sigbegin = negate(shr(length, 1));

	/* Right shift input signal and put in temp buffer */
	temp_buf = v_get(length);

	scale = 3;
#if (!CONSTANT_SCALE)
	v_equ_shr(temp_buf, &sigin[sigbegin], scale, length);
	L_temp = L_v_magsq(temp_buf, length, 0, 1);

	if (L_temp){
		S_temp1 = norm_l(L_temp);
		scale = sub(scale, shr(S_temp1, 1));
		if (scale < 0)
			scale = 0;
	} else
		scale = 0;
#endif

	if (scale)
		v_equ_shr(temp_buf, &sigin[sigbegin], scale, length);
	else
		v_equ(temp_buf, &sigin[sigbegin], length);
	tmp_sig = temp_buf - sigbegin;

	if (scale){
		L_temp = L_v_magsq(&tmp_sig[sigbegin], length, 0, 0);
		S_temp1 = L_log10_fxp(L_temp, 0);
		S_temp2 = L_log10_fxp(L_deposit_l(length), 0);
		S_temp1 = sub(S_temp1, S_temp2);
		/* shift right to counter-act for the shift in L_mult */
		S_temp2 = extract_l(L_shr(L_mult(scale, LOG10OF2X2), 1));
		S_temp1 = add(S_temp1, S_temp2);
		S_temp1 = mult(TEN_Q11, S_temp1);
		gain = shl(S_temp1, 1);
	} else {
		L_temp = L_v_magsq(&tmp_sig[sigbegin], length, 0, 0);
		/* Add one to avoid log of a zero value */
		if (L_temp == 0)
			S_temp1 = N2_Q11;
		else
			S_temp1 = L_log10_fxp(L_temp, 0);
		S_temp2 = L_log10_fxp(L_deposit_l(length), 0);
		S_temp1 = sub(S_temp1, S_temp2);
		S_temp1 = mult(TEN_Q11, S_temp1);
		gain = shl(S_temp1, 1);
	}

	if (gain < MINGAIN){
		gain = MINGAIN;
	}

	v_free(temp_buf);

	return(gain);
}


/* Name: lin_int_bnd.c                                                        */
/*	Description: Linear interpolation within bounds                           */
/*	Inputs:                                                                   */
/*	  x - input X value                                                       */
/*	  xmin - minimum X value                                                  */
/*	  xmax - maximum X value                                                  */
/*	  ymin - minimum Y value                                                  */
/*	  ymax - maximum Y value                                                  */
/*	Returns: y - interpolated and bounded y value                             */
/*                                                                            */
/*	Copyright (c) 1995,1997 by Texas Instruments, Inc.	All rights reserved.  */
/*                                                                            */
/*	Q values:                                                                 */
/*	x,xmin,xmax - Q8                                                          */
/*	y,ymin,ymax - Q15                                                         */

Shortword lin_int_bnd(Shortword x, Shortword xmin, Shortword xmax,
					  Shortword ymin, Shortword ymax)
{
	Shortword	y, temp1, temp2;


	if (x <= xmin)
		y = ymin;
	else if (x >= xmax)
		y = ymax;
	else {
		/* y = ymin + (x-xmin)*(ymax-ymin)/(xmax-xmin); */
		temp1 = sub(x, xmin);
		temp2 = sub(ymax, ymin);
		temp1 = mult(temp1, temp2);                            /* temp1 in Q8 */
		temp2 = sub(xmax, xmin);
		/* temp1 always smaller than temp2 */
		temp1 = divide_s(temp1, temp2);                       /* temp1 in Q15 */
		y = add(ymin, temp1);
	}

	return(y);
}


/* Name: noise_est.c                                                          */
/*	Description: Estimate long-term noise floor                               */
/*	Inputs:                                                                   */
/*	  gain - input gain (in dB)                                               */
/* 	  noise_gain - current noise gain estimate                                */
/*	  up - maximum up stepsize                                                */
/*	  down - maximum down stepsize                                            */
/*	  min - minimum allowed gain                                              */
/*	  max - maximum allowed gain                                              */
/*	Outputs:                                                                  */
/*	  noise_gain - updated noise gain estimate                                */
/*	Returns: void                                                             */
/*                                                                            */
/*	Copyright (c) 1995,1997 by Texas Instruments, Inc.	All rights reserved.  */
/*                                                                            */
/*	Q values:                                                                 */
/*	gain - Q8, *noise_gain - Q8, up - Q19, down - Q17, min - Q8, max - Q8     */

void noise_est(Shortword gain, Shortword *noise_gain, Shortword up,
			   Shortword down, Shortword min, Shortword max)
{
	Shortword	temp1, temp2;
	Longword	L_noise_gain, L_temp;


	/* Update noise_gain */
	/*	temp1 = add(*noise_gain, up); */
	/*	temp2 = add(*noise_gain, down); */
	L_noise_gain = L_deposit_h(*noise_gain);           /* L_noise_gain in Q24 */
	L_temp = L_shl(L_deposit_l(up), 5);
	L_temp = L_add(L_noise_gain, L_temp);
	temp1 = round(L_temp);
	L_temp = L_shl(L_deposit_l(down), 7);
	L_temp = L_add(L_noise_gain, L_temp);
	temp2 = round(L_temp);

	if (gain > temp1)
		*noise_gain = temp1;
	else if (gain < temp2)
		*noise_gain = temp2;
	else
		*noise_gain = gain;

	/* Constrain total range of noise_gain */
	if (*noise_gain < min)
		*noise_gain = min;
	if (*noise_gain > max)
		*noise_gain = max;
}


/* Name: noise_sup.c                                                          */
/*	Description: Perform noise suppression on speech gain                     */
/*	Inputs: (all in dB)                                                       */
/*	  gain - input gain (in dB)                                               */
/*	  noise_gain - current noise gain estimate (in dB)                        */
/*	  max_noise - maximum allowed noise gain                                  */
/*	  max_atten - maximum allowed attenuation                                 */
/*	  nfact - noise floor boost                                               */
/*	Outputs:                                                                  */
/*	  gain - updated gain                                                     */
/*	Returns: void                                                             */
/*                                                                            */
/*	Copyright (c) 1995,1997 by Texas Instruments, Inc.	All rights reserved.  */
/*                                                                            */
/*	Q values:                                                                 */
/*	*gain - Q8, noise_gain - Q8, max_noise - Q8, max_atten - Q8, nfact - Q8   */

void noise_sup(Shortword *gain, Shortword noise_gain, Shortword max_noise,
			   Shortword max_atten, Shortword nfact)
{
	Shortword	gain_lev, suppress, temp;
	Longword	L_temp;


	/* Reduce effect for louder background noise */
	if (noise_gain > max_noise)
		noise_gain = max_noise;

	/* Calculate suppression factor */
	gain_lev = sub(*gain, add(noise_gain, nfact));
	if (gain_lev > X0001_Q8){
		/*	suppress = -10.0*log10(1.0 - pow(10.0,-0.1*gain_lev));            */
		L_temp = L_mult(M01_Q15, gain_lev);                  /* L_temp in Q24 */
		temp = extract_h(L_shl(L_temp, 4));                    /* temp in Q12 */
		temp = pow10_fxp(temp, 14);
		temp = sub(ONE_Q14, temp);
		suppress = mult(M10_Q11, log10_fxp(temp, 14));     /* log returns Q12 */
		if (suppress > max_atten)
			suppress = max_atten;
	} else
		suppress = max_atten;

	/* Apply suppression to input gain */
	*gain = sub(*gain, suppress);
}


/* Name: q_bpvc.c, q_bpvc_dec.c                                               */
/* 	Description: Quantize/decode bandpass voicing                             */
/*	Inputs:                                                                   */
/*	  bpvc, bpvc_index                                                        */
/*	  num_bands - number of bands                                             */
/*	Outputs:                                                                  */
/*	  bpvc, bpvc_index                                                        */
/*	Returns: uv_flag - flag if unvoiced                                       */
/*                                                                            */
/*	Copyright (c) 1995,1997 by Texas Instruments, Inc.	All rights reserved.  */
/*                                                                            */
/*	Q values:                                                                 */
/*	*bpvc - Q14                                                               */

BOOLEAN q_bpvc(Shortword bpvc[], Shortword *bpvc_index, Shortword num_bands)
{
	register Shortword	i;
	BOOLEAN		uv_flag;
	Shortword	index;


	if (bpvc[0] > BPTHRESH_Q14){

		/* Voiced: pack bandpass voicing */
		uv_flag = FALSE;
		index = 0;
		bpvc[0] = ONE_Q14;

		for (i = 1; i < num_bands; i++){
			index = shl(index, 1);                      /* left shift one bit */
			if (bpvc[i] > BPTHRESH_Q14){
				bpvc[i] = ONE_Q14;
				index |= 1;
			} else {
				bpvc[i] = 0;
				index |= 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -