一种近似AMP音频增益转dB的优化写法:20.0*log10(amp)

 AGC算法中有两个很重要的优化函数,没有具体测试这两个函数到底优化多少

2022年8月18日做了一个MIPS的效率测试,测试函数如下

 void TranslateDBCompare1(void)
{
    int i = 0;
    float white;

    for (i = 0; i < 100; i++)
    {
        white = (float)(i * (1.0 / 100.0)) + 0.5;
        white = AMF_TranslateVeryFastAmpToDB_Fast(white);
    }
}

void TranslateDBCompare2(void)
{
    int i = 0;
    float white;

    for (i = 0; i < 100; i++)
    {
        white = (float)(i * (1.0 / 100.0)) + 0.5;
        white = AMF_TranslateVeryFastAmpToDB(white);
    }
}
void TranslateDBCompare3(void)
{
    int i = 0;
    float white;

    for (i = 0; i < 100; i++)
    {
        white = (float)(i * (1.0 / 100.0)) + 0.5;
        white = AMF_TranslateVeryFastDBToAmp(white);
    }
}
void TranslateDBCompare4(void)
{
    int i = 0;
    float white;

    for (i = 0; i < 100; i++)
    {
        white = (float)(i * (1.0 / 100.0)) + 0.5;
        white = AMF_TranslateVeryFastDBToAmp_Fast(white);
    }
}

打印结果如下

white:  AMF_TranslateVeryFastAmpToDB_Fast               AMF_TranslateVeryFastAmpToDB
0.500000:       -6.020600               -6.020600
0.510000:       -5.900188               -5.848597
0.520000:       -5.779776               -5.679934
0.530000:       -5.659364               -5.514483
0.540000:       -5.538952               -5.352125
0.550000:       -5.418540               -5.192746
0.560000:       -5.298128               -5.036240
0.570000:       -5.177716               -4.882503
0.580000:       -5.057304               -4.731440
0.590000:       -4.936892               -4.582959
0.600000:       -4.816480               -4.436975
0.610000:       -4.696068               -4.293403
0.620000:       -4.575656               -4.152166
0.630000:       -4.455244               -4.013189
0.640000:       -4.334832               -3.876400
0.650000:       -4.214420               -3.741733
0.660000:       -4.094008               -3.609122
0.670000:       -3.973596               -3.478504
0.680000:       -3.853184               -3.349822
0.690000:       -3.732772               -3.223018
0.700000:       -3.612360               -3.098039
0.710000:       -3.491948               -2.974833
0.720000:       -3.371536               -2.853350
0.730000:       -3.251124               -2.733543
0.740000:       -3.130712               -2.615366
0.750000:       -3.010300               -2.498775
0.760000:       -2.889888               -2.383728
0.770000:       -2.769476               -2.270186
0.780000:       -2.649064               -2.158108
0.790000:       -2.528652               -2.047459
0.800000:       -2.408240               -1.938200
0.810000:       -2.287828               -1.830300
0.820000:       -2.167416               -1.723723
0.830000:       -2.047004               -1.618438
0.840000:       -1.926592               -1.514414
0.850000:       -1.806180               -1.411621
0.860000:       -1.685768               -1.310031
0.870000:       -1.565356               -1.209615
0.880000:       -1.444944               -1.110347
0.890000:       -1.324532               -1.012200
0.900000:       -1.204120               -0.915150
0.910000:       -1.083708               -0.819173
0.920000:       -0.963297               -0.724244
0.930000:       -0.842884               -0.630341
0.940000:       -0.722472               -0.537443
0.950000:       -0.602060               -0.445528
0.960000:       -0.481648               -0.354575
0.970000:       -0.361236               -0.264565
0.980000:       -0.240824               -0.175478
0.990000:       -0.120412               -0.087296
1.000000:       0.000000                0.000000
1.010000:       0.060206                0.086427
1.020000:       0.120412                0.172003
1.030000:       0.180618                0.256744
1.040000:       0.240824                0.340666
1.050000:       0.301030                0.423786
1.060000:       0.361236                0.506117
1.070000:       0.421442                0.587675
1.080000:       0.481648                0.668474
1.090000:       0.541853                0.748529
1.100000:       0.602060                0.827854
1.110000:       0.662266                0.906460
1.120000:       0.722472                0.984361
1.130000:       0.782678                1.061569
1.140000:       0.842884                1.138097
1.150000:       0.903090                1.213957
1.160000:       0.963297                1.289160
1.170000:       1.023502                1.363718
1.180000:       1.083708                1.437641
1.190000:       1.143914                1.510940
1.200000:       1.204120                1.583625
1.210000:       1.264326                1.655708
1.220000:       1.324532                1.727197
1.230000:       1.384738                1.798102
1.240000:       1.444944                1.868434
1.250000:       1.505150                1.938200
1.260000:       1.565356                2.007411
1.270000:       1.625562                2.076074
1.280000:       1.685768                2.144199
1.290000:       1.745974                2.211794
1.300000:       1.806180                2.278867
1.310000:       1.866386                2.345426
1.320000:       1.926592                2.411478
1.330000:       1.986798                2.477032
1.340000:       2.047004                2.542095
1.350000:       2.107210                2.606676
1.360000:       2.167416                2.670778
1.370000:       2.227622                2.734411
1.380000:       2.287828                2.797582
1.390000:       2.348034                2.860296
1.400000:       2.408240                2.922561
1.410000:       2.468446                2.984383
1.420000:       2.528652                3.045767
1.430000:       2.588858                3.106721
1.440000:       2.649064                3.167250
1.450000:       2.709270                3.227360
1.460000:       2.769476                3.287057
1.470000:       2.829682                3.346347
1.480000:       2.889888                3.405235
1.490000:       2.950094                3.463726

