1 /*
2  * Copyright (C) 2004-2010 NXP Software
3  * Copyright (C) 2010 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /************************************************************************************/
19 /*                                                                                  */
20 /*  Includes                                                                        */
21 /*                                                                                  */
22 /************************************************************************************/
23 
24 #include "LVPSA.h"
25 #include "LVPSA_QPD.h"
26 #include "LVPSA_Tables.h"
27 /************************************************************************************/
28 /*                                                                                  */
29 /*  Sample rate table                                                               */
30 /*                                                                                  */
31 /************************************************************************************/
32 
33 /*
34  * Sample rate table for converting between the enumerated type and the actual
35  * frequency
36  */
37 const LVM_UINT32    LVPSA_SampleRateTab[] = {   8000,                    /* 8kS/s  */
38                                                 11025,
39                                                 12000,
40                                                 16000,
41                                                 22050,
42                                                 24000,
43                                                 32000,
44                                                 44100,
45                                                 48000,
46                                                 88200,
47                                                 96000,
48                                                176400,
49                                                192000};                  /* 192kS/s */
50 
51 /************************************************************************************/
52 /*                                                                                  */
53 /*  Sample rate inverse table                                                       */
54 /*                                                                                  */
55 /************************************************************************************/
56 
57 /*
58  * Sample rate table for converting between the enumerated type and the actual
59  * frequency
60  */
61 const LVM_UINT32    LVPSA_SampleRateInvTab[] = {    268435,                    /* 8kS/s  */
62                                                     194783,
63                                                     178957,
64                                                     134218,
65                                                     97391,
66                                                     89478,
67                                                     67109,
68                                                     48696,
69                                                     44739
70                                                     ,24348
71                                                     ,22369
72                                                     ,12174
73                                                     ,11185                  /* 192kS/s */
74                                                };
75 
76 /************************************************************************************/
77 /*                                                                                  */
78 /*  Number of samples in 20ms                                                       */
79 /*                                                                                  */
80 /************************************************************************************/
81 
82 /*
83  * Table for converting between the enumerated type and the number of samples
84  * during 20ms
85  */
86 const LVM_UINT16    LVPSA_nSamplesBufferUpdate[]  = {   160,                   /* 8kS/s  */
87                                                         220,
88                                                         240,
89                                                         320,
90                                                         441,
91                                                         480,
92                                                         640,
93                                                         882,
94                                                         960
95                                                         ,1764
96                                                         ,1920
97                                                         ,3528
98                                                         ,3840                  /* 192kS/s */
99                                                     };
100 /************************************************************************************/
101 /*                                                                                  */
102 /*  Down sampling factors                                                           */
103 /*                                                                                  */
104 /************************************************************************************/
105 
106 /*
107  * Table for converting between the enumerated type and the down sampling factor
108  */
109 const LVM_UINT16    LVPSA_DownSamplingFactor[]  = {     5,                    /* 8000  S/s  */
110                                                         7,                    /* 11025 S/s  */
111                                                         8,                    /* 12000 S/s  */
112                                                         10,                   /* 16000 S/s  */
113                                                         15,                   /* 22050 S/s  */
114                                                         16,                   /* 24000 S/s  */
115                                                         21,                   /* 32000 S/s  */
116                                                         30,                   /* 44100 S/s  */
117                                                         32                    /* 48000 S/s  */
118                                                        ,60                   /* 88200 S/s  */
119                                                        ,64                   /* 96000 S/s  */
120                                                        ,120                  /* 176400 S/s  */
121                                                        ,128                  /*192000 S/s  */
122                                                   };
123 
124 /************************************************************************************/
125 /*                                                                                  */
126 /*  Coefficient calculation tables                                                  */
127 /*                                                                                  */
128 /************************************************************************************/
129 
130 /*
131  * Table for 2 * Pi / Fs
132  */
133 const LVM_INT16     LVPSA_TwoPiOnFsTable[] = {  26354,      /* 8kS/s */
134                                                 19123,
135                                                 17569,
136                                                 13177,
137                                                  9561,
138                                                  8785,
139                                                  6588,
140                                                  4781,
141                                                  4392
142                                                 ,2390
143                                                 ,2196
144                                                 ,1195
145                                                 ,1098    /* 192kS/s */
146                                              };
147 
148 const LVM_FLOAT     LVPSA_Float_TwoPiOnFsTable[] = {  0.8042847f,      /* 8kS/s */
149                                                       0.5836054f,
150                                                       0.5361796f,
151                                                       0.4021423f,
152                                                       0.2917874f,
153                                                       0.2681051f,
154                                                       0.2010559f,
155                                                       0.1459089f,
156                                                       0.1340372f
157                                                      ,0.0729476f
158                                                      ,0.0670186f
159                                                      ,0.0364738f
160                                                      ,0.0335093f    /* 192kS/s */
161                                                    };
162 
163 /*
164  * Gain table
165  */
166 const LVM_INT16     LVPSA_GainTable[] = {   364,          /* -15dB gain */
167                                             408,
168                                             458,
169                                             514,
170                                             577,
171                                             647,
172                                             726,
173                                             815,
174                                             914,
175                                             1026,
176                                             1151,
177                                             1292,
178                                             1449,
179                                             1626,
180                                             1825,
181                                             2048,         /* 0dB gain */
182                                             2297,
183                                             2578,
184                                             2892,
185                                             3245,
186                                             3641,
187                                             4096,
188                                             4584,
189                                             5144,
190                                             5772,
191                                             6476,
192                                             7266,
193                                             8153,
194                                             9148,
195                                             10264,
196                                             11576};        /* +15dB gain */
197 
198 const LVM_FLOAT  LVPSA_Float_GainTable[]={  0.177734375f,          /* -15dB gain */
199                                             0.199218750f,
200                                             0.223632812f,
201                                             0.250976562f,
202                                             0.281738281f,
203                                             0.315917968f,
204                                             0.354492187f,
205                                             0.397949218f,
206                                             0.446289062f,
207                                             0.500976562f,
208                                             0.562011718f,
209                                             0.630859375f,
210                                             0.707519531f,
211                                             0.793945312f,
212                                             0.891113281f,
213                                             1.000000000f,         /* 0dB gain */
214                                             1.121582031f,
215                                             1.258789062f,
216                                             1.412109375f,
217                                             1.584472656f,
218                                             1.777832031f,
219                                             2.000000000f,
220                                             2.238281250f,
221                                             2.511718750f,
222                                             2.818359375f,
223                                             3.162109375f,
224                                             3.547851562f,
225                                             3.980957031f,
226                                             4.466796875f,
227                                             5.011718750f,
228                                             5.652343750f};        /* +15dB gain */
229 /************************************************************************************/
230 /*                                                                                  */
231 /*  Cosone polynomial coefficients                                                  */
232 /*                                                                                  */
233 /************************************************************************************/
234 
235 /*
236  * Coefficients for calculating the cosine with the equation:
237  *
238  *  Cos(x) = (2^Shifts)*(a0 + a1*x + a2*x^2 + a3*x^3 + a4*x^4 + a5*x^5)
239  *
240  * These coefficients expect the input, x, to be in the range 0 to 32768 respresenting
241  * a range of 0 to Pi. The output is in the range 32767 to -32768 representing the range
242  * +1.0 to -1.0
243  */
244 const LVM_INT16     LVPSA_CosCoef[] = { 3,                             /* Shifts */
245                                         4096,                          /* a0 */
246                                         -36,                           /* a1 */
247                                         -19725,                        /* a2 */
248                                         -2671,                         /* a3 */
249                                         23730,                         /* a4 */
250                                         -9490};                        /* a5 */
251 const LVM_FLOAT     LVPSA_Float_CosCoef[] = { 3,                             /* Shifts */
252                                               0.1250038f,                          /* a0 */
253                                               -0.0010986f,                           /* a1 */
254                                               -0.6019775f,                        /* a2 */
255                                               -0.0815149f,                         /* a3 */
256                                               0.7242042f,                         /* a4 */
257                                               -0.2896206f};                        /* a5 */
258 /*
259  * Coefficients for calculating the cosine error with the equation:
260  *
261  *  CosErr(x) = (2^Shifts)*(a0 + a1*x + a2*x^2 + a3*x^3)
262  *
263  * These coefficients expect the input, x, to be in the range 0 to 32768 respresenting
264  * a range of 0 to Pi/25. The output is in the range 0 to 32767 representing the range
265  * 0.0 to 0.0078852986
266  *
267  * This is used to give a double precision cosine over the range 0 to Pi/25 using the
268  * the equation:
269  *
270  * Cos(x) = 1.0 - CosErr(x)
271  */
272 const LVM_INT16     LVPSA_DPCosCoef[] = {   1,                           /* Shifts */
273                                             0,                           /* a0 */
274                                             -6,                          /* a1 */
275                                             16586,                       /* a2 */
276                                             -44};                        /* a3 */
277 const LVM_FLOAT    LVPSA_Float_DPCosCoef[] = {1.0f,                        /* Shifts */
278                                               0.0f,                        /* a0 */
279                                               -0.00008311f,                 /* a1 */
280                                               0.50617999f,                 /* a2 */
281                                               -0.00134281f};                /* a3 */
282 /************************************************************************************/
283 /*                                                                                  */
284 /*  Quasi peak filter coefficients table                                            */
285 /*                                                                                  */
286 /************************************************************************************/
287 const QPD_C32_Coefs     LVPSA_QPD_Coefs[] = {
288                                          /* 8kS/s  */    /* LVPSA_SPEED_LOW   */
289                                          {(LVM_INT32)0x80CEFD2B,0x00CB9B17},
290                                          {(LVM_INT32)0x80D242E7,0x00CED11D},
291                                          {(LVM_INT32)0x80DCBAF5,0x00D91679},
292                                          {(LVM_INT32)0x80CEFD2B,0x00CB9B17},
293                                          {(LVM_INT32)0x80E13739,0x00DD7CD3},
294                                          {(LVM_INT32)0x80DCBAF5,0x00D91679},
295                                          {(LVM_INT32)0x80D94BAF,0x00D5B7E7},
296                                          {(LVM_INT32)0x80E13739,0x00DD7CD3},
297                                          {(LVM_INT32)0x80DCBAF5,0x00D91679},  /* 48kS/s */
298 
299                                          /* 8kS/s  */    /* LVPSA_SPEED_MEDIUM */
300                                          {(LVM_INT32)0x8587513D,0x055C22CF},
301                                          {(LVM_INT32)0x859D2967,0x0570F007},
302                                          {(LVM_INT32)0x85E2EFAC,0x05B34D79},
303                                          {(LVM_INT32)0x8587513D,0x055C22CF},
304                                          {(LVM_INT32)0x8600C7B9,0x05CFA6CF},
305                                          {(LVM_INT32)0x85E2EFAC,0x05B34D79},
306                                          {(LVM_INT32)0x85CC1018,0x059D8F69},
307                                          {(LVM_INT32)0x8600C7B9,0x05CFA6CF},
308                                          {(LVM_INT32)0x85E2EFAC,0x05B34D79},  /* 48kS/s */
309 
310                                          /* 8kS/s  */   /* LVPSA_SPEED_HIGH    */
311                                          {(LVM_INT32)0xA115EA7A,0x1CDB3F5C},
312                                          {(LVM_INT32)0xA18475F0,0x1D2C83A2},
313                                          {(LVM_INT32)0xA2E1E950,0x1E2A532E},
314                                          {(LVM_INT32)0xA115EA7A,0x1CDB3F5C},
315                                          {(LVM_INT32)0xA375B2C6,0x1E943BBC},
316                                          {(LVM_INT32)0xA2E1E950,0x1E2A532E},
317                                          {(LVM_INT32)0xA26FF6BD,0x1DD81530},
318                                          {(LVM_INT32)0xA375B2C6,0x1E943BBC},
319                                          {(LVM_INT32)0xA2E1E950,0x1E2A532E}}; /* 48kS/s */
320 
321 const QPD_FLOAT_Coefs     LVPSA_QPD_Float_Coefs[] = {
322 
323                                          /* 8kS/s  */    /* LVPSA_SPEED_LOW   */
324                                          {-0.9936831989325583f,0.0062135565094650f},
325                                          {-0.9935833332128823f,0.0063115493394434f},
326                                          {-0.9932638457976282f,0.0066249934025109f},
327                                          {-0.9936831989325583f,0.0062135565094650f},
328                                          {-0.9931269618682563f,0.0067592649720609f},
329                                          {-0.9932638457976282f,0.0066249934025109f},
330                                          {-0.9933686633594334f,0.0065221670083702f},
331                                          {-0.9931269618682563f,0.0067592649720609f},
332                                           /* 48kS/s */
333                                          {-0.9932638457976282f,0.0066249934025109f},
334                                          {-0.9931269618682563f,0.0067592649720609f},
335                                          {-0.9932638457976282f,0.0066249934025109f},
336                                          {-0.9931269618682563f,0.0067592649720609f},
337                                          {-0.9932638457976282f,0.0066249934025109f},
338                                          /* 8kS/s  */    /* LVPSA_SPEED_MEDIUM      */
339                                          {-0.9568079425953329f,0.0418742666952312f},
340                                          {-0.9561413046903908f,0.0425090822391212f},
341                                          {-0.9540119562298059f,0.0445343819446862f},
342                                          {-0.9568079425953329f,0.0418742666952312f},
343                                          {-0.9531011912040412f,0.0453995238058269f},
344                                          {-0.9540119562298059f,0.0445343819446862f},
345                                          {-0.9547099955379963f,0.0438708555884659f},
346                                           //{0x8600C7B9,0x05CFA6CF},
347                                          {-0.9531011912040412f,0.0453995238058269f},
348                                           /* 48kS/s */
349                                          {-0.9540119562298059f,0.0445343819446862f},
350                                          {-0.9531011912040412f,0.0453995238058269f},
351                                          {-0.9540119562298059f,0.0445343819446862f},
352                                          {-0.9531011912040412f,0.0453995238058269f},
353                                          {-0.9540119562298059f,0.0445343819446862f},
354                                           /* 8kS/s  */   /* LVPSA_SPEED_HIGH      */
355                                          {-0.7415186790749431f,0.2254409026354551f},
356                                          {-0.7381451204419136f,0.2279209652915597f},
357                                          {-0.7274807319045067f,0.2356666540727019f},
358                                          {-0.7415186790749431f,0.2254409026354551f},
359                                          {-0.7229706319049001f,0.2388987224549055f},
360                                          {-0.7274807319045067f,0.2356666540727019f},
361                                          {-0.7309581353329122f,0.2331568226218224f},
362                                          {-0.7229706319049001f,0.2388987224549055f},
363                                            /* 48kS/s */
364                                          {-0.7274807319045067f,0.2356666540727019f}
365                                         ,{-0.7229706319049001f,0.2388987224549055f}
366                                         ,{-0.7274807319045067f,0.2356666540727019f}
367                                         ,{-0.7229706319049001f,0.2388987224549055f}
368                                         ,{-0.7274807319045067f,0.2356666540727019f}
369                                         };
370