4 /******************************************************************
6 iLBC Speech Coder ANSI-C Source Code
10 Copyright (C) The Internet Society (2004).
13 ******************************************************************/
15 #include "iLBC_define.h"
17 #include "constants.h"
21 /*----------------------------------------------------------------*
22 * Construct an additional codebook vector by filtering the
23 * initial codebook buffer. This vector is then used to expand
24 * the codebook with an additional section.
25 *---------------------------------------------------------------*/
28 float *cbvectors
, /* (o) Codebook vectors for the
30 float *mem
, /* (i) Buffer to create codebook
32 int lMem
/* (i) Length of buffer */
36 float tempbuff2
[CB_MEML
+CB_FILTERLEN
];
39 memset(tempbuff2
, 0, (CB_HALFFILTERLEN
-1)*sizeof(float));
40 memcpy(&tempbuff2
[CB_HALFFILTERLEN
-1], mem
, lMem
*sizeof(float));
41 memset(&tempbuff2
[lMem
+CB_HALFFILTERLEN
-1], 0,
42 (CB_HALFFILTERLEN
+1)*sizeof(float));
44 /* Create codebook vector for higher section by filtering */
48 memset(pos
, 0, lMem
*sizeof(float));
49 for (k
=0; k
<lMem
; k
++) {
51 pp1
=&cbfiltersTbl
[CB_FILTERLEN
-1];
52 for (j
=0;j
<CB_FILTERLEN
;j
++) {
53 (*pos
)+=(*pp
++)*(*pp1
--);
61 /*----------------------------------------------------------------*
62 * Search the augmented part of the codebook to find the best
64 *----------------------------------------------------------------*/
66 void searchAugmentedCB(
67 int low
, /* (i) Start index for the search */
68 int high
, /* (i) End index for the search */
69 int stage
, /* (i) Current stage */
70 int startIndex
, /* (i) Codebook index for the first
72 float *target
, /* (i) Target vector for encoding */
73 float *buffer
, /* (i) Pointer to the end of the buffer for
74 augmented codebook construction */
75 float *max_measure
, /* (i/o) Currently maximum measure */
76 int *best_index
,/* (o) Currently the best index */
77 float *gain
, /* (o) Currently the best gain */
78 float *energy
, /* (o) Energy of augmented codebook
80 float *invenergy
/* (o) Inv energy of augmented codebook
83 int icount
, ilow
, j
, tmpIndex
;
84 float *pp
, *ppo
, *ppi
, *ppe
, crossDot
, alfa
;
85 float weighted
, measure
, nrjRecursive
;
88 /* Compute the energy for the first (low-5)
89 noninterpolated samples */
90 nrjRecursive
= (float) 0.0;
91 pp
= buffer
- low
+ 1;
92 for (j
=0; j
<(low
-5); j
++) {
93 nrjRecursive
+= ( (*pp
)*(*pp
) );
99 for (icount
=low
; icount
<=high
; icount
++) {
101 /* Index of the codebook vector used for retrieving
103 tmpIndex
= startIndex
+icount
-20;
107 /* Update the energy recursively to save complexity */
108 nrjRecursive
= nrjRecursive
+ (*ppe
)*(*ppe
);
110 energy
[tmpIndex
] = nrjRecursive
;
112 /* Compute cross dot product for the first (low-5)
114 crossDot
= (float) 0.0;
118 for (j
=0; j
<ilow
; j
++) {
119 crossDot
+= target
[j
]*(*pp
++);
125 ppi
= buffer
-icount
-4;
126 for (j
=ilow
; j
<icount
; j
++) {
127 weighted
= ((float)1.0-alfa
)*(*ppo
)+alfa
*(*ppi
);
130 energy
[tmpIndex
] += weighted
*weighted
;
131 crossDot
+= target
[j
]*weighted
;
135 /* Compute energy and cross dot product for the
137 pp
= buffer
- icount
;
138 for (j
=icount
; j
<SUBL
; j
++) {
139 energy
[tmpIndex
] += (*pp
)*(*pp
);
140 crossDot
+= target
[j
]*(*pp
++);
143 if (energy
[tmpIndex
]>0.0) {
144 invenergy
[tmpIndex
]=(float)1.0/(energy
[tmpIndex
]+EPS
);
146 invenergy
[tmpIndex
] = (float) 0.0;
150 measure
= (float)-10000000.0;
152 if (crossDot
> 0.0) {
153 measure
= crossDot
*crossDot
*invenergy
[tmpIndex
];
157 measure
= crossDot
*crossDot
*invenergy
[tmpIndex
];
160 /* check if measure is better */
161 ftmp
= crossDot
*invenergy
[tmpIndex
];
163 if ((measure
>*max_measure
) && (fabs(ftmp
)<CB_MAXGAIN
)) {
164 *best_index
= tmpIndex
;
165 *max_measure
= measure
;
174 /*----------------------------------------------------------------*
175 * Recreate a specific codebook vector from the augmented part.
177 *----------------------------------------------------------------*/
179 void createAugmentedVec(
180 int index
, /* (i) Index for the augmented vector
182 float *buffer
, /* (i) Pointer to the end of the buffer for
183 augmented codebook construction */
184 float *cbVec
/* (o) The construced codebook vector */
187 float *pp
, *ppo
, *ppi
, alfa
, alfa1
, weighted
;
191 /* copy the first noninterpolated part */
194 memcpy(cbVec
,pp
,sizeof(float)*index
);
201 ppi
= buffer
-index
-5;
202 for (j
=ilow
; j
<index
; j
++) {
203 weighted
= ((float)1.0-alfa
)*(*ppo
)+alfa
*(*ppi
);
210 /* copy the second noninterpolated part */
213 memcpy(cbVec
+index
,pp
,sizeof(float)*(SUBL
-index
));