📄 enc_gain.c
字号:
for (i = 4; i > 0; i--)
{
old_ol_lag[i] = old_ol_lag[i-1];
}
old_ol_lag[0] = prev_ol_lag;
for (i = 0; i < 5; i++)
{
tmp[i+1] = old_ol_lag[i];
}
E_GAIN_sort(5, tmp);
return tmp[3];
}
/*
* E_GAIN_norm_corr
*
* Parameters:
* exc I: excitation buffer
* xn I: target signal
* h I: weighted synthesis filter impulse response (Q15)
* t0_min I: minimum value in the searched range
* t0_max I: maximum value in the searched range
* corr_norm O: normalized correlation (Q15)
*
* Function:
* Find the normalized correlation between the target vector and the
* filtered past excitation (correlation between target and filtered
* excitation divided by the square root of energy of filtered excitation)
* Size of subframe = L_SUBFR.
*
* Returns:
* void
*/
static void E_GAIN_norm_corr(Float32 exc[], Float32 xn[], Float32 h[],
Word32 t_min, Word32 t_max, Float32 corr_norm[])
{
Float32 excf[L_SUBFR]; /* filtered past excitation */
Float32 alp, ps, norm;
Word32 t, j, k;
k = - t_min;
/* compute the filtered excitation for the first delay t_min */
E_UTIL_f_convolve(&exc[k], h, excf);
/* loop for every possible period */
for (t = t_min; t <= t_max; t++)
{
/* Compute correlation between xn[] and excf[] */
ps = 0.0F;
alp = 0.01F;
for (j = 0; j < L_SUBFR; j++)
{
ps += xn[j] * excf[j];
alp += excf[j] * excf[j];
}
/* Compute 1/sqrt(energie of excf[]) */
norm = (Float32)(1.0F / sqrt(alp));
/* Normalize correlation = correlation * (1/sqrt(energy)) */
corr_norm[t] = ps * norm;
/* update the filtered excitation excf[] for the next iteration */
if (t != t_max)
{
k--;
for (j = L_SUBFR - 1; j > 0; j--)
{
excf[j] = excf[j - 1] + exc[k] * h[j];
}
excf[0] = exc[k];
}
}
return;
}
/*
* E_GAIN_norm_corr_interpolate
*
* Parameters:
* x I: input vector
* frac I: fraction (-4..+3)
*
* Function:
* Interpolating the normalized correlation
*
* Returns:
* interpolated value
*/
static Float32 E_GAIN_norm_corr_interpolate(Float32 *x, Word32 frac)
{
Float32 s, *x1, *x2;
const Float32 *c1, *c2;
if (frac < 0)
{
frac += 4;
x--;
}
x1 = &x[0];
x2 = &x[1];
c1 = &E_ROM_inter4_1[frac];
c2 = &E_ROM_inter4_1[4 - frac];
s = x1[0] * c1[0] + x2[0] * c2[0];
s += x1[-1] * c1[4] + x2[1] * c2[4];
s += x1[-2] * c1[8] + x2[2] * c2[8];
s += x1[-3] * c1[12] + x2[3] * c2[12];
return s;
}
/*
* E_GAIN_closed_loop_search
*
* Parameters:
* exc I: excitation buffer
* xn I: target signal
* h I: weighted synthesis filter impulse response
* t0_min I: minimum value in the searched range
* t0_max I: maximum value in the searched range
* pit_frac O: chosen fraction
* i_subfr I: flag to first subframe
* t0_fr2 I: minimum value for resolution 1/2
* t0_fr1 I: minimum value for resolution 1
*
* Function:
* Find the closed loop pitch period with 1/4 subsample resolution.
*
* Returns:
* chosen integer pitch lag
*/
Word32 E_GAIN_closed_loop_search(Float32 exc[], Float32 xn[], Float32 h[],
Word32 t0_min, Word32 t0_max, Word32 *pit_frac,
Word32 i_subfr, Word32 t0_fr2, Word32 t0_fr1)
{
Float32 corr_v[15 + 2 * L_INTERPOL1 + 1];
Float32 cor_max, max, temp;
Float32 *corr;
Word32 i, fraction, step;
Word32 t0, t_min, t_max;
/* Find interval to compute normalized correlation */
t_min = t0_min - L_INTERPOL1;
t_max = t0_max + L_INTERPOL1;
/* allocate memory to normalized correlation vector */
corr = &corr_v[-t_min]; /* corr[t_min..t_max] */
/* Compute normalized correlation between target and filtered excitation */
E_GAIN_norm_corr(exc, xn, h, t_min, t_max, corr);
/* find integer pitch */
max = corr[t0_min];
t0 = t0_min;
for(i = t0_min + 1; i <= t0_max; i++)
{
if( corr[i] > max)
{
max = corr[i];
t0 = i;
}
}
/* If first subframe and t0 >= t0_fr1, do not search fractionnal pitch */
if((i_subfr == 0) & (t0 >= t0_fr1))
{
*pit_frac = 0;
return(t0);
}
/*
* Search fractionnal pitch with 1/4 subsample resolution.
* Test the fractions around t0 and choose the one which maximizes
* the interpolated normalized correlation.
*/
step = 1; /* 1/4 subsample resolution */
fraction = -3;
if (((i_subfr == 0) & (t0 >= t0_fr2)) | (t0_fr2 == PIT_MIN))
{
step = 2; /* 1/2 subsample resolution */
fraction = -2;
}
if (t0 == t0_min)
{
fraction = 0;
}
cor_max = E_GAIN_norm_corr_interpolate(&corr[t0], fraction);
for (i = (fraction + step); i <= 3; i += step)
{
;
temp = E_GAIN_norm_corr_interpolate(&corr[t0], i);
if (temp > cor_max)
{
cor_max = temp;
fraction = i;
}
}
/* limit the fraction value in the interval [0,1,2,3] */
if (fraction < 0)
{
fraction += 4;
t0 -= 1;
}
*pit_frac = fraction;
return (t0);
}
/*
* E_GAIN_adaptive_codebook_excitation
*
* Parameters:
* exc I/O: excitation buffer
* T0 I: integer pitch lag
* frac I: fraction of lag
* L_subfr I: subframe size
*
* Function:
* Compute the result of Word32 term prediction with fractional
* interpolation of resolution 1/4.
*
* Returns:
* interpolated signal (adaptive codebook excitation)
*/
void E_GAIN_adaptive_codebook_excitation(Word16 exc[], Word16 T0, Word32 frac, Word16 L_subfr)
{
Word32 i, j, k, L_sum;
Word16 *x;
x = &exc[-T0];
frac = -(frac);
if (frac < 0)
{
frac = (frac + UP_SAMP);
x--;
}
x = x - L_INTERPOL2 + 1;
for (j = 0; j < L_subfr; j++)
{
L_sum = 0L;
for (i = 0, k = ((UP_SAMP - 1) - frac); i < 2 * L_INTERPOL2; i++, k += UP_SAMP)
{
L_sum = L_sum + (x[i] * E_ROM_inter4_2[k]);
}
L_sum = (L_sum + 0x2000) >> 14;
exc[j] = E_UTIL_saturate(L_sum);
x++;
}
return;
}
/*
* E_GAIN_pitch_sharpening
*
* Parameters:
* x I/O: impulse response (or algebraic code)
* pit_lag I: pitch lag
*
* Function:
* Performs Pitch sharpening routine for one subframe.
* pitch sharpening factor is 0.85
*
* Returns:
* void
*/
void E_GAIN_pitch_sharpening(Word16 *x, Word16 pit_lag)
{
Word32 L_tmp, i;
for (i = pit_lag; i < L_SUBFR; i++)
{
L_tmp = x[i] << 15;
L_tmp += x[i - pit_lag] * PIT_SHARP;
x[i] = (Word16)((L_tmp + 0x4000) >> 15);
}
return;
}
void E_GAIN_f_pitch_sharpening(Float32 *x, Word32 pit_lag)
{
Word32 i;
for (i = pit_lag; i < L_SUBFR; i++)
{
x[i] += x[i - pit_lag] * F_PIT_SHARP;
}
return;
}
/*
* E_GAIN_voice_factor
*
* Parameters:
* exc I: pitch excitation (Q_exc)
* Q_exc I: exc format
* gain_pit I: gain of pitch (Q14)
* code I: Fixed codebook excitation (Q9)
* gain_code I: gain of code (Q0)
*
*
* Function:
* Find the voicing factor (1=voice to -1=unvoiced)
* Subframe length is L_SUBFR
*
* Returns:
* factor (-1=unvoiced to 1=voiced) (Q15)
*/
Word32 E_GAIN_voice_factor(Word16 exc[], Word16 Q_exc, Word16 gain_pit,
Word16 code[], Word16 gain_code)
{
Word32 i, L_tmp, tmp, exp, ener1, exp1, ener2, exp2;
ener1 = E_UTIL_dot_product12(exc, exc, L_SUBFR, &exp1) >> 16;
exp1 = exp1 - (Q_exc + Q_exc);
L_tmp = (gain_pit * gain_pit) << 1;
exp = E_UTIL_norm_l(L_tmp);
tmp = (L_tmp << exp) >> 16;
ener1 = (ener1 * tmp) >> 15;
exp1 = (exp1 - exp) - 10; /* 10 -> gain_pit Q14 to Q9 */
ener2 = E_UTIL_dot_product12(code, code, L_SUBFR, &exp2) >> 16;
exp = E_UTIL_norm_s(gain_code);
tmp = gain_code << exp;
tmp = (tmp * tmp) >> 15;
ener2 = (ener2 * tmp) >> 15;
exp2 = exp2 - (exp + exp);
i = exp1 - exp2;
if (i >= 0)
{
ener1 = ener1 >> 1;
ener2 = ener2 >> (i + 1);
}
else
{
i = 1 - i;
if (i < 32)
{
ener1 = ener1 >> i;
}
else
{
ener1 = 0;
}
ener2 = ener2 >> 1;
}
tmp = ener1 - ener2;
ener1 = (ener1 + ener2) + 1;
tmp = (tmp << 15) / ener1;
return (tmp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -