在音频算法中经常会使用log10的计算方法这里推荐一种芯片大厂的快速写法,效率较C语言库函数提高50%+
0: log10(0.000000) = -7.000000, TC_Log10(0.000000) = -7.020006
1: log10(312.562012) = 2.494936, TC_Log10(312.562012) = 2.474504
2: log10(625.124023) = 2.795966, TC_Log10(625.124023) = 2.775505
3: log10(937.685974) = 2.972057, TC_Log10(937.685974) = 2.959257
4: log10(1250.248047) = 3.096996, TC_Log10(1250.248047) = 3.076504
5: log10(1562.810059) = 3.193906, TC_Log10(1562.810059) = 3.168381
6: log10(1875.371948) = 3.273088, TC_Log10(1875.371948) = 3.260257
7: log10(2187.934082) = 3.340034, TC_Log10(2187.934082) = 3.331567
8: log10(2500.496094) = 3.398026, TC_Log10(2500.496094) = 3.377505
9: log10(2813.058105) = 3.449179, TC_Log10(2813.058105) = 3.423443
10: log10(3125.620117) = 3.494936, TC_Log10(3125.620117) = 3.469381
11: log10(3438.181885) = 3.536329, TC_Log10(3438.181885) = 3.515319
12: log10(3750.743896) = 3.574117, TC_Log10(3750.743896) = 3.561257
13: log10(4063.305908) = 3.608880, TC_Log10(4063.305908) = 3.607195
14: log10(4375.868164) = 3.641064, TC_Log10(4375.868164) = 3.632566
15: log10(4688.430176) = 3.671027, TC_Log10(4688.430176) = 3.655535
16: log10(5000.992188) = 3.699056, TC_Log10(5000.992188) = 3.678504
17: log10(5313.554199) = 3.725385, TC_Log10(5313.554199) = 3.701473
18: log10(5626.116211) = 3.750209, TC_Log10(5626.116211) = 3.724443
19: log10(5938.678223) = 3.773690, TC_Log10(5938.678223) = 3.747412
20: log10(6251.240234) = 3.795966, TC_Log10(6251.240234) = 3.770381
21: log10(6563.801758) = 3.817155, TC_Log10(6563.801758) = 3.793350
22: log10(6876.363770) = 3.837359, TC_Log10(6876.363770) = 3.816319
23: log10(7188.925781) = 3.856664, TC_Log10(7188.925781) = 3.839288
24: log10(7501.487793) = 3.875147, TC_Log10(7501.487793) = 3.862257
25: log10(7814.049805) = 3.892876, TC_Log10(7814.049805) = 3.885226
26: log10(8126.611816) = 3.909909, TC_Log10(8126.611816) = 3.908195
27: log10(8439.173828) = 3.926300, TC_Log10(8439.173828) = 3.922082
28: log10(8751.736328) = 3.942094, TC_Log10(8751.736328) = 3.933567
29: log10(9064.297852) = 3.957334, TC_Log10(9064.297852) = 3.945051
30: log10(9376.860352) = 3.972057, TC_Log10(9376.860352) = 3.956536
31: log10(9689.421875) = 3.986298, TC_Log10(9689.421875) = 3.968020
32: log10(10001.984375) = 4.000086, TC_Log10(10001.984375) = 3.979505
33: log10(10314.545898) = 4.013450, TC_Log10(10314.545898) = 3.990989
34: log10(10627.108398) = 4.026415, TC_Log10(10627.108398) = 4.002473
35: log10(10939.669922) = 4.039004, TC_Log10(10939.669922) = 4.013958
36: log10(11252.232422) = 4.051239, TC_Log10(11252.232422) = 4.025443
37: log10(11564.793945) = 4.063138, TC_Log10(11564.793945) = 4.036927
38: log10(11877.356445) = 4.074720, TC_Log10(11877.356445) = 4.048412
39: log10(12189.917969) = 4.086001, TC_Log10(12189.917969) = 4.059896
40: log10(12502.480469) = 4.096996, TC_Log10(12502.480469) = 4.071381
41: log10(12815.041992) = 4.107720, TC_Log10(12815.041992) = 4.082865
42: log10(13127.603516) = 4.118186, TC_Log10(13127.603516) = 4.094350
43: log10(13440.166016) = 4.128405, TC_Log10(13440.166016) = 4.105834
44: log10(13752.727539) = 4.138389, TC_Log10(13752.727539) = 4.117319
45: log10(14065.290039) = 4.148149, TC_Log10(14065.290039) = 4.128803
46: log10(14377.851563) = 4.157694, TC_Log10(14377.851563) = 4.140288
47: log10(14690.414063) = 4.167034, TC_Log10(14690.414063) = 4.151772
48: log10(15002.975586) = 4.176178, TC_Log10(15002.975586) = 4.163257
49: log10(15315.538086) = 4.185132, TC_Log10(15315.538086) = 4.174741
50: log10(15628.099609) = 4.193906, TC_Log10(15628.099609) = 4.186226
51: log10(15940.662109) = 4.202507, TC_Log10(15940.662109) = 4.197711
52: log10(16253.223633) = 4.210939, TC_Log10(16253.223633) = 4.209195
53: log10(16565.785156) = 4.219212, TC_Log10(16565.785156) = 4.217340
54: log10(16878.347656) = 4.227330, TC_Log10(16878.347656) = 4.223082
55: log10(17190.910156) = 4.235299, TC_Log10(17190.910156) = 4.228824
56: log10(17503.472656) = 4.243124, TC_Log10(17503.472656) = 4.234567
57: log10(17816.033203) = 4.250811, TC_Log10(17816.033203) = 4.240309
58: log10(18128.595703) = 4.258364, TC_Log10(18128.595703) = 4.246051
59: log10(18441.158203) = 4.265788, TC_Log10(18441.158203) = 4.251793
60: log10(18753.720703) = 4.273088, TC_Log10(18753.720703) = 4.257535
61: log10(19066.281250) = 4.280266, TC_Log10(19066.281250) = 4.263278
62: log10(19378.843750) = 4.287328, TC_Log10(19378.843750) = 4.269020
63: log10(19691.406250) = 4.294277, TC_Log10(19691.406250) = 4.274762
64: log10(20003.968750) = 4.301116, TC_Log10(20003.968750) = 4.280505
65: log10(20316.529297) = 4.307849, TC_Log10(20316.529297) = 4.286247
66: log10(20629.091797) = 4.314480, TC_Log10(20629.091797) = 4.291989
67: log10(20941.654297) = 4.321011, TC_Log10(20941.654297) = 4.297731
68: log10(21254.216797) = 4.327445, TC_Log10(21254.216797) = 4.303473
69: log10(21566.777344) = 4.333785, TC_Log10(21566.777344) = 4.309216
70: log10(21879.339844) = 4.340034, TC_Log10(21879.339844) = 4.314958
71: log10(22191.902344) = 4.346195, TC_Log10(22191.902344) = 4.320700
72: log10(22504.464844) = 4.352269, TC_Log10(22504.464844) = 4.326443
73: log10(22817.025391) = 4.358259, TC_Log10(22817.025391) = 4.332185
74: log10(23129.587891) = 4.364168, TC_Log10(23129.587891) = 4.337927
75: log10(23442.150391) = 4.369998, TC_Log10(23442.150391) = 4.343669
76: log10(23754.712891) = 4.375750, TC_Log10(23754.712891) = 4.349411
77: log10(24067.273438) = 4.381427, TC_Log10(24067.273438) = 4.355154
78: log10(24379.835938) = 4.387031, TC_Log10(24379.835938) = 4.360896
79: log10(24692.398438) = 4.392563, TC_Log10(24692.398438) = 4.366639
80: log10(25004.960938) = 4.398026, TC_Log10(25004.960938) = 4.372381
81: log10(25317.521484) = 4.403421, TC_Log10(25317.521484) = 4.378123
82: log10(25630.083984) = 4.408750, TC_Log10(25630.083984) = 4.383865
83: log10(25942.646484) = 4.414014, TC_Log10(25942.646484) = 4.389607
84: log10(26255.207031) = 4.419216, TC_Log10(26255.207031) = 4.395350
85: log10(26567.769531) = 4.424355, TC_Log10(26567.769531) = 4.401092
86: log10(26880.332031) = 4.429435, TC_Log10(26880.332031) = 4.406834
87: log10(27192.894531) = 4.434455, TC_Log10(27192.894531) = 4.412577
88: log10(27505.455078) = 4.439419, TC_Log10(27505.455078) = 4.418319
89: log10(27818.017578) = 4.444326, TC_Log10(27818.017578) = 4.424061
90: log10(28130.580078) = 4.449179, TC_Log10(28130.580078) = 4.429803
91: log10(28443.142578) = 4.453978, TC_Log10(28443.142578) = 4.435545
92: log10(28755.703125) = 4.458724, TC_Log10(28755.703125) = 4.441288
93: log10(29068.265625) = 4.463419, TC_Log10(29068.265625) = 4.447030
94: log10(29380.828125) = 4.468064, TC_Log10(29380.828125) = 4.452772
95: log10(29693.390625) = 4.472660, TC_Log10(29693.390625) = 4.458515
96: log10(30005.951172) = 4.477207, TC_Log10(30005.951172) = 4.464257
97: log10(30318.513672) = 4.481708, TC_Log10(30318.513672) = 4.469999
98: log10(30631.076172) = 4.486162, TC_Log10(30631.076172) = 4.475741
99: log10(30943.638672) = 4.490571, TC_Log10(30943.638672) = 4.481483
/*****************************************************************
* log10 计算循环1000次占用575214
* TC_Log10 计算循环1000次占用233155
*****************************************************************/
float TC_Log10(float amp)
{
int exponent, ind1;
float mantissa;
mantissa = frexp(amp, &exponent); // get mantissa and exponent of input number
mantissa = (mantissa - 1.0) * 2.0; // linear approximation of log over range (-1,0)
return 0.3010 * (exponent + mantissa);
}
int BaseFuncMain(void)
{
int i = 0;
#define TOTAL_COUNT (1000)
float aaa[TOTAL_COUNT] = {0};
float bbb[TOTAL_COUNT] = {0};
float ccc[TOTAL_COUNT] = {0};
for(i = 0; i < 100; i++)
{
aaa[i] = 100 * 3.12562 * i + 0.0000001;
bbb[i] = log10(aaa[i]);
ccc[i] = TC_Log10(aaa[i]);
printf("%d: log10(%f) = %f, TC_Log10(%f) = %f\r\n", i, aaa[i], bbb[i], aaa[i], ccc[i]);
}
return 1;
}