white:  AMF_TranslateVeryFastDBToAmp_Fast               AMF_TranslateVeryFastDBToAmp
0.500000:       -6.020600               -6.020600
0.510000:       -5.900188               -5.848597
0.520000:       -5.779776               -5.679934
0.530000:       -5.659364               -5.514483
0.540000:       -5.538952               -5.352125
0.550000:       -5.418540               -5.192746
0.560000:       -5.298128               -5.036240
0.570000:       -5.177716               -4.882503
0.580000:       -5.057304               -4.731440
0.590000:       -4.936892               -4.582959
0.600000:       -4.816480               -4.436975
0.610000:       -4.696068               -4.293403
0.620000:       -4.575656               -4.152166
0.630000:       -4.455244               -4.013189
0.640000:       -4.334832               -3.876400
0.650000:       -4.214420               -3.741733
0.660000:       -4.094008               -3.609122
0.670000:       -3.973596               -3.478504
0.680000:       -3.853184               -3.349822
0.690000:       -3.732772               -3.223018
0.700000:       -3.612360               -3.098039
0.710000:       -3.491948               -2.974833
0.720000:       -3.371536               -2.853350
0.730000:       -3.251124               -2.733543
0.740000:       -3.130712               -2.615366
0.750000:       -3.010300               -2.498775
0.760000:       -2.889888               -2.383728
0.770000:       -2.769476               -2.270186
0.780000:       -2.649064               -2.158108
0.790000:       -2.528652               -2.047459
0.800000:       -2.408240               -1.938200
0.810000:       -2.287828               -1.830300
0.820000:       -2.167416               -1.723723
0.830000:       -2.047004               -1.618438
0.840000:       -1.926592               -1.514414
0.850000:       -1.806180               -1.411621
0.860000:       -1.685768               -1.310031
0.870000:       -1.565356               -1.209615
0.880000:       -1.444944               -1.110347
0.890000:       -1.324532               -1.012200
0.900000:       -1.204120               -0.915150
0.910000:       -1.083708               -0.819173
0.920000:       -0.963297               -0.724244
0.930000:       -0.842884               -0.630341
0.940000:       -0.722472               -0.537443
0.950000:       -0.602060               -0.445528
0.960000:       -0.481648               -0.354575
0.970000:       -0.361236               -0.264565
0.980000:       -0.240824               -0.175478
0.990000:       -0.120412               -0.087296
1.000000:       0.000000                0.000000
1.010000:       0.060206                0.086427
1.020000:       0.120412                0.172003
1.030000:       0.180618                0.256744
1.040000:       0.240824                0.340666
1.050000:       0.301030                0.423786
1.060000:       0.361236                0.506117
1.070000:       0.421442                0.587675
1.080000:       0.481648                0.668474
1.090000:       0.541853                0.748529
1.100000:       0.602060                0.827854
1.110000:       0.662266                0.906460
1.120000:       0.722472                0.984361
1.130000:       0.782678                1.061569
1.140000:       0.842884                1.138097
1.150000:       0.903090                1.213957
1.160000:       0.963297                1.289160
1.170000:       1.023502                1.363718
1.180000:       1.083708                1.437641
1.190000:       1.143914                1.510940
1.200000:       1.204120                1.583625
1.210000:       1.264326                1.655708
1.220000:       1.324532                1.727197
1.230000:       1.384738                1.798102
1.240000:       1.444944                1.868434
1.250000:       1.505150                1.938200
1.260000:       1.565356                2.007411
1.270000:       1.625562                2.076074
1.280000:       1.685768                2.144199
1.290000:       1.745974                2.211794
1.300000:       1.806180                2.278867
1.310000:       1.866386                2.345426
1.320000:       1.926592                2.411478
1.330000:       1.986798                2.477032
1.340000:       2.047004                2.542095
1.350000:       2.107210                2.606676
1.360000:       2.167416                2.670778
1.370000:       2.227622                2.734411
1.380000:       2.287828                2.797582
1.390000:       2.348034                2.860296
1.400000:       2.408240                2.922561
1.410000:       2.468446                2.984383
1.420000:       2.528652                3.045767
1.430000:       2.588858                3.106721
1.440000:       2.649064                3.167250
1.450000:       2.709270                3.227360
1.460000:       2.769476                3.287057
1.470000:       2.829682                3.346347
1.480000:       2.889888                3.405235
1.490000:       2.950094                3.463726

