Mercurial > hg > audiostuff
comparison intercom/ilbc/StateSearchW.c @ 2:13be24d74cd2
import intercom-0.4.1
author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
---|---|
date | Fri, 25 Jun 2010 09:57:52 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1:9cadc470e3da | 2:13be24d74cd2 |
---|---|
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 | |
26 void AbsQuantW(iLBC_Enc_Inst_t * iLBCenc_inst, | |
27 /* (i) Encoder instance */ | |
28 float *in, /* (i) vector to encode */ | |
29 float *syntDenum, /* (i) denominator of synthesis filter */ | |
30 float *weightDenum, /* (i) denominator of weighting filter */ | |
31 int *out, /* (o) vector of quantizer indexes */ | |
32 int len, /* (i) length of vector to encode and | |
33 vector of quantizer indexes */ | |
34 int state_first /* (i) position of start state in the | |
35 80 vec */ | |
36 ) | |
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 | |
51 | |
52 | |
53 /* initialization of pointer for filtering */ | |
54 | |
55 syntOut = &syntOutBuf[LPC_FILTERORDER]; | |
56 | |
57 /* synthesis and weighting filters on input */ | |
58 | |
59 if (state_first) { | |
60 AllPoleFilter(in, weightDenum, SUBL, LPC_FILTERORDER); | |
61 } else { | |
62 AllPoleFilter(in, weightDenum, | |
63 iLBCenc_inst->state_short_len - SUBL, LPC_FILTERORDER); | |
64 } | |
65 | |
66 /* encoding loop */ | |
67 | |
68 for (n = 0; n < len; n++) { | |
69 | |
70 /* time update of filter coefficients */ | |
71 | |
72 if ((state_first) && (n == SUBL)) { | |
73 syntDenum += (LPC_FILTERORDER + 1); | |
74 weightDenum += (LPC_FILTERORDER + 1); | |
75 | |
76 /* synthesis and weighting filters on input */ | |
77 AllPoleFilter(&in[n], weightDenum, len - n, LPC_FILTERORDER); | |
78 | |
79 } else if ((state_first == 0) && | |
80 (n == (iLBCenc_inst->state_short_len - SUBL))) { | |
81 syntDenum += (LPC_FILTERORDER + 1); | |
82 weightDenum += (LPC_FILTERORDER + 1); | |
83 | |
84 /* synthesis and weighting filters on input */ | |
85 AllPoleFilter(&in[n], weightDenum, len - n, LPC_FILTERORDER); | |
86 | |
87 } | |
88 | |
89 /* prediction of synthesized and weighted input */ | |
90 | |
91 syntOut[n] = 0.0; | |
92 AllPoleFilter(&syntOut[n], weightDenum, 1, LPC_FILTERORDER); | |
93 | |
94 /* quantization */ | |
95 | |
96 toQ = in[n] - syntOut[n]; | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 sort_sq(&xq, &index, toQ, state_sq3Tbl, 8); | |
103 out[n] = index; | |
104 syntOut[n] = state_sq3Tbl[out[n]]; | |
105 | |
106 /* update of the prediction filter */ | |
107 | |
108 AllPoleFilter(&syntOut[n], weightDenum, 1, LPC_FILTERORDER); | |
109 } | |
110 } | |
111 | |
112 /*----------------------------------------------------------------* | |
113 * encoding of start state | |
114 *---------------------------------------------------------------*/ | |
115 | |
116 void StateSearchW(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 { | |
129 float dtmp, maxVal; | |
130 float tmpbuf[LPC_FILTERORDER + 2 * STATE_SHORT_LEN_30MS]; | |
131 float *tmp, numerator[1 + LPC_FILTERORDER]; | |
132 float foutbuf[LPC_FILTERORDER + 2 * STATE_SHORT_LEN_30MS], *fout; | |
133 int k; | |
134 float qmax, scal; | |
135 | |
136 /* initialization of buffers and filter coefficients */ | |
137 | |
138 memset(tmpbuf, 0, LPC_FILTERORDER * sizeof(float)); | |
139 memset(foutbuf, 0, LPC_FILTERORDER * sizeof(float)); | |
140 for (k = 0; k < LPC_FILTERORDER; k++) { | |
141 numerator[k] = syntDenum[LPC_FILTERORDER - k]; | |
142 } | |
143 numerator[LPC_FILTERORDER] = syntDenum[0]; | |
144 tmp = &tmpbuf[LPC_FILTERORDER]; | |
145 fout = &foutbuf[LPC_FILTERORDER]; | |
146 | |
147 /* circular convolution with the all-pass filter */ | |
148 | |
149 | |
150 | |
151 | |
152 | |
153 | |
154 memcpy(tmp, residual, len * sizeof(float)); | |
155 memset(tmp + len, 0, len * sizeof(float)); | |
156 ZeroPoleFilter(tmp, numerator, syntDenum, 2 * len, | |
157 LPC_FILTERORDER, fout); | |
158 for (k = 0; k < len; k++) { | |
159 fout[k] += fout[k + len]; | |
160 } | |
161 | |
162 /* identification of the maximum amplitude value */ | |
163 | |
164 maxVal = fout[0]; | |
165 for (k = 1; k < len; k++) { | |
166 | |
167 if (fout[k] * fout[k] > maxVal * maxVal) { | |
168 maxVal = fout[k]; | |
169 } | |
170 } | |
171 maxVal = (float) fabs(maxVal); | |
172 | |
173 /* encoding of the maximum amplitude value */ | |
174 | |
175 if (maxVal < 10.0) { | |
176 maxVal = 10.0; | |
177 } | |
178 maxVal = (float) log10(maxVal); | |
179 sort_sq(&dtmp, idxForMax, maxVal, state_frgqTbl, 64); | |
180 | |
181 /* decoding of the maximum amplitude representation value, | |
182 and corresponding scaling of start state */ | |
183 | |
184 maxVal = state_frgqTbl[*idxForMax]; | |
185 qmax = (float) pow(10, maxVal); | |
186 scal = (float) (4.5) / qmax; | |
187 for (k = 0; k < len; k++) { | |
188 fout[k] *= scal; | |
189 } | |
190 | |
191 /* predictive noise shaping encoding of scaled start state */ | |
192 | |
193 AbsQuantW(iLBCenc_inst, fout, syntDenum, | |
194 weightDenum, idxVec, len, state_first); | |
195 } |