3
/******************************************************************
5
iLBC Speech Coder ANSI-C Source Code
9
Copyright (C) The Internet Society (2004).
12
******************************************************************/
18
#include "iLBC_define.h"
19
#include "constants.h"
23
/*----------------------------------------------------------------*
24
* Construct an additional codebook vector by filtering the
25
* initial codebook buffer. This vector is then used to expand
26
* the codebook with an additional section.
27
*---------------------------------------------------------------*/
30
float *cbvectors, /* (o) Codebook vectors for the
32
float *mem, /* (i) Buffer to create codebook
34
int lMem /* (i) Length of buffer */
38
float tempbuff2[CB_MEML+CB_FILTERLEN];
41
memset(tempbuff2, 0, (CB_HALFFILTERLEN-1)*sizeof(float));
42
memcpy(&tempbuff2[CB_HALFFILTERLEN-1], mem, lMem*sizeof(float));
43
memset(&tempbuff2[lMem+CB_HALFFILTERLEN-1], 0,
44
(CB_HALFFILTERLEN+1)*sizeof(float));
46
/* Create codebook vector for higher section by filtering */
50
memset(pos, 0, lMem*sizeof(float));
51
for (k=0; k<lMem; k++) {
53
pp1=&cbfiltersTbl[CB_FILTERLEN-1];
54
for (j=0;j<CB_FILTERLEN;j++) {
55
(*pos)+=(*pp++)*(*pp1--);
61
/*----------------------------------------------------------------*
62
* Search the augmented part of the codebook to find the best
64
*----------------------------------------------------------------*/
71
void searchAugmentedCB(
72
int low, /* (i) Start index for the search */
73
int high, /* (i) End index for the search */
74
int stage, /* (i) Current stage */
75
int startIndex, /* (i) Codebook index for the first
77
float *target, /* (i) Target vector for encoding */
78
float *buffer, /* (i) Pointer to the end of the buffer for
79
augmented codebook construction */
80
float *max_measure, /* (i/o) Currently maximum measure */
81
int *best_index,/* (o) Currently the best index */
82
float *gain, /* (o) Currently the best gain */
83
float *energy, /* (o) Energy of augmented codebook
85
float *invenergy/* (o) Inv energy of augmented codebook
88
int icount, ilow, j, tmpIndex;
89
float *pp, *ppo, *ppi, *ppe, crossDot, alfa;
90
float weighted, measure, nrjRecursive;
93
/* Compute the energy for the first (low-5)
94
noninterpolated samples */
95
nrjRecursive = (float) 0.0;
96
pp = buffer - low + 1;
97
for (j=0; j<(low-5); j++) {
98
nrjRecursive += ( (*pp)*(*pp) );
104
for (icount=low; icount<=high; icount++) {
106
/* Index of the codebook vector used for retrieving
108
tmpIndex = startIndex+icount-20;
112
/* Update the energy recursively to save complexity */
113
nrjRecursive = nrjRecursive + (*ppe)*(*ppe);
115
energy[tmpIndex] = nrjRecursive;
117
/* Compute cross dot product for the first (low-5)
124
crossDot = (float) 0.0;
126
for (j=0; j<ilow; j++) {
127
crossDot += target[j]*(*pp++);
133
ppi = buffer-icount-4;
134
for (j=ilow; j<icount; j++) {
135
weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
138
energy[tmpIndex] += weighted*weighted;
139
crossDot += target[j]*weighted;
143
/* Compute energy and cross dot product for the
145
pp = buffer - icount;
146
for (j=icount; j<SUBL; j++) {
147
energy[tmpIndex] += (*pp)*(*pp);
148
crossDot += target[j]*(*pp++);
151
if (energy[tmpIndex]>0.0) {
152
invenergy[tmpIndex]=(float)1.0/(energy[tmpIndex]+EPS);
154
invenergy[tmpIndex] = (float) 0.0;
158
measure = (float)-10000000.0;
160
if (crossDot > 0.0) {
161
measure = crossDot*crossDot*invenergy[tmpIndex];
165
measure = crossDot*crossDot*invenergy[tmpIndex];
168
/* check if measure is better */
169
ftmp = crossDot*invenergy[tmpIndex];
171
if ((measure>*max_measure) && (fabs(ftmp)<CB_MAXGAIN)) {
177
*best_index = tmpIndex;
178
*max_measure = measure;
185
/*----------------------------------------------------------------*
186
* Recreate a specific codebook vector from the augmented part.
188
*----------------------------------------------------------------*/
190
void createAugmentedVec(
191
int index, /* (i) Index for the augmented vector
193
float *buffer, /* (i) Pointer to the end of the buffer for
194
augmented codebook construction */
195
float *cbVec/* (o) The construced codebook vector */
198
float *pp, *ppo, *ppi, alfa, alfa1, weighted;
202
/* copy the first noninterpolated part */
205
memcpy(cbVec,pp,sizeof(float)*index);
212
ppi = buffer-index-5;
213
for (j=ilow; j<index; j++) {
214
weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
221
/* copy the second noninterpolated part */
224
memcpy(cbVec+index,pp,sizeof(float)*(SUBL-index));