-
Notifications
You must be signed in to change notification settings - Fork 13
/
db.c
276 lines (255 loc) · 9.9 KB
/
db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// db.c - convert magnitudes to and from decibels
#include <stdio.h>
#include <math.h>
#include "chapro.h"
// METHOD: 0=exact, 1=polynomial_ratio, 2=lookup_table
#define METHOD 2
/***********************************************************/
#if METHOD == 1
static __inline float
pow_pr(float x) // approximate 2^x
{
float p, q;
int n;
static float a0 = -206059.514;
static float a1 = -72102.2578;
static float a2 = -11240.0288;
static float a3 = -989.027847;
static float b0 = -206059.514;
static float b1 = 70727.3131;
static float b2 = -10763.5093;
static float b3 = -14.5169712;
// assume: x > -0.5 and x < 0.5
n = (x < 0);
if (n) x = -x;
p = (((a3 * x + a2) * x + a1) * x + a0);
q = (((b3 * x + b2) * x + b1) * x + b0);
if (n) return (q / p);
return (p / q);
}
static __inline float
log_pr(float x) // approximate ln(x)
{
float p, q, z, z2;
static float a0 = 75.1518561;
static float a1 = -134.730400;
static float a2 = 74.2011014;
static float b0 = 37.5759281;
static float b1 = -79.8905092;
static float b2 = 56.2155348;
// assume: x > sqrt(1/2) and x < sqrt(2)
z = (x - 1) / (x + 1);
z2 = z * z;
p = ((a2 * z2 + a1) * z2 + a0);
q = ((b2 * z2 + b1) * z2 + b0);
return (z * p / q);
}
#elif METHOD == 2
static __inline float
pow_lu(float x) // approximate 2^x
{
float f, y;
int i;
static float xmn = -0.500000000;
static float odx = 1.270000000e+02;
static float lu[] = { // pow
7.071067691e-01f, 7.109766006e-01f, 7.148676515e-01f, 7.187799215e-01f,
7.227136493e-01f, 7.266688943e-01f, 7.306457758e-01f, 7.346444726e-01f,
7.386649847e-01f, 7.427075505e-01f, 7.467722297e-01f, 7.508591413e-01f,
7.549684048e-01f, 7.591001987e-01f, 7.632545829e-01f, 7.674316764e-01f,
7.716316581e-01f, 7.758546472e-01f, 7.801007032e-01f, 7.843700051e-01f,
7.886626720e-01f, 7.929788828e-01f, 7.973186970e-01f, 8.016822338e-01f,
8.060696125e-01f, 8.104810715e-01f, 8.149166703e-01f, 8.193765283e-01f,
8.238607645e-01f, 8.283695579e-01f, 8.329030275e-01f, 8.374613523e-01f,
8.420445919e-01f, 8.466528654e-01f, 8.512864113e-01f, 8.559452891e-01f,
8.606297374e-01f, 8.653397560e-01f, 8.700755835e-01f, 8.748372793e-01f,
8.796250820e-01f, 8.844390512e-01f, 8.892793655e-01f, 8.941462040e-01f,
8.990396857e-01f, 9.039599299e-01f, 9.089071155e-01f, 9.138813019e-01f,
9.188827872e-01f, 9.239116311e-01f, 9.289679527e-01f, 9.340519905e-01f,
9.391638637e-01f, 9.443036914e-01f, 9.494716525e-01f, 9.546679258e-01f,
9.598925710e-01f, 9.651458859e-01f, 9.704278708e-01f, 9.757388234e-01f,
9.810788035e-01f, 9.864480495e-01f, 9.918466210e-01f, 9.972748160e-01f,
1.002732635e+00f, 1.008220434e+00f, 1.013738155e+00f, 1.019286036e+00f,
1.024864435e+00f, 1.030473232e+00f, 1.036112785e+00f, 1.041783214e+00f,
1.047484636e+00f, 1.053217292e+00f, 1.058981299e+00f, 1.064776897e+00f,
1.070604205e+00f, 1.076463342e+00f, 1.082354546e+00f, 1.088278055e+00f,
1.094233990e+00f, 1.100222468e+00f, 1.106243730e+00f, 1.112297893e+00f,
1.118385315e+00f, 1.124505997e+00f, 1.130660176e+00f, 1.136847973e+00f,
1.143069744e+00f, 1.149325490e+00f, 1.155615449e+00f, 1.161939859e+00f,
1.168298960e+00f, 1.174692750e+00f, 1.181121588e+00f, 1.187585592e+00f,
1.194085002e+00f, 1.200619936e+00f, 1.207190633e+00f, 1.213797331e+00f,
1.220440149e+00f, 1.227119327e+00f, 1.233835101e+00f, 1.240587592e+00f,
1.247377038e+00f, 1.254203677e+00f, 1.261067629e+00f, 1.267969251e+00f,
1.274908543e+00f, 1.281885743e+00f, 1.288901210e+00f, 1.295955062e+00f,
1.303047538e+00f, 1.310178876e+00f, 1.317349195e+00f, 1.324558735e+00f,
1.331807733e+00f, 1.339096427e+00f, 1.346424937e+00f, 1.353793621e+00f,
1.361202717e+00f, 1.368652225e+00f, 1.376142621e+00f, 1.383673906e+00f,
1.391246438e+00f, 1.398860335e+00f, 1.406516075e+00f, 1.414213538e+00f};
y = (x - xmn) * odx;
i = (int) y;
f = y - i;
if (f == 0) return (lu[i]);
return ((1 - f) * lu[i] + f * lu[i + 1]);
}
static __inline float
log_lu(float x) // approximate ln(x)
{
float f, y;
int i;
static float xmn = 0.707106769f;
static float odx = 1.796051178e+02f;
static float lu[] = { // log
-3.465736210e-01f, -3.387303948e-01f, -3.309483230e-01f, -3.232262433e-01f,
-3.155633509e-01f, -3.079588115e-01f, -3.004115820e-01f, -2.929208875e-01f,
-2.854859531e-01f, -2.781058252e-01f, -2.707797587e-01f, -2.635069788e-01f,
-2.562867701e-01f, -2.491182536e-01f, -2.420008332e-01f, -2.349336445e-01f,
-2.279160470e-01f, -2.209473550e-01f, -2.140269727e-01f, -2.071540654e-01f,
-2.003280669e-01f, -1.935484409e-01f, -1.868143827e-01f, -1.801253706e-01f,
-1.734808683e-01f, -1.668801606e-01f, -1.603227407e-01f, -1.538081169e-01f,
-1.473355740e-01f, -1.409046650e-01f, -1.345148385e-01f, -1.281656623e-01f,
-1.218564659e-01f, -1.155868992e-01f, -1.093563288e-01f, -1.031643376e-01f,
-9.701044858e-02f, -9.089426696e-02f, -8.481520414e-02f, -7.877293229e-02f,
-7.276688516e-02f, -6.679669768e-02f, -6.086194515e-02f, -5.496226251e-02f,
-4.909712449e-02f, -4.326624796e-02f, -3.746910766e-02f, -3.170538321e-02f,
-2.597468905e-02f, -2.027664892e-02f, -1.461095270e-02f, -8.977175690e-03f,
-3.374900669e-03f, 2.196163638e-03f, 7.736362983e-03f, 1.324603800e-02f,
1.872540452e-02f, 2.417502925e-02f, 2.959511615e-02f, 3.498598188e-02f,
4.034794495e-02f, 4.568130895e-02f, 5.098637938e-02f, 5.626345426e-02f,
6.151271611e-02f, 6.673467904e-02f, 7.192951441e-02f, 7.709749788e-02f,
8.223880827e-02f, 8.735392243e-02f, 9.244301170e-02f, 9.750632942e-02f,
1.025441438e-01f, 1.075567007e-01f, 1.125442609e-01f, 1.175070703e-01f,
1.224452630e-01f, 1.273592860e-01f, 1.322492957e-01f, 1.371155083e-01f,
1.419581473e-01f, 1.467773467e-01f, 1.515735239e-01f, 1.563468277e-01f,
1.610974520e-01f, 1.658256054e-01f, 1.705315113e-01f, 1.752153784e-01f,
1.798774004e-01f, 1.845176965e-01f, 1.891366690e-01f, 1.937343925e-01f,
1.983110756e-01f, 2.028668076e-01f, 2.074019760e-01f, 2.119166702e-01f,
2.164110839e-01f, 2.208853662e-01f, 2.253397405e-01f, 2.297743559e-01f,
2.341893911e-01f, 2.385850102e-01f, 2.429613024e-01f, 2.473186255e-01f,
2.516570389e-01f, 2.559766173e-01f, 2.602777183e-01f, 2.645604014e-01f,
2.688248158e-01f, 2.730711102e-01f, 2.772994637e-01f, 2.815100253e-01f,
2.857029140e-01f, 2.898783088e-01f, 2.940362394e-01f, 2.981770337e-01f,
3.023007810e-01f, 3.064075708e-01f, 3.104974627e-01f, 3.145708144e-01f,
3.186276257e-01f, 3.226680458e-01f, 3.266922235e-01f, 3.307002485e-01f,
3.346922994e-01f, 3.386684656e-01f, 3.426288664e-01f, 3.465735614e-01f};
y = (x - xmn) * odx;
i = (int) y;
f = y - i;
if (f == 0) return (lu[i]);
return ((1 - f) * lu[i] + f * lu[i + 1]);
}
#endif // METHOD
/***********************************************************/
FUNC(float)
cha_db1(float x) // 10 * log10(x)
{
float m, ln;
int e;
static float c1 = 1e-38f;
static float c2 = -380;
static float c3 = 1e38f;
static float c4 = 380;
static float c5 = 0.707106769f; // sqrt(0.5)
static float c6 = 4.34294462f; // 10 / log(10)
static float c7 = 0.693147182f; // log(2);
if (x < c1) return (c2);
if (x > c3) return (c4);
m = frexpf(x, &e);
if (m < c5) {
m *= 2;
e--;
}
#if METHOD == 0
ln = logf(m); // exact
#elif METHOD == 1
ln = log_pr(m);
#elif METHOD == 2
ln = log_lu(m);
#endif
return (c6 * (ln + c7 * e));
}
FUNC(float)
cha_undb1(float x) // 10 ^ (x / 10)
{
float m, p2;
int e;
static float c1 = 1e-38f;
static float c2 = -380;
static float c3 = 1e38f;
static float c4 = 380;
static float c5 = 0.166096404f; // log(10) / (20 * log(2))
if (x < c2) return (c1);
if (x > c4) return (c3);
x *= c5;
e = (int) x;
m = x - e;
if (m < -0.5) {
m++;
e--;
} else if (m > 0.5) {
m--;
e++;
}
#if METHOD == 0
p2 = powf(2, m); // exact
#elif METHOD == 1
p2 = pow_pr(m);
#elif METHOD == 2
p2 = pow_lu(m);
#endif
return (ldexpf(p2, e));
}
/***********************************************************/
FUNC(float)
cha_db2(float x) // 20 * log10(x)
{
float m, ln;
int e;
static float c1 = 1e-38f;
static float c2 = -760;
static float c3 = 1e38f;
static float c4 = 760;
static float c5 = 0.707106769f; // sqrt(0.5)
static float c6 = 8.68588924f; // 20 / log(10)
static float c7 = 0.693147182f; // log(2);
if (x < c1) return (c2);
if (x > c3) return (c4);
m = frexpf(x, &e);
if (m < c5) {
m *= 2;
e--;
}
// assume: x > sqrt(0.5) and x < sqrt(2)
#if METHOD == 0
ln = logf(m); // exact
#elif METHOD == 1
ln = log_pr(m);
#elif METHOD == 2
ln = log_lu(m);
#endif
return (c6 * (ln + c7 * e));
}
FUNC(float)
cha_undb2(float x) // 10 ^ (x / 20)
{
float m, p2;
int e;
static float c1 = 1e-38f;
static float c2 = -760;
static float c3 = 1e38f;
static float c4 = 760;
static float c5 = 0.166096404f; // log(10) / (20 * log(2))
if (x < c2) return (c1);
if (x > c4) return (c3);
x *= c5;
e = (int) ((x < 0) ? (x - 0.5) : (x + 0.5));
m = x - e;
// assume: x > -0.5 and x < 0.5
#if METHOD == 0
p2 = powf(2, m); // exact
#elif METHOD == 1
p2 = pow_pr(m);
#elif METHOD == 2
p2 = pow_lu(m);
#endif
return (ldexpf(p2, e));
}