summaryrefslogtreecommitdiff
path: root/apps/codecs/adx.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/adx.c')
-rw-r--r--apps/codecs/adx.c123
1 files changed, 3 insertions, 120 deletions
diff --git a/apps/codecs/adx.c b/apps/codecs/adx.c
index cc36f6a908..0e50054753 100644
--- a/apps/codecs/adx.c
+++ b/apps/codecs/adx.c
@@ -21,6 +21,7 @@
21#include "codeclib.h" 21#include "codeclib.h"
22#include "inttypes.h" 22#include "inttypes.h"
23#include "math.h" 23#include "math.h"
24#include "lib/fixedpoint.h"
24 25
25CODEC_HEADER 26CODEC_HEADER
26 27
@@ -41,124 +42,6 @@ const long cutoff = 500;
41 42
42static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR; 43static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR;
43 44
44/* fixed point stuff from apps/plugins/lib/fixedpoint.c */
45
46/* Inverse gain of circular cordic rotation in s0.31 format. */
47static const long cordic_circular_gain = 0xb2458939; /* 0.607252929 */
48
49/* Table of values of atan(2^-i) in 0.32 format fractions of pi where pi = 0xffffffff / 2 */
50static const unsigned long atan_table[] = {
51 0x1fffffff, /* +0.785398163 (or pi/4) */
52 0x12e4051d, /* +0.463647609 */
53 0x09fb385b, /* +0.244978663 */
54 0x051111d4, /* +0.124354995 */
55 0x028b0d43, /* +0.062418810 */
56 0x0145d7e1, /* +0.031239833 */
57 0x00a2f61e, /* +0.015623729 */
58 0x00517c55, /* +0.007812341 */
59 0x0028be53, /* +0.003906230 */
60 0x00145f2e, /* +0.001953123 */
61 0x000a2f98, /* +0.000976562 */
62 0x000517cc, /* +0.000488281 */
63 0x00028be6, /* +0.000244141 */
64 0x000145f3, /* +0.000122070 */
65 0x0000a2f9, /* +0.000061035 */
66 0x0000517c, /* +0.000030518 */
67 0x000028be, /* +0.000015259 */
68 0x0000145f, /* +0.000007629 */
69 0x00000a2f, /* +0.000003815 */
70 0x00000517, /* +0.000001907 */
71 0x0000028b, /* +0.000000954 */
72 0x00000145, /* +0.000000477 */
73 0x000000a2, /* +0.000000238 */
74 0x00000051, /* +0.000000119 */
75 0x00000028, /* +0.000000060 */
76 0x00000014, /* +0.000000030 */
77 0x0000000a, /* +0.000000015 */
78 0x00000005, /* +0.000000007 */
79 0x00000002, /* +0.000000004 */
80 0x00000001, /* +0.000000002 */
81 0x00000000, /* +0.000000001 */
82 0x00000000, /* +0.000000000 */
83};
84
85/**
86 * Implements sin and cos using CORDIC rotation.
87 *
88 * @param phase has range from 0 to 0xffffffff, representing 0 and
89 * 2*pi respectively.
90 * @param cos return address for cos
91 * @return sin of phase, value is a signed value from LONG_MIN to LONG_MAX,
92 * representing -1 and 1 respectively.
93 */
94static long fsincos(unsigned long phase, long *cos)
95{
96 int32_t x, x1, y, y1;
97 unsigned long z, z1;
98 int i;
99
100 /* Setup initial vector */
101 x = cordic_circular_gain;
102 y = 0;
103 z = phase;
104
105 /* The phase has to be somewhere between 0..pi for this to work right */
106 if (z < 0xffffffff / 4) {
107 /* z in first quadrant, z += pi/2 to correct */
108 x = -x;
109 z += 0xffffffff / 4;
110 } else if (z < 3 * (0xffffffff / 4)) {
111 /* z in third quadrant, z -= pi/2 to correct */
112 z -= 0xffffffff / 4;
113 } else {
114 /* z in fourth quadrant, z -= 3pi/2 to correct */
115 x = -x;
116 z -= 3 * (0xffffffff / 4);
117 }
118
119 /* Each iteration adds roughly 1-bit of extra precision */
120 for (i = 0; i < 31; i++) {
121 x1 = x >> i;
122 y1 = y >> i;
123 z1 = atan_table[i];
124
125 /* Decided which direction to rotate vector. Pivot point is pi/2 */
126 if (z >= 0xffffffff / 4) {
127 x -= y1;
128 y += x1;
129 z -= z1;
130 } else {
131 x += y1;
132 y -= x1;
133 z += z1;
134 }
135 }
136
137 if (cos)
138 *cos = x;
139
140 return y;
141}
142
143/**
144 * Fixed point square root via Newton-Raphson.
145 * @param a square root argument.
146 * @param fracbits specifies number of fractional bits in argument.
147 * @return Square root of argument in same fixed point format as input.
148 */
149static long fsqrt(long a, unsigned int fracbits)
150{
151 long b = a/2 + (1 << fracbits); /* initial approximation */
152 unsigned n;
153 const unsigned iterations = 8; /* bumped up from 4 as it wasn't
154 nearly enough for 28 fractional bits */
155
156 for (n = 0; n < iterations; ++n)
157 b = (b + (long)(((long long)(a) << fracbits)/b))/2;
158
159 return b;
160}
161
162/* this is the codec entry point */ 45/* this is the codec entry point */
163enum codec_status codec_main(void) 46enum codec_status codec_main(void)
164{ 47{
@@ -238,7 +121,7 @@ next_track:
238 int64_t c; 121 int64_t c;
239 int64_t d; 122 int64_t d;
240 123
241 fsincos((unsigned long)phasemultiple,&z); 124 fp_sincos((unsigned long)phasemultiple,&z);
242 125
243 a = (M_SQRT2*big28)-(z*big28/LONG_MAX); 126 a = (M_SQRT2*big28)-(z*big28/LONG_MAX);
244 127
@@ -247,7 +130,7 @@ next_track:
247 * which is sufficient here, but this is the only reason why I don't 130 * which is sufficient here, but this is the only reason why I don't
248 * use 32 fractional bits everywhere. 131 * use 32 fractional bits everywhere.
249 */ 132 */
250 d = fsqrt((a+b)*(a-b)/big28,28); 133 d = fp_sqrt((a+b)*(a-b)/big28,28);
251 c = (a-d)*big28/b; 134 c = (a-d)*big28/b;
252 135
253 coef1 = (c*8192) >> 28; 136 coef1 = (c*8192) >> 28;