1 
2    /******************************************************************
3 
4        iLBC Speech Coder ANSI-C Source Code
5 
6        StateSearchW.c
7 
8        Copyright (C) The Internet Society (2004).
9        All Rights Reserved.
10 
11    ******************************************************************/
12 
13    #include <math.h>
14    #include <string.h>
15 
16    #include "iLBC_define.h"
17    #include "constants.h"
18    #include "filter.h"
19    #include "helpfun.h"
20 
21    /*----------------------------------------------------------------*
22     *  predictive noise shaping encoding of scaled start state
23     *  (subrutine for StateSearchW)
24     *---------------------------------------------------------------*/
25 
AbsQuantW(iLBC_Enc_Inst_t * iLBCenc_inst,float * in,float * syntDenum,float * weightDenum,int * out,int len,int state_first)26    void AbsQuantW(
27        iLBC_Enc_Inst_t *iLBCenc_inst,
28                            /* (i) Encoder instance */
29        float *in,          /* (i) vector to encode */
30        float *syntDenum,   /* (i) denominator of synthesis filter */
31        float *weightDenum, /* (i) denominator of weighting filter */
32        int *out,           /* (o) vector of quantizer indexes */
33        int len,        /* (i) length of vector to encode and
34                                   vector of quantizer indexes */
35        int state_first     /* (i) position of start state in the
36                                   80 vec */
37    ){
38        float *syntOut;
39        float syntOutBuf[LPC_FILTERORDER+STATE_SHORT_LEN_30MS];
40        float toQ, xq;
41        int n;
42        int index;
43 
44        /* initialization of buffer for filtering */
45 
46        memset(syntOutBuf, 0, LPC_FILTERORDER*sizeof(float));
47 
48 
49 
50        /* initialization of pointer for filtering */
51 
52        syntOut = &syntOutBuf[LPC_FILTERORDER];
53 
54        /* synthesis and weighting filters on input */
55 
56        if (state_first) {
57            AllPoleFilter (in, weightDenum, SUBL, LPC_FILTERORDER);
58        } else {
59            AllPoleFilter (in, weightDenum,
60                iLBCenc_inst->state_short_len-SUBL,
61                LPC_FILTERORDER);
62        }
63 
64        /* encoding loop */
65 
66        for (n=0; n<len; n++) {
67 
68            /* time update of filter coefficients */
69 
70            if ((state_first)&&(n==SUBL)){
71                syntDenum += (LPC_FILTERORDER+1);
72                weightDenum += (LPC_FILTERORDER+1);
73 
74                /* synthesis and weighting filters on input */
75                AllPoleFilter (&in[n], weightDenum, len-n,
76                    LPC_FILTERORDER);
77 
78            } else if ((state_first==0)&&
79                (n==(iLBCenc_inst->state_short_len-SUBL))) {
80                syntDenum += (LPC_FILTERORDER+1);
81                weightDenum += (LPC_FILTERORDER+1);
82 
83                /* synthesis and weighting filters on input */
84                AllPoleFilter (&in[n], weightDenum, len-n,
85                    LPC_FILTERORDER);
86 
87            }
88 
89            /* prediction of synthesized and weighted input */
90 
91            syntOut[n] = 0.0;
92            AllPoleFilter (&syntOut[n], weightDenum, 1,
93                LPC_FILTERORDER);
94 
95            /* quantization */
96 
97            toQ = in[n]-syntOut[n];
98            sort_sq(&xq, &index, toQ, state_sq3Tbl, 8);
99            out[n]=index;
100            syntOut[n] = state_sq3Tbl[out[n]];
101 
102            /* update of the prediction filter */
103 
104 
105 
106            AllPoleFilter(&syntOut[n], weightDenum, 1,
107                LPC_FILTERORDER);
108        }
109    }
110 
111    /*----------------------------------------------------------------*
112     *  encoding of start state
113     *---------------------------------------------------------------*/
114 
StateSearchW(iLBC_Enc_Inst_t * iLBCenc_inst,float * residual,float * syntDenum,float * weightDenum,int * idxForMax,int * idxVec,int len,int state_first)115    void StateSearchW(
116        iLBC_Enc_Inst_t *iLBCenc_inst,
117                            /* (i) Encoder instance */
118        float *residual,/* (i) target residual vector */
119        float *syntDenum,   /* (i) lpc synthesis filter */
120        float *weightDenum, /* (i) weighting filter denuminator */
121        int *idxForMax,     /* (o) quantizer index for maximum
122                                   amplitude */
123        int *idxVec,    /* (o) vector of quantization indexes */
124        int len,        /* (i) length of all vectors */
125        int state_first     /* (i) position of start state in the
126                                   80 vec */
127    ){
128        float dtmp, maxVal;
129        float tmpbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN_30MS];
130        float *tmp, numerator[1+LPC_FILTERORDER];
131        float foutbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN_30MS], *fout;
132        int k;
133        float qmax, scal;
134 
135        /* initialization of buffers and filter coefficients */
136 
137        memset(tmpbuf, 0, LPC_FILTERORDER*sizeof(float));
138        memset(foutbuf, 0, LPC_FILTERORDER*sizeof(float));
139        for (k=0; k<LPC_FILTERORDER; k++) {
140            numerator[k]=syntDenum[LPC_FILTERORDER-k];
141        }
142        numerator[LPC_FILTERORDER]=syntDenum[0];
143        tmp = &tmpbuf[LPC_FILTERORDER];
144        fout = &foutbuf[LPC_FILTERORDER];
145 
146        /* circular convolution with the all-pass filter */
147 
148        memcpy(tmp, residual, len*sizeof(float));
149        memset(tmp+len, 0, len*sizeof(float));
150        ZeroPoleFilter(tmp, numerator, syntDenum, 2*len,
151            LPC_FILTERORDER, fout);
152        for (k=0; k<len; k++) {
153            fout[k] += fout[k+len];
154        }
155 
156        /* identification of the maximum amplitude value */
157 
158        maxVal = fout[0];
159 
160 
161        for (k=1; k<len; k++) {
162 
163            if (fout[k]*fout[k] > maxVal*maxVal){
164                maxVal = fout[k];
165            }
166        }
167        maxVal=(float)fabs(maxVal);
168 
169        /* encoding of the maximum amplitude value */
170 
171        if (maxVal < 10.0) {
172            maxVal = 10.0;
173        }
174        maxVal = (float)log10(maxVal);
175        sort_sq(&dtmp, idxForMax, maxVal, state_frgqTbl, 64);
176 
177        /* decoding of the maximum amplitude representation value,
178           and corresponding scaling of start state */
179 
180        maxVal=state_frgqTbl[*idxForMax];
181        qmax = (float)pow(10,maxVal);
182        scal = (float)(4.5)/qmax;
183        for (k=0; k<len; k++){
184            fout[k] *= scal;
185        }
186 
187        /* predictive noise shaping encoding of scaled start state */
188 
189        AbsQuantW(iLBCenc_inst, fout,syntDenum,
190            weightDenum,idxVec, len, state_first);
191    }
192 
193 
194