Simulation algorithm from floating point to fixed point[Copy link]
When we write DSP simulation algorithms, for convenience, we usually use high-level languages (such as C) to write simulation programs. The variables used in the program generally include both integers and floating-point numbers. For example, in Example 1.1, the variable i is an integer, while pi is a floating-point number, and hamwindow is a floating-point array. Example 1.1 256-point Hamming window calculation int i;+ float pi=3.14l59; float hamwindow[256]; for(i=0;i<256;i++) hamwindow=0.54-0.46*cos(2.0*pi*i/255); If we want to implement the above program with a fixed-point DSP chip, we need to rewrite the above program into an assembly language program for the DSP chip. In order to facilitate DSP program debugging and simulate the algorithm performance when implementing fixed-point DSP, it is generally necessary to rewrite the high-level floating-point algorithm into a high-level fixed-point algorithm before writing the DSP assembly program. Next we discuss the fixed-point implementation of basic arithmetic operations. 2.1 Fixed-point simulation of addition/subtraction in C language Suppose the expression of floating-point addition operation is: float x, y, z; z=x+y; The most important point in converting floating-point addition/subtraction into fixed-point addition/subtraction is to ensure the calibration of the two operands temp=x+temp; z=temp>>(Qx-Qz), if Qx>=Qz z=temp<<(Qz-Qx), if Qx<=Qz Example 1.4 Fixed-point addition with a result exceeding 16 bits Suppose x=15000, y=20000, then the floating-point operation value is z=x+y=35000. Obviously z>32767. Therefore Qx=1, Qy=0, Qz=0, then the fixed-point addition is: x=30000; y=20000; temp=20000<<1=40000; temp=temp+x=40000+30000=70000; z=70000L>>1=35000; Because the Q value of z is 0, the fixed-point value z=35000 is a floating-point value, where z is a long integer. When the result of addition or addition exceeds the 16-bit representation range, if the programmer can understand this situation in advance and needs to maintain the calculation accuracy, the 32-bit result must be maintained. If the program is operated according to 16-bit numbers, exceeding 16 bits actually means overflow. If appropriate measures are not taken, data overflow will lead to a serious deterioration in calculation accuracy. General fixed-point DSP chips do not have overflow protection function. When the overflow protection function is effective, once an overflow occurs, the result of the accumulator ACC is the maximum saturation value (overflow is 7FFFH, underflow is 8001H), thereby preventing the overflow from causing a serious deterioration in accuracy. 2.2 Fixed-point simulation of multiplication in C language Suppose the expression of floating-point multiplication is: float x, y, z; z=xy; Assume that after statistics, the calibration value of x is Qx, the calibration value of y is Qy, and the calibration value of the product z is Qz, then z=xy zq*2-Qx=xq*yq*2-(Qx+Qy) zq=(xqyq)2Qz-(Qx+Qy) zq=(xqyq)2Qz-(Qx+Qy) Therefore, the multiplication represented by fixed-point is: int x, y, z; long temp; temp=(long)x; z=(temp*y)>>(Qx+Qy-Qz); Example 1.5 Fixed-point multiplication. Assume x=18.4, y=36.8, then the floating point value is =18.4*36.8=677.12; According to the previous section, Qx=10, Qy=9, Qz=5, so x=18841; y=18841; temp=18841L; z=(18841L*18841)>>(10+9-5)=354983281L>>14=21666; Because the calibration value of z is 5, the fixed point z=21666, that is, the floating point z=21666/32=677.08. 2.3 Fixed-point simulation of division operation in C language Suppose the expression of floating-point division operation is: float x, y, z; z = x/y; Assume that after statistics, the calibration value of dividend x is Qx, the calibration value of divisor y is Qy, and the calibration value of quotient z is Qz, then z = x/y zq*2-Qz = (xq*2-Qx)/(yq*2-Qy) zq=(xq*2(Qz-Qx+Qy))/yq So the division in fixed-point representation is: int x, y, z; long temp; temp=(long)x; z=(temp<<(Qz-Qx+Qy))/y; Example 1.6 Fixed-point division. Assume x=18.4, y=36.8, the floating-point operation value is z=x/y=18.4/36.8=0.5; According to the previous section, we get Qx=10, Qy=9, Qz=15; so z=18841, y=18841; temp=(long)18841; z=(18841L<<(15-10+9)/18841=3O8690944L/18841=16384; Because the calibration value of the quotient z is 15, the fixed point z=16384, that is, the floating point z=16384/215=0.5.