1 2 /****************************************************************** 3 4 iLBC Speech Coder ANSI-C Source Code 5 6 helpfun.c 7 8 Copyright (C) The Internet Society (2004). 9 All Rights Reserved. 10 11 ******************************************************************/ 12 13 #include <math.h> 14 15 #include "iLBC_define.h" 16 #include "constants.h" 17 18 /*----------------------------------------------------------------* 19 * calculation of auto correlation 20 *---------------------------------------------------------------*/ 21 autocorr(float * r,const float * x,int N,int order)22 void autocorr( 23 float *r, /* (o) autocorrelation vector */ 24 const float *x, /* (i) data vector */ 25 int N, /* (i) length of data vector */ 26 int order /* largest lag for calculated 27 autocorrelations */ 28 ){ 29 int lag, n; 30 float sum; 31 32 for (lag = 0; lag <= order; lag++) { 33 sum = 0; 34 for (n = 0; n < N - lag; n++) { 35 sum += x[n] * x[n+lag]; 36 } 37 r[lag] = sum; 38 } 39 } 40 41 42 43 /*----------------------------------------------------------------* 44 * window multiplication 45 *---------------------------------------------------------------*/ 46 window(float * z,const float * x,const float * y,int N)47 void window( 48 float *z, /* (o) the windowed data */ 49 const float *x, /* (i) the original data vector */ 50 const float *y, /* (i) the window */ 51 int N /* (i) length of all vectors */ 52 ){ 53 int i; 54 55 for (i = 0; i < N; i++) { 56 z[i] = x[i] * y[i]; 57 } 58 } 59 60 /*----------------------------------------------------------------* 61 * levinson-durbin solution for lpc coefficients 62 *---------------------------------------------------------------*/ 63 levdurb(float * a,float * k,float * r,int order)64 void levdurb( 65 float *a, /* (o) lpc coefficient vector starting 66 with 1.0 */ 67 float *k, /* (o) reflection coefficients */ 68 float *r, /* (i) autocorrelation vector */ 69 int order /* (i) order of lpc filter */ 70 ){ 71 float sum, alpha; 72 int m, m_h, i; 73 74 a[0] = 1.0; 75 76 if (r[0] < EPS) { /* if r[0] <= 0, set LPC coeff. to zero */ 77 for (i = 0; i < order; i++) { 78 k[i] = 0; 79 a[i+1] = 0; 80 } 81 } else { 82 a[1] = k[0] = -r[1]/r[0]; 83 alpha = r[0] + r[1] * k[0]; 84 for (m = 1; m < order; m++){ 85 sum = r[m + 1]; 86 for (i = 0; i < m; i++){ 87 sum += a[i+1] * r[m - i]; 88 } 89 k[m] = -sum / alpha; 90 alpha += k[m] * sum; 91 m_h = (m + 1) >> 1; 92 for (i = 0; i < m_h; i++){ 93 sum = a[i+1] + k[m] * a[m - i]; 94 a[m - i] += k[m] * a[i+1]; 95 a[i+1] = sum; 96 97 98 } 99 a[m+1] = k[m]; 100 } 101 } 102 } 103 104 /*----------------------------------------------------------------* 105 * interpolation between vectors 106 *---------------------------------------------------------------*/ 107 interpolate(float * out,float * in1,float * in2,float coef,int length)108 void interpolate( 109 float *out, /* (o) the interpolated vector */ 110 float *in1, /* (i) the first vector for the 111 interpolation */ 112 float *in2, /* (i) the second vector for the 113 interpolation */ 114 float coef, /* (i) interpolation weights */ 115 int length /* (i) length of all vectors */ 116 ){ 117 int i; 118 float invcoef; 119 120 invcoef = (float)1.0 - coef; 121 for (i = 0; i < length; i++) { 122 out[i] = coef * in1[i] + invcoef * in2[i]; 123 } 124 } 125 126 /*----------------------------------------------------------------* 127 * lpc bandwidth expansion 128 *---------------------------------------------------------------*/ 129 bwexpand(float * out,float * in,float coef,int length)130 void bwexpand( 131 float *out, /* (o) the bandwidth expanded lpc 132 coefficients */ 133 float *in, /* (i) the lpc coefficients before bandwidth 134 expansion */ 135 float coef, /* (i) the bandwidth expansion factor */ 136 int length /* (i) the length of lpc coefficient vectors */ 137 ){ 138 int i; 139 float chirp; 140 141 chirp = coef; 142 143 out[0] = in[0]; 144 for (i = 1; i < length; i++) { 145 out[i] = chirp * in[i]; 146 chirp *= coef; 147 } 148 } 149 150 /*----------------------------------------------------------------* 151 * vector quantization 152 153 154 *---------------------------------------------------------------*/ 155 vq(float * Xq,int * index,const float * CB,float * X,int n_cb,int dim)156 void vq( 157 float *Xq, /* (o) the quantized vector */ 158 int *index, /* (o) the quantization index */ 159 const float *CB,/* (i) the vector quantization codebook */ 160 float *X, /* (i) the vector to quantize */ 161 int n_cb, /* (i) the number of vectors in the codebook */ 162 int dim /* (i) the dimension of all vectors */ 163 ){ 164 int i, j; 165 int pos, minindex; 166 float dist, tmp, mindist; 167 168 pos = 0; 169 mindist = FLOAT_MAX; 170 minindex = 0; 171 for (j = 0; j < n_cb; j++) { 172 dist = X[0] - CB[pos]; 173 dist *= dist; 174 for (i = 1; i < dim; i++) { 175 tmp = X[i] - CB[pos + i]; 176 dist += tmp*tmp; 177 } 178 179 if (dist < mindist) { 180 mindist = dist; 181 minindex = j; 182 } 183 pos += dim; 184 } 185 for (i = 0; i < dim; i++) { 186 Xq[i] = CB[minindex*dim + i]; 187 } 188 *index = minindex; 189 } 190 191 /*----------------------------------------------------------------* 192 * split vector quantization 193 *---------------------------------------------------------------*/ 194 SplitVQ(float * qX,int * index,float * X,const float * CB,int nsplit,const int * dim,const int * cbsize)195 void SplitVQ( 196 float *qX, /* (o) the quantized vector */ 197 int *index, /* (o) a vector of indexes for all vector 198 codebooks in the split */ 199 float *X, /* (i) the vector to quantize */ 200 const float *CB,/* (i) the quantizer codebook */ 201 int nsplit, /* the number of vector splits */ 202 const int *dim, /* the dimension of X and qX */ 203 const int *cbsize /* the number of vectors in the codebook */ 204 ){ 205 int cb_pos, X_pos, i; 206 207 cb_pos = 0; 208 209 210 X_pos= 0; 211 for (i = 0; i < nsplit; i++) { 212 vq(qX + X_pos, index + i, CB + cb_pos, X + X_pos, 213 cbsize[i], dim[i]); 214 X_pos += dim[i]; 215 cb_pos += dim[i] * cbsize[i]; 216 } 217 } 218 219 /*----------------------------------------------------------------* 220 * scalar quantization 221 *---------------------------------------------------------------*/ 222 sort_sq(float * xq,int * index,float x,const float * cb,int cb_size)223 void sort_sq( 224 float *xq, /* (o) the quantized value */ 225 int *index, /* (o) the quantization index */ 226 float x, /* (i) the value to quantize */ 227 const float *cb,/* (i) the quantization codebook */ 228 int cb_size /* (i) the size of the quantization codebook */ 229 ){ 230 int i; 231 232 if (x <= cb[0]) { 233 *index = 0; 234 *xq = cb[0]; 235 } else { 236 i = 0; 237 while ((x > cb[i]) && i < cb_size - 1) { 238 i++; 239 } 240 241 if (x > ((cb[i] + cb[i - 1])/2)) { 242 *index = i; 243 *xq = cb[i]; 244 } else { 245 *index = i - 1; 246 *xq = cb[i - 1]; 247 } 248 } 249 } 250 251 /*----------------------------------------------------------------* 252 * check for stability of lsf coefficients 253 *---------------------------------------------------------------*/ 254 LSF_check(float * lsf,int dim,int NoAn)255 int LSF_check( /* (o) 1 for stable lsf vectors and 0 for 256 nonstable ones */ 257 float *lsf, /* (i) a table of lsf vectors */ 258 int dim, /* (i) the dimension of each lsf vector */ 259 int NoAn /* (i) the number of lsf vectors in the 260 table */ 261 ){ 262 int k,n,m, Nit=2, change=0,pos; 263 float tmp; 264 265 266 static float eps=(float)0.039; /* 50 Hz */ 267 static float eps2=(float)0.0195; 268 static float maxlsf=(float)3.14; /* 4000 Hz */ 269 static float minlsf=(float)0.01; /* 0 Hz */ 270 271 /* LSF separation check*/ 272 273 for (n=0; n<Nit; n++) { /* Run through a couple of times */ 274 for (m=0; m<NoAn; m++) { /* Number of analyses per frame */ 275 for (k=0; k<(dim-1); k++) { 276 pos=m*dim+k; 277 278 if ((lsf[pos+1]-lsf[pos])<eps) { 279 280 if (lsf[pos+1]<lsf[pos]) { 281 tmp=lsf[pos+1]; 282 lsf[pos+1]= lsf[pos]+eps2; 283 lsf[pos]= lsf[pos+1]-eps2; 284 } else { 285 lsf[pos]-=eps2; 286 lsf[pos+1]+=eps2; 287 } 288 change=1; 289 } 290 291 if (lsf[pos]<minlsf) { 292 lsf[pos]=minlsf; 293 change=1; 294 } 295 296 if (lsf[pos]>maxlsf) { 297 lsf[pos]=maxlsf; 298 change=1; 299 } 300 } 301 } 302 } 303 304 return change; 305 } 306 307 308