刘品 学以致用---博主简介：专注语音、视频数字信号处理方面的研究以及算法在手机平台上面的移植和优化。涉及语音/音频编解码，语音增强，语音识别，语音音质客观评估，VOIP语音引擎 Qos算法模块和声音变速不变调和变调不变速等功能实现。熟悉ARM 各个系列的CPU，用ARMv4,ARMv5,ARMv6 and Cortex Neon 汇编优化相关算法代码，争取perforamance 最优。目前主要Windows mobile and Android platform上做相关speech/audio算法以及应用开发, HEVC 编解码器PC 和手机上的开发与应用。(交流Email:liupin.2008@gmail.com)

十六 提高专业技能之 “Codec定点化”

Write by Liupin 2010-4-1

1. 基本理论

1)     为什么要定点化

Since the Core is an integer process,all floating point operations must be simulated using integer arithmetic. 现在大多数嵌入式CPU都是定点的，当然在这些CPU platform上是能跑浮点数的程序，但效果很低，原因是内部是调用floating 处理的库或专用的硬件处理单元。另一点，由于浮点数的处理单元功耗更高，这就限制在移动devices上的应用。

2)     定点化的方法

In signal processing the data oftenconsists of fractional values, in the range -1 to +1. This example uses exponentq and 32-bit integer mantissas (so that each value can be held in a single ARMregister). In order to be able to multiply two numbers without overflow, youneed , or . In practice q=14 is often chosen as this allows multiplication withseveral accumulates without risk of overflow。

Fig16.1     Q=14  Basic operations

X: Q14, Y :Q14, So XY: Q28, in order tonormal the Q value to 14, so the result have to right shift 14: (X*Y) >>14.

Example1:

To calculate the distance from theorigin, d = sqrt(x^2 + y^2 + z^2), in q=8 form. If you apply the above formulaedirectly and keep all intermediate answers in q=8 form, you arrive at thefollowing code:

x = (x*x)>>8 square x

y = (y*y)>>8 square y

z = (z*z)>>8 square z

s = x+y+z sum of squares

d = sqrt(s<<8)the distance in q=8 form

Alternatively, if you keep theintermediate answers in q=16 form, the number of shifts is reduced and theaccuracy increased:

x = x*x square of x in q=16 form

y = y*y square of y in q=16 form

z = z*z square of z in q=16 form

s = x+y+x sum of squares in q=16 form

d = sqrt(s) distance d in q=8 form

Conclusion:

(1)  If you add two numbers in Q-form, the stay in Q-form

(2)  If you multiply two numbers in Q-form, the answer is 2Q-from

(3) If you take the square root of a number in q form the answer isin q/2-form.

(4) To convert from q-formto r-form you shift left by (r-q) or right by (q-r),depending on which of q andr is greater

(5) To get the bestprecision results, choose q to be the largest number such that the intermediatecalculations cannot overflow.

1. 深入探究

1. 工程实战

AC3 Decoder---idctsc() function

void idctsc(DEC_PARAM *p_decparam, BUFF_PARAM *p_buff)

{

//DSPfract*fftrptr, *fftiptr, *fftrptr2, *fftiptr2;

//DSPfract*tcrptr, *tciptr, *cosptr, *sinptr;

//DSPfractar, ai, cr, ci;

int     *fftrptr, *fftiptr, *fftrptr2, *fftiptr2;

int     *tcrptr,*tciptr;

int     ar, ai, i;

int     fftbuf_fx[N], tcbuf_fx[N];

short   *cosptr, *sinptr;

short   cr, ci;

//对输入buffer里的数据进行Q31的定点化处理

for(i=0;i<N; i++)

{

fftbuf_fx[i]= (int)(p_buff->fftbuf[i] * 2147483647);

tcbuf_fx[i]  = (int)(p_aublk->tcbuf[i] * 2147483647);

fprintf(fdebug,"idctsc1: 0x%08x\n", fftbuf_fx[i]);

fprintf(fdebug,"idctsc2: 0x%08x\n", tcbuf_fx[i]);

}

……

进行相关运算

……

//恢复outputbuffer floating data, in order to the next function run.

for(i=0;i<N; i++)

{

p_buff->fftbuf[i]= (double)(fftbuf_fx[i]/2147483647.0);

}

}

1. 参考文献

[1]. ARM Document, ApplicationNote 33 --- Fixed point Arithmetic on the ARM

posted on 2010-07-30 15:37  刘品  阅读(...)  评论(...编辑  收藏
-->