diff options
Diffstat (limited to 'apps/codecs/adx.c')
-rw-r--r-- | apps/codecs/adx.c | 119 |
1 files changed, 118 insertions, 1 deletions
diff --git a/apps/codecs/adx.c b/apps/codecs/adx.c index e23b3d4f80..cc36f6a908 100644 --- a/apps/codecs/adx.c +++ b/apps/codecs/adx.c | |||
@@ -21,7 +21,6 @@ | |||
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 "fixedpoint.h" | ||
25 | 24 | ||
26 | CODEC_HEADER | 25 | CODEC_HEADER |
27 | 26 | ||
@@ -42,6 +41,124 @@ const long cutoff = 500; | |||
42 | 41 | ||
43 | static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR; | 42 | static int16_t samples[WAV_CHUNK_SIZE] IBSS_ATTR; |
44 | 43 | ||
44 | /* fixed point stuff from apps/plugins/lib/fixedpoint.c */ | ||
45 | |||
46 | /* Inverse gain of circular cordic rotation in s0.31 format. */ | ||
47 | static 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 */ | ||
50 | static 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 | */ | ||
94 | static 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 | */ | ||
149 | static 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 | |||
45 | /* this is the codec entry point */ | 162 | /* this is the codec entry point */ |
46 | enum codec_status codec_main(void) | 163 | enum codec_status codec_main(void) |
47 | { | 164 | { |