Coverage Report

Created: 2025-11-24 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/speex/libspeex/lpc.c
Line
Count
Source
1
/*
2
  Copyright 1992, 1993, 1994 by Jutta Degener and Carsten Bormann,
3
  Technische Universitaet Berlin
4
5
  Any use of this software is permitted provided that this notice is not
6
  removed and that neither the authors nor the Technische Universitaet Berlin
7
  are deemed to have made any representations as to the suitability of this
8
  software for any purpose nor are held responsible for any defects of
9
  this software.  THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
10
11
  As a matter of courtesy, the authors request to be informed about uses
12
  this software has found, about bugs in this software, and about any
13
  improvements that may be of general interest.
14
15
  Berlin, 28.11.1994
16
  Jutta Degener
17
  Carsten Bormann
18
19
20
   Code modified by Jean-Marc Valin
21
22
   Speex License:
23
24
   Redistribution and use in source and binary forms, with or without
25
   modification, are permitted provided that the following conditions
26
   are met:
27
28
   - Redistributions of source code must retain the above copyright
29
   notice, this list of conditions and the following disclaimer.
30
31
   - Redistributions in binary form must reproduce the above copyright
32
   notice, this list of conditions and the following disclaimer in the
33
   documentation and/or other materials provided with the distribution.
34
35
   - Neither the name of the Xiph.org Foundation nor the names of its
36
   contributors may be used to endorse or promote products derived from
37
   this software without specific prior written permission.
38
39
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
40
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
41
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
42
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
43
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
46
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
47
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
48
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
49
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50
*/
51
52
#ifdef HAVE_CONFIG_H
53
#include "config.h"
54
#endif
55
56
#ifndef DISABLE_ENCODER
57
58
#include "lpc.h"
59
60
#ifdef BFIN_ASM
61
#include "lpc_bfin.h"
62
#endif
63
64
/* LPC analysis
65
 *
66
 * The next two functions calculate linear prediction coefficients
67
 * and/or the related reflection coefficients from the first P_MAX+1
68
 * values of the autocorrelation function.
69
 */
70
71
/* Invented by N. Levinson in 1947, modified by J. Durbin in 1959.
72
 */
73
74
/* returns minimum mean square error    */
75
spx_word32_t _spx_lpc(
76
spx_coef_t       *lpc, /* out: [0...p-1] LPC coefficients      */
77
const spx_word16_t *ac,  /* in:  [0...p] autocorrelation values  */
78
int          p
79
)
80
62.8k
{
81
62.8k
   int i, j;
82
62.8k
   spx_word16_t r;
83
62.8k
   spx_word16_t error = ac[0];
84
85
656k
   for (i = 0; i < p; i++) {
86
87
      /* Sum up this iteration's reflection coefficient */
88
594k
      spx_word32_t rr = NEG32(SHL32(EXTEND32(ac[i + 1]),13));
89
3.13M
      for (j = 0; j < i; j++)
90
2.53M
         rr = SUB32(rr,MULT16_16(lpc[j],ac[i - j]));
91
#ifdef FIXED_POINT
92
297k
      r = DIV32_16(rr+PSHR32(error,1),ADD16(error,8));
93
#else
94
      r = rr/(error+.003*ac[0]);
95
#endif
96
      /*  Update LPC coefficients and total error */
97
594k
      lpc[i] = r;
98
2.01M
      for (j = 0; j < (i+1)>>1; j++)
99
1.41M
      {
100
1.41M
         spx_word16_t tmp1, tmp2;
101
         /* It could be that j == i-1-j, in which case, we're updating the same value twice, which is OK */
102
1.41M
         tmp1 = lpc[j];
103
1.41M
         tmp2 = lpc[i-1-j];
104
1.41M
         lpc[j]     = MAC16_16_P13(tmp1,r,tmp2);
105
1.41M
         lpc[i-1-j] = MAC16_16_P13(tmp2,r,tmp1);
106
1.41M
      }
107
108
594k
      error = SUB16(error,MULT16_16_Q13(r,MULT16_16_Q13(error,r)));
109
594k
   }
110
62.8k
   return error;
111
62.8k
}
_spx_lpc
Line
Count
Source
80
31.4k
{
81
31.4k
   int i, j;
82
31.4k
   spx_word16_t r;
83
31.4k
   spx_word16_t error = ac[0];
84
85
328k
   for (i = 0; i < p; i++) {
86
87
      /* Sum up this iteration's reflection coefficient */
88
297k
      spx_word32_t rr = NEG32(SHL32(EXTEND32(ac[i + 1]),13));
89
1.56M
      for (j = 0; j < i; j++)
90
1.26M
         rr = SUB32(rr,MULT16_16(lpc[j],ac[i - j]));
91
#ifdef FIXED_POINT
92
      r = DIV32_16(rr+PSHR32(error,1),ADD16(error,8));
93
#else
94
297k
      r = rr/(error+.003*ac[0]);
95
297k
#endif
96
      /*  Update LPC coefficients and total error */
97
297k
      lpc[i] = r;
98
1.00M
      for (j = 0; j < (i+1)>>1; j++)
99
708k
      {
100
708k
         spx_word16_t tmp1, tmp2;
101
         /* It could be that j == i-1-j, in which case, we're updating the same value twice, which is OK */
102
708k
         tmp1 = lpc[j];
103
708k
         tmp2 = lpc[i-1-j];
104
708k
         lpc[j]     = MAC16_16_P13(tmp1,r,tmp2);
105
708k
         lpc[i-1-j] = MAC16_16_P13(tmp2,r,tmp1);
106
708k
      }
107
108
297k
      error = SUB16(error,MULT16_16_Q13(r,MULT16_16_Q13(error,r)));
109
297k
   }
110
31.4k
   return error;
111
31.4k
}
_spx_lpc
Line
Count
Source
80
31.4k
{
81
31.4k
   int i, j;
82
31.4k
   spx_word16_t r;
83
31.4k
   spx_word16_t error = ac[0];
84
85
328k
   for (i = 0; i < p; i++) {
86
87
      /* Sum up this iteration's reflection coefficient */
88
297k
      spx_word32_t rr = NEG32(SHL32(EXTEND32(ac[i + 1]),13));
89
1.56M
      for (j = 0; j < i; j++)
90
1.26M
         rr = SUB32(rr,MULT16_16(lpc[j],ac[i - j]));
91
297k
#ifdef FIXED_POINT
92
297k
      r = DIV32_16(rr+PSHR32(error,1),ADD16(error,8));
93
#else
94
      r = rr/(error+.003*ac[0]);
95
#endif
96
      /*  Update LPC coefficients and total error */
97
297k
      lpc[i] = r;
98
1.00M
      for (j = 0; j < (i+1)>>1; j++)
99
708k
      {
100
708k
         spx_word16_t tmp1, tmp2;
101
         /* It could be that j == i-1-j, in which case, we're updating the same value twice, which is OK */
102
708k
         tmp1 = lpc[j];
103
708k
         tmp2 = lpc[i-1-j];
104
708k
         lpc[j]     = MAC16_16_P13(tmp1,r,tmp2);
105
708k
         lpc[i-1-j] = MAC16_16_P13(tmp2,r,tmp1);
106
708k
      }
107
108
297k
      error = SUB16(error,MULT16_16_Q13(r,MULT16_16_Q13(error,r)));
109
297k
   }
110
31.4k
   return error;
111
31.4k
}
112
113
114
#ifdef FIXED_POINT
115
116
/* Compute the autocorrelation
117
 *                      ,--,
118
 *              ac(i) = >  x(n) * x(n-i)  for all n
119
 *                      `--'
120
 * for lags between 0 and lag-1, and x == 0 outside 0...n-1
121
 */
122
123
#ifndef OVERRIDE_SPEEX_AUTOCORR
124
void _spx_autocorr(
125
const spx_word16_t *x,   /*  in: [0...n-1] samples x   */
126
spx_word16_t       *ac,  /* out: [0...lag-1] ac values */
127
int          lag,
128
int          n
129
)
130
19.9k
{
131
19.9k
   spx_word32_t d;
132
19.9k
   int i, j;
133
19.9k
   spx_word32_t ac0=1;
134
19.9k
   int shift, ac_shift;
135
136
4.19M
   for (j=0;j<n;j++)
137
4.17M
      ac0 = ADD32(ac0,SHR32(MULT16_16(x[j],x[j]),8));
138
19.9k
   ac0 = ADD32(ac0,n);
139
19.9k
   shift = 8;
140
163k
   while (shift && ac0<0x40000000)
141
143k
   {
142
143k
      shift--;
143
143k
      ac0 <<= 1;
144
143k
   }
145
19.9k
   ac_shift = 18;
146
177k
   while (ac_shift && ac0<0x40000000)
147
157k
   {
148
157k
      ac_shift--;
149
157k
      ac0 <<= 1;
150
157k
   }
151
152
153
229k
   for (i=0;i<lag;i++)
154
209k
   {
155
209k
      d=0;
156
42.7M
      for (j=i;j<n;j++)
157
42.5M
      {
158
42.5M
         d = ADD32(d,SHR32(MULT16_16(x[j],x[j-i]), shift));
159
42.5M
      }
160
161
209k
      ac[i] = SHR32(d, ac_shift);
162
209k
   }
163
19.9k
}
164
#endif
165
166
167
#else
168
169
170
171
/* Compute the autocorrelation
172
 *                      ,--,
173
 *              ac(i) = >  x(n) * x(n-i)  for all n
174
 *                      `--'
175
 * for lags between 0 and lag-1, and x == 0 outside 0...n-1
176
 */
177
void _spx_autocorr(
178
const spx_word16_t *x,   /*  in: [0...n-1] samples x   */
179
float       *ac,  /* out: [0...lag-1] ac values */
180
int          lag,
181
int          n
182
)
183
11.4k
{
184
11.4k
   float d;
185
11.4k
   int i;
186
130k
   while (lag--)
187
119k
   {
188
24.8M
      for (i = lag, d = 0; i < n; i++)
189
24.7M
         d += x[i] * x[i-lag];
190
119k
      ac[lag] = d;
191
119k
   }
192
11.4k
   ac[0] += 10;
193
11.4k
}
194
195
#endif
196
197
198
#endif /* DISABLE_ENCODER */