📄 exc2.c
字号:
for (i=k+1; i<NB_POS; i++)
{
cor += h[m+0]*h2[m+0] + h[m+1]*h2[m+1]; p3[t] = cor;
cor += h[m+2]*h2[m+2] + h[m+3]*h2[m+3]; p2[t] = cor;
cor += h[m+4]*h2[m+4] + h[m+5]*h2[m+5]; p1[t] = cor;
cor += h[m+6]*h2[m+6] + h[m+7]*h2[m+7]; p0[t] = cor;
t -= (NB_POS+1);
m += 8;
}
cor += h[m+0]*h2[m+0] + h[m+1]*h2[m+1]; p3[t] = cor;
h2 += STEP;
p3 -= NB_POS;
p2 -= 1;
p1 -= 1;
p0 -= 1;
}
return;
}
/*
**
** Function: Corr_h_X()
**
** Description: Compute correlations of input response h[] with
** the target vector X[].
**
** Links to the text: Section 2.16
**
** Arguments:
**
** FLOAT h[] Impulse response.
** FLOAT X[] Target vector.
** FLOAT D[] Correlations.
**
** Outputs:
**
** FLOAT D[] Correlations.
**
** Return value: None
*/
void Cor_h_X(FLOAT h[],FLOAT X[],FLOAT D[])
{
int i;
for (i=0; i < SubFrLen; i++)
D[i] = DotProd(&X[i],h,(SubFrLen-i));
return;
}
/*
** Function: Reset_max_time()
**
** Description: This function should be called at the beginnig
** of each frame.
**
** Links to the text: Section 2.16
**
** Arguments: None
**
** Inputs: None
**
** Outputs:
**
** Word16 extra
**
** Return value: None
**
*/
static int extra;
void reset_max_time(void)
{
extra = 120;
return;
}
/*
**
** Function: D4i64_LBC
**
** Description: Algebraic codebook for LBC.
** -> 17 bits; 4 pulses in a frame of 60 samples
**
** The code length is 60, containing 4 nonzero pulses
** i0, i1, i2, i3. Each pulse can have 8 possible
** positions (positive or negative):
**
** i0 (+-1) : 0, 8, 16, 24, 32, 40, 48, 56
** i1 (+-1) : 2, 10, 18, 26, 34, 42, 50, 58
** i2 (+-1) : 4, 12, 20, 28, 36, 44, 52, (60)
** i3 (+-1) : 6, 14, 22, 30, 38, 46, 54, (62)
**
** All the pulse can be shifted by one.
** The last position of the last 2 pulses falls outside the
** frame and signifies that the pulse is not present.
** The threshold controls if a section of the innovative
** codebook should be searched or not.
**
** Links to the text: Section 2.16
**
** Input arguments:
**
** FLOAT Dn[] Correlation between target vector & impulse resp h[]
** FLOAT rr[] Correlations of impulse response h[]
** FLOAT h[] Impulse response of filters
**
** Output arguments:
**
** FLOAT cod[] Selected algebraic codeword
** FLOAT y[] Filtered codeword
** int code_shift Shift of the codeword
** int sign Signs of the 4 pulses.
**
** Return value:
**
** int Index of selected codevector
**
*/
int D4i64_LBC(FLOAT Dn[], FLOAT rr[], FLOAT h[], FLOAT cod[],
FLOAT y[], int *code_shift, int *sign)
{
int i0, i1, i2, i3, ip0, ip1, ip2, ip3;
int i, j, time;
int shif, shift;
FLOAT ps0, ps1, ps2, ps3;
FLOAT alp0, alp1, alp2, alp3;
FLOAT ps0a, ps1a, ps2a;
FLOAT ps3c, psc, alpha;
FLOAT means, max0, max1, max2, thres;
FLOAT *rri0i0,*rri1i1,*rri2i2,*rri3i3;
FLOAT *rri0i1,*rri0i2,*rri0i3;
FLOAT *rri1i2,*rri1i3,*rri2i3;
FLOAT *ptr_ri0i0,*ptr_ri1i1,*ptr_ri2i2,*ptr_ri3i3;
FLOAT *ptr_ri0i1,*ptr_ri0i2,*ptr_ri0i3;
FLOAT *ptr_ri1i2,*ptr_ri1i3,*ptr_ri2i3;
int p_sign[SubFrLen2/2],p_sign2[SubFrLen2/2];
/* Init pointers */
rri0i0 = rr;
rri1i1 = rri0i0 + NB_POS;
rri2i2 = rri1i1 + NB_POS;
rri3i3 = rri2i2 + NB_POS;
rri0i1 = rri3i3 + NB_POS;
rri0i2 = rri0i1 + MSIZE;
rri0i3 = rri0i2 + MSIZE;
rri1i2 = rri0i3 + MSIZE;
rri1i3 = rri1i2 + MSIZE;
rri2i3 = rri1i3 + MSIZE;
/* Extend the backward filtered target vector by zeros */
for (i=SubFrLen; i < SubFrLen2; i++)
Dn[i] = (FLOAT)0.0;
/* Chose the sign of the impulse. */
for (i=0; i<SubFrLen; i+=2)
{
if ((Dn[i] + Dn[i+1]) >= (FLOAT)0.0)
{
p_sign[i/2] = 1;
p_sign2[i/2] = 2;
}
else
{
p_sign[i/2] = -1;
p_sign2[i/2] = -2;
Dn[i] = -Dn[i];
Dn[i+1] = -Dn[i+1];
}
}
p_sign[30] = p_sign[31] = 1;
p_sign2[30] = p_sign2[31] = 2;
/* - Compute the search threshold after three pulses */
/* odd positions */
/* Find maximum of Dn[i0]+Dn[i1]+Dn[i2] */
max0 = Dn[0];
max1 = Dn[2];
max2 = Dn[4];
for (i=8; i < SubFrLen; i+=STEP)
{
if (Dn[i] > max0)
max0 = Dn[i];
if (Dn[i+2] > max1)
max1 = Dn[i+2];
if (Dn[i+4] > max2)
max2 = Dn[i+4];
}
max0 = max0 + max1 + max2;
/* Find means of Dn[i0]+Dn[i1]+Dn[i] */
means = (FLOAT)0.0;
for (i=0; i < SubFrLen; i+=STEP)
means += Dn[i+4] + Dn[i+2] + Dn[i];
means *= (FLOAT)0.125;
thres = means + (max0-means)*(FLOAT)0.5;
/* even positions */
/* Find maximum of Dn[i0]+Dn[i1]+Dn[i2] */
max0 = Dn[1];
max1 = Dn[3];
max2 = Dn[5];
for (i=9; i < SubFrLen; i+=STEP)
{
if (Dn[i] > max0)
max0 = Dn[i];
if (Dn[i+2] > max1)
max1 = Dn[i+2];
if (Dn[i+4] > max2)
max2 = Dn[i+4];
}
max0 = max0 + max1 + max2;
/* Find means of Dn[i0]+Dn[i1]+Dn[i2] */
means = (FLOAT)0.0;
for (i=1; i < SubFrLen; i+=STEP)
means += Dn[i+4] + Dn[i+2] + Dn[i];
means *= (FLOAT)0.125;
max1 = means + (max0-means)*(FLOAT)0.5;
/* Keep maximum threshold between odd and even position */
if (max1 > thres)
thres = max1;
/* Modification of rrixiy[] to take signs into account. */
ptr_ri0i1 = rri0i1;
ptr_ri0i2 = rri0i2;
ptr_ri0i3 = rri0i3;
for (i0=0; i0<SubFrLen/2; i0+=STEP/2)
{
for (i1=2/2; i1<SubFrLen/2; i1+=STEP/2)
{
*ptr_ri0i1++ *= p_sign[i0] * p_sign2[i1];
*ptr_ri0i2++ *= p_sign[i0] * p_sign2[i1+1];
*ptr_ri0i3++ *= p_sign[i0] * p_sign2[i1+2];
}
}
ptr_ri1i2 = rri1i2;
ptr_ri1i3 = rri1i3;
for (i1=2/2; i1<SubFrLen/2; i1+=STEP/2)
{
for (i2=4/2; i2<SubFrLen2/2; i2+=STEP/2)
{
*ptr_ri1i2++ *= p_sign[i1] * p_sign2[i2];
*ptr_ri1i3++ *= p_sign[i1] * p_sign2[i2+1];
}
}
ptr_ri2i3 = rri2i3;
for (i2=4/2; i2<SubFrLen2/2; i2+=STEP/2)
{
for (i3=6/2; i3<SubFrLen2/2; i3+=STEP/2)
*ptr_ri2i3++ *= p_sign[i2] * p_sign2[i3];
}
/*-------------------------------------------------------------------
* Search the optimum positions of the four pulses which maximize
* square(correlation) / energy
* The search is performed in four nested loops. At each loop, one
* pulse contribution is added to the correlation and energy.
*
* The fourth loop is entered only if the correlation due to the
* contribution of the first three pulses exceeds the preset
* threshold.
*/
/* Default values */
ip0 = 0;
ip1 = 2;
ip2 = 4;
ip3 = 6;
shif = 0;
psc = (FLOAT)0.0;
alpha = (FLOAT)1.0;
time = max_time + extra;
/* Four loops to search innovation code. */
/* Init. pointers that depend on first loop */
ptr_ri0i0 = rri0i0;
ptr_ri0i1 = rri0i1;
ptr_ri0i2 = rri0i2;
ptr_ri0i3 = rri0i3;
/* first pulse loop */
for (i0=0; i0 < SubFrLen; i0 +=STEP)
{
ps0 = Dn[i0];
ps0a = Dn[i0+1];
alp0 = *ptr_ri0i0++;
/* Init. pointers that depend on second loop */
ptr_ri1i1 = rri1i1;
ptr_ri1i2 = rri1i2;
ptr_ri1i3 = rri1i3;
/* second pulse loop */
for (i1=2; i1 < SubFrLen; i1 +=STEP)
{
ps1 = ps0 + Dn[i1];
ps1a = ps0a + Dn[i1+1];
alp1 = alp0 + *ptr_ri1i1++ + *ptr_ri0i1++;
/* Init. pointers that depend on third loop */
ptr_ri2i2 = rri2i2;
ptr_ri2i3 = rri2i3;
/* third pulse loop */
for (i2 = 4; i2 < SubFrLen2; i2 +=STEP)
{
ps2 = ps1 + Dn[i2];
ps2a = ps1a + Dn[i2+1];
alp2 = alp1 + *ptr_ri2i2++ + *ptr_ri0i2++ + *ptr_ri1i2++;
/* Decide the shift */
shift = 0;
if (ps2a > ps2)
{
shift = 1;
ps2 = ps2a;
}
/* Test threshold */
if (ps2 > thres)
{
/* Init. pointers that depend on 4th loop */
ptr_ri3i3 = rri3i3;
/* 4th pulse loop */
for (i3 = 6; i3 < SubFrLen2; i3 +=STEP)
{
ps3 = ps2 + Dn[i3+shift];
alp3 = alp2 + *ptr_ri3i3++ +
*ptr_ri0i3++ + *ptr_ri1i3++ + *ptr_ri2i3++;
ps3c = ps3 * ps3;
if ((ps3c * alpha) > (psc * alp3))
{
psc = ps3c;
alpha = alp3;
ip0 = i0;
ip1 = i1;
ip2 = i2;
ip3 = i3;
shif = shift;
}
}
time--;
/* Maximum time finish */
if (time <= 0)
goto end_search;
ptr_ri0i3 -= NB_POS;
ptr_ri1i3 -= NB_POS;
}
else
ptr_ri2i3 += NB_POS;
}
ptr_ri0i2 -= NB_POS;
ptr_ri1i3 += NB_POS;
}
ptr_ri0i2 += NB_POS;
ptr_ri0i3 += NB_POS;
}
end_search:
extra = time;
/* Set the sign of impulses */
i0 = p_sign[(ip0 >> 1)];
i1 = p_sign[(ip1 >> 1)];
i2 = p_sign[(ip2 >> 1)];
i3 = p_sign[(ip3 >> 1)];
/* Find the codeword corresponding to the selected positions */
for (i=0; i<SubFrLen; i++)
cod[i] = (FLOAT)0.0;
if (shif > 0)
{
ip0++;
ip1++;
ip2++;
ip3++;
}
cod[ip0] = (FLOAT)i0;
cod[ip1] = (FLOAT)i1;
if (ip2<SubFrLen)
cod[ip2] = (FLOAT)i2;
if (ip3<SubFrLen)
cod[ip3] = (FLOAT)i3;
/* find the filtered codeword */
for (i=0; i < SubFrLen; i++)
y[i] = (FLOAT)0.0;
if (i0 > 0)
for (i=ip0, j=0; i<SubFrLen; i++, j++)
y[i] = y[i] + h[j];
else
for (i=ip0, j=0; i<SubFrLen; i++, j++)
y[i] = y[i] - h[j];
if (i1 > 0)
for (i=ip1, j=0; i<SubFrLen; i++, j++)
y[i] = y[i] + h[j];
else
for (i=ip1, j=0; i<SubFrLen; i++, j++)
y[i] = y[i] - h[j];
if (ip2<SubFrLen)
{
if (i2 > 0)
for (i=ip2, j=0; i<SubFrLen; i++, j++)
y[i] = y[i] + h[j];
else
for (i=ip2, j=0; i<SubFrLen; i++, j++)
y[i] = y[i] - h[j];
}
if (ip3<SubFrLen)
{
if (i3 > 0)
for (i=ip3, j=0; i<SubFrLen; i++, j++)
y[i] = y[i] + h[j];
else
for (i=ip3, j=0; i<SubFrLen; i++, j++)
y[i] = y[i] - h[j];
}
*code_shift = shif;
*sign = 0;
if (i0 > 0)
*sign += 1;
if (i1 > 0)
*sign += 2;
if (i2 > 0)
*sign += 4;
if (i3 > 0)
*sign += 8;
i = ((ip3 >> 3) << 9) + ((ip2 >> 3) << 6) + ((ip1 >> 3) << 3) + (ip0 >> 3);
return i;
}
/*
**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -