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