blob: 47770a1d7c00f4124e30419f2e49bb4616723f64 [file] [log] [blame]
Seppo Ingalsuo29566ee2017-06-07 14:17:56 +03001/*
2 * Copyright (c) 2016, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Intel Corporation nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
29 * Liam Girdwood <liam.r.girdwood@linux.intel.com>
30 * Keyon Jie <yang.jie@linux.intel.com>
31 */
32
33#include <stdint.h>
Pierre-Louis Bossart81708a52018-04-04 18:46:50 -050034#include <sof/audio/format.h>
35#include <sof/math/trig.h>
Seppo Ingalsuo29566ee2017-06-07 14:17:56 +030036
37
38#define SINE_C_Q20 341782638 /* 2*SINE_NQUART/pi in Q12.20 */
39#define SINE_NQUART 512 /* Must be 2^N */
40#define SINE_TABLE_SIZE (SINE_NQUART+1)
41
42/* An 1/4 period of sine wave as Q1.31 */
43const int32_t sine_table[SINE_TABLE_SIZE] = {
44 0,
45 6588387,
46 13176712,
47 19764913,
48 26352928,
49 32940695,
50 39528151,
51 46115236,
52 52701887,
53 59288042,
54 65873638,
55 72458615,
56 79042909,
57 85626460,
58 92209205,
59 98791081,
60 105372028,
61 111951983,
62 118530885,
63 125108670,
64 131685278,
65 138260647,
66 144834714,
67 151407418,
68 157978697,
69 164548489,
70 171116732,
71 177683365,
72 184248325,
73 190811551,
74 197372981,
75 203932553,
76 210490206,
77 217045877,
78 223599506,
79 230151030,
80 236700388,
81 243247517,
82 249792358,
83 256334847,
84 262874923,
85 269412525,
86 275947592,
87 282480061,
88 289009871,
89 295536961,
90 302061269,
91 308582734,
92 315101294,
93 321616889,
94 328129457,
95 334638936,
96 341145265,
97 347648383,
98 354148229,
99 360644742,
100 367137860,
101 373627523,
102 380113669,
103 386596237,
104 393075166,
105 399550396,
106 406021864,
107 412489512,
108 418953276,
109 425413098,
110 431868915,
111 438320667,
112 444768293,
113 451211734,
114 457650927,
115 464085813,
116 470516330,
117 476942419,
118 483364019,
119 489781069,
120 496193509,
121 502601279,
122 509004318,
123 515402566,
124 521795963,
125 528184448,
126 534567963,
127 540946445,
128 547319836,
129 553688076,
130 560051103,
131 566408860,
132 572761285,
133 579108319,
134 585449903,
135 591785976,
136 598116478,
137 604441351,
138 610760535,
139 617073970,
140 623381597,
141 629683357,
142 635979190,
143 642269036,
144 648552837,
145 654830534,
146 661102068,
147 667367379,
148 673626408,
149 679879097,
150 686125386,
151 692365218,
152 698598533,
153 704825272,
154 711045377,
155 717258790,
156 723465451,
157 729665303,
158 735858287,
159 742044345,
160 748223418,
161 754395449,
162 760560379,
163 766718151,
164 772868706,
165 779011986,
166 785147934,
167 791276492,
168 797397602,
169 803511207,
170 809617248,
171 815715670,
172 821806413,
173 827889421,
174 833964637,
175 840032003,
176 846091463,
177 852142959,
178 858186434,
179 864221832,
180 870249095,
181 876268167,
182 882278991,
183 888281511,
184 894275670,
185 900261412,
186 906238681,
187 912207419,
188 918167571,
189 924119082,
190 930061894,
191 935995952,
192 941921200,
193 947837582,
194 953745043,
195 959643527,
196 965532978,
197 971413341,
198 977284561,
199 983146583,
200 988999351,
201 994842809,
202 1000676905,
203 1006501581,
204 1012316784,
205 1018122458,
206 1023918549,
207 1029705003,
208 1035481765,
209 1041248781,
210 1047005996,
211 1052753356,
212 1058490807,
213 1064218296,
214 1069935767,
215 1075643168,
216 1081340445,
217 1087027543,
218 1092704410,
219 1098370992,
220 1104027236,
221 1109673088,
222 1115308496,
223 1120933406,
224 1126547765,
225 1132151521,
226 1137744620,
227 1143327011,
228 1148898640,
229 1154459455,
230 1160009404,
231 1165548435,
232 1171076495,
233 1176593532,
234 1182099495,
235 1187594332,
236 1193077990,
237 1198550419,
238 1204011566,
239 1209461381,
240 1214899812,
241 1220326808,
242 1225742318,
243 1231146290,
244 1236538675,
245 1241919421,
246 1247288477,
247 1252645793,
248 1257991319,
249 1263325005,
250 1268646799,
251 1273956652,
252 1279254515,
253 1284540337,
254 1289814068,
255 1295075658,
256 1300325059,
257 1305562221,
258 1310787095,
259 1315999631,
260 1321199780,
261 1326387493,
262 1331562722,
263 1336725418,
264 1341875532,
265 1347013016,
266 1352137822,
267 1357249900,
268 1362349204,
269 1367435684,
270 1372509294,
271 1377569985,
272 1382617710,
273 1387652421,
274 1392674071,
275 1397682613,
276 1402677999,
277 1407660183,
278 1412629117,
279 1417584755,
280 1422527050,
281 1427455956,
282 1432371426,
283 1437273414,
284 1442161874,
285 1447036759,
286 1451898025,
287 1456745625,
288 1461579513,
289 1466399644,
290 1471205973,
291 1475998455,
292 1480777044,
293 1485541695,
294 1490292364,
295 1495029005,
296 1499751575,
297 1504460029,
298 1509154322,
299 1513834410,
300 1518500249,
301 1523151796,
302 1527789006,
303 1532411836,
304 1537020243,
305 1541614182,
306 1546193612,
307 1550758488,
308 1555308767,
309 1559844407,
310 1564365366,
311 1568871600,
312 1573363067,
313 1577839726,
314 1582301533,
315 1586748446,
316 1591180425,
317 1595597427,
318 1599999410,
319 1604386334,
320 1608758157,
321 1613114837,
322 1617456334,
323 1621782607,
324 1626093615,
325 1630389318,
326 1634669675,
327 1638934646,
328 1643184190,
329 1647418268,
330 1651636840,
331 1655839867,
332 1660027308,
333 1664199124,
334 1668355276,
335 1672495724,
336 1676620431,
337 1680729357,
338 1684822463,
339 1688899710,
340 1692961061,
341 1697006478,
342 1701035921,
343 1705049354,
344 1709046738,
345 1713028036,
346 1716993211,
347 1720942224,
348 1724875039,
349 1728791619,
350 1732691927,
351 1736575926,
352 1740443580,
353 1744294852,
354 1748129706,
355 1751948106,
356 1755750016,
357 1759535401,
358 1763304223,
359 1767056449,
360 1770792043,
361 1774510970,
362 1778213194,
363 1781898680,
364 1785567395,
365 1789219304,
366 1792854372,
367 1796472564,
368 1800073848,
369 1803658188,
370 1807225552,
371 1810775906,
372 1814309215,
373 1817825448,
374 1821324571,
375 1824806551,
376 1828271355,
377 1831718951,
378 1835149305,
379 1838562387,
380 1841958164,
381 1845336603,
382 1848697673,
383 1852041343,
384 1855367580,
385 1858676354,
386 1861967633,
387 1865241387,
388 1868497585,
389 1871736195,
390 1874957188,
391 1878160534,
392 1881346201,
393 1884514160,
394 1887664382,
395 1890796836,
396 1893911493,
397 1897008324,
398 1900087300,
399 1903148391,
400 1906191569,
401 1909216806,
402 1912224072,
403 1915213339,
404 1918184580,
405 1921137766,
406 1924072870,
407 1926989863,
408 1929888719,
409 1932769410,
410 1935631909,
411 1938476189,
412 1941302224,
413 1944109986,
414 1946899450,
415 1949670588,
416 1952423376,
417 1955157787,
418 1957873795,
419 1960571374,
420 1963250500,
421 1965911147,
422 1968553291,
423 1971176905,
424 1973781966,
425 1976368449,
426 1978936330,
427 1981485584,
428 1984016188,
429 1986528117,
430 1989021349,
431 1991495859,
432 1993951624,
433 1996388621,
434 1998806828,
435 2001206221,
436 2003586778,
437 2005948477,
438 2008291295,
439 2010615209,
440 2012920200,
441 2015206244,
442 2017473320,
443 2019721407,
444 2021950483,
445 2024160528,
446 2026351521,
447 2028523441,
448 2030676268,
449 2032809981,
450 2034924561,
451 2037019987,
452 2039096240,
453 2041153301,
454 2043191149,
455 2045209766,
456 2047209132,
457 2049189230,
458 2051150040,
459 2053091543,
460 2055013722,
461 2056916559,
462 2058800035,
463 2060664132,
464 2062508835,
465 2064334123,
466 2066139982,
467 2067926393,
468 2069693341,
469 2071440807,
470 2073168776,
471 2074877232,
472 2076566159,
473 2078235539,
474 2079885359,
475 2081515602,
476 2083126253,
477 2084717297,
478 2086288719,
479 2087840504,
480 2089372637,
481 2090885104,
482 2092377891,
483 2093850984,
484 2095304369,
485 2096738031,
486 2098151959,
487 2099546138,
488 2100920555,
489 2102275198,
490 2103610053,
491 2104925108,
492 2106220351,
493 2107495769,
494 2108751351,
495 2109987084,
496 2111202958,
497 2112398959,
498 2113575079,
499 2114731304,
500 2115867625,
501 2116984030,
502 2118080510,
503 2119157053,
504 2120213650,
505 2121250291,
506 2122266966,
507 2123263665,
508 2124240379,
509 2125197099,
510 2126133816,
511 2127050521,
512 2127947205,
513 2128823861,
514 2129680479,
515 2130517051,
516 2131333571,
517 2132130029,
518 2132906419,
519 2133662733,
520 2134398965,
521 2135115106,
522 2135811152,
523 2136487094,
524 2137142926,
525 2137778643,
526 2138394239,
527 2138989707,
528 2139565042,
529 2140120239,
530 2140655292,
531 2141170196,
532 2141664947,
533 2142139540,
534 2142593970,
535 2143028233,
536 2143442325,
537 2143836243,
538 2144209981,
539 2144563538,
540 2144896909,
541 2145210091,
542 2145503082,
543 2145775879,
544 2146028479,
545 2146260880,
546 2146473079,
547 2146665075,
548 2146836865,
549 2146988449,
550 2147119824,
551 2147230990,
552 2147321945,
553 2147392689,
554 2147443221,
555 2147473541,
556 2147483647
557};
558
559/* Sine lookup table read */
560static inline int32_t sine_lookup(int idx) {
561 int32_t s;
562 int i1;
563
564 i1 = idx & (2 * SINE_NQUART - 1);
565 if (i1 > SINE_NQUART)
566 i1 = 2 * SINE_NQUART - i1;
567
568 if (idx > 2 * SINE_NQUART)
569 s = -sine_table[i1];
570 else
571 s = sine_table[i1];
572
573 return (s);
574}
575
576/* Compute fixed point sine with table lookup and interpolation */
577int32_t sin_fixed(int32_t w) {
578 int idx;
Pierre-Louis Bossart4ccf81d2017-09-25 14:52:09 -0500579 int32_t frac;
580 int32_t s0;
581 int32_t s1;
582 int32_t delta;
583 int64_t sine;
584 int64_t idx_tmp;
Seppo Ingalsuo29566ee2017-06-07 14:17:56 +0300585
586 /* Q4.28 x Q12.20 -> Q16.48 */
587 idx_tmp = (int64_t) w * SINE_C_Q20;
588 idx = (int) (idx_tmp >> 48); /* Shift to Q0 */
589 idx_tmp = idx_tmp >> 17; /* Shift to Q16.31 */
590 idx_tmp = idx_tmp - (idx << 31); /* Get fraction */
591 frac = (int32_t) idx_tmp; /* Q1.31 */
592 s0 = sine_lookup(idx); /* Q1.31 */
593 s1 = sine_lookup(idx + 1); /* Q1.31 */
594 delta = s1 - s0; /* Q1.31 */
595 //sine = (int64_t) frac*delta; /* Q1.31 x Q1.31 -> Q2.62 */
596 //sine = (sine >> 31) + s0; /* Q2.31 */
Ranjani Sridharan034b06f2017-10-20 08:42:00 +0100597 /* All Q1.31 */
598 sine = s0 + q_mults_32x32(frac, delta, Q_SHIFT_BITS_64(31, 31, 31));
Seppo Ingalsuo29566ee2017-06-07 14:17:56 +0300599 return (int32_t) sine;
600}