总结:

1.这是一种近似的算法,可以预见最后的结果和计算结果不一致,可用于dB值不太精确的场合

2.这种近似的算法,效率却还并不一定高

NameAddressFileLineSizeTotal Without ChildrenTotal % Without ChildrenTotalTotal %levelcall count帧长度采样率MCPS(Hz)
TranslateDBCompare10x39479102107856160.89328026782.7235625256480001093422.6
TranslateDBCompare20x39580103107856171.89328026793.7245626256480001093228.282
TranslateDBCompare30x39681104107856182.89328026804.7255627256480001093034.032
TranslateDBCompare40x39782105107856193.89328026815.7265628256480001092839.852

 最后是函数原型

float AMF_TranslateVeryFastAmpToDB(float famp)
{
// out[0] = log (in[0]) * 0.43429448190325f;
    return 20.0 * log10f(famp);
}

float AMF_TranslateVeryFastDBToAmp(float fdB)
{
    return powf(10.0, fdB / 20.0f);
}

float AMF_TranslateVeryFastDBToAmp_Fast(float dB)
{
    // uses nonlinear table lookup to approximate
    // pow(10.0, dB/20.0)
    // Some math tricks that are used:
    // pow(10.0, x) = pow(2.0, x*logBase2of10)
    // pow(2.0, x) = pow(2.0, x.int) * pow(2.0, x.frac)

    int xint;
    float xfrac, tmp, powxfrac;

    tmp = dB * 0.166096;    // tmp = input * 0.05 * log_base2_of10
    xint = (int)floor(tmp);    // get integer part of input - set mode 1 bit to truncate towards -infinity
    xfrac = tmp - (float)xint;    // get fractional part of input
    powxfrac = xfrac + 1.0;    // linear approximation of desired function
    return ldexp(powxfrac, xint);    // use scalb assembly instruction
}

float AMF_TranslateVeryFastAmpToDB_Fast(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)

    // add exponent to mantissa, and scale by 20.0 * (1/log10(2)
    return 6.0206 * (exponent + mantissa);
}

文章最后,不推荐这种算法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ToneChip

觉得博主写得不错欢迎鼓励支持哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值