Make file access in cdr_custom similar to cdr_csv.
[asterisk-bristuff.git] / codecs / ilbc / createCB.c
bloba7efd93801a3bf574018a445d137917f2952e72a
4 /******************************************************************
6 iLBC Speech Coder ANSI-C Source Code
8 createCB.c
10 Copyright (C) The Internet Society (2004).
11 All Rights Reserved.
13 ******************************************************************/
15 #include "iLBC_define.h"
16 #include "createCB.h"
17 #include "constants.h"
18 #include <string.h>
19 #include <math.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 *---------------------------------------------------------------*/
27 void filteredCBvecs(
28 float *cbvectors, /* (o) Codebook vectors for the
29 higher section */
30 float *mem, /* (i) Buffer to create codebook
31 vector from */
32 int lMem /* (i) Length of buffer */
34 int j, k;
35 float *pp, *pp1;
36 float tempbuff2[CB_MEML+CB_FILTERLEN];
37 float *pos;
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 */
46 /* do filtering */
47 pos=cbvectors;
48 memset(pos, 0, lMem*sizeof(float));
49 for (k=0; k<lMem; k++) {
50 pp=&tempbuff2[k];
51 pp1=&cbfiltersTbl[CB_FILTERLEN-1];
52 for (j=0;j<CB_FILTERLEN;j++) {
53 (*pos)+=(*pp++)*(*pp1--);
55 pos++;
61 /*----------------------------------------------------------------*
62 * Search the augmented part of the codebook to find the best
63 * measure.
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
71 aug vector */
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
79 vectors */
80 float *invenergy/* (o) Inv energy of augmented codebook
81 vectors */
82 ) {
83 int icount, ilow, j, tmpIndex;
84 float *pp, *ppo, *ppi, *ppe, crossDot, alfa;
85 float weighted, measure, nrjRecursive;
86 float ftmp;
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) );
94 pp++;
96 ppe = buffer - low;
99 for (icount=low; icount<=high; icount++) {
101 /* Index of the codebook vector used for retrieving
102 energy values */
103 tmpIndex = startIndex+icount-20;
105 ilow = icount-4;
107 /* Update the energy recursively to save complexity */
108 nrjRecursive = nrjRecursive + (*ppe)*(*ppe);
109 ppe--;
110 energy[tmpIndex] = nrjRecursive;
112 /* Compute cross dot product for the first (low-5)
113 samples */
114 crossDot = (float) 0.0;
117 pp = buffer-icount;
118 for (j=0; j<ilow; j++) {
119 crossDot += target[j]*(*pp++);
122 /* interpolation */
123 alfa = (float) 0.2;
124 ppo = buffer-4;
125 ppi = buffer-icount-4;
126 for (j=ilow; j<icount; j++) {
127 weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
128 ppo++;
129 ppi++;
130 energy[tmpIndex] += weighted*weighted;
131 crossDot += target[j]*weighted;
132 alfa += (float)0.2;
135 /* Compute energy and cross dot product for the
136 remaining samples */
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);
145 } else {
146 invenergy[tmpIndex] = (float) 0.0;
149 if (stage==0) {
150 measure = (float)-10000000.0;
152 if (crossDot > 0.0) {
153 measure = crossDot*crossDot*invenergy[tmpIndex];
156 else {
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;
166 *gain = ftmp;
174 /*----------------------------------------------------------------*
175 * Recreate a specific codebook vector from the augmented part.
177 *----------------------------------------------------------------*/
179 void createAugmentedVec(
180 int index, /* (i) Index for the augmented vector
181 to be created */
182 float *buffer, /* (i) Pointer to the end of the buffer for
183 augmented codebook construction */
184 float *cbVec/* (o) The construced codebook vector */
186 int ilow, j;
187 float *pp, *ppo, *ppi, alfa, alfa1, weighted;
189 ilow = index-5;
191 /* copy the first noninterpolated part */
193 pp = buffer-index;
194 memcpy(cbVec,pp,sizeof(float)*index);
196 /* interpolation */
198 alfa1 = (float)0.2;
199 alfa = 0.0;
200 ppo = buffer-5;
201 ppi = buffer-index-5;
202 for (j=ilow; j<index; j++) {
203 weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi);
204 ppo++;
205 ppi++;
206 cbVec[j] = weighted;
207 alfa += alfa1;
210 /* copy the second noninterpolated part */
212 pp = buffer - index;
213 memcpy(cbVec+index,pp,sizeof(float)*(SUBL-index));