DSP Fixed-Point Calculation Method

Source: Internet
Author: User

One DSP Fixed-Point Arithmetic Operation
1-digit Calibration
In a fixed-point DSP chip, the number of fixed points is used for numerical calculation, and the operands are generally expressed by integer numbers. The maximum value of an integer is determined by the length of the DSP chip, which is generally 16 or 24 bits. Obviously, the longer the word length, the larger the range of numbers that can be expressed, the higher the accuracy. Unless otherwise stated, this book uses 16 characters as an example.
The number of DSP chips is expressed in the form of 2's complement. Each 16-digit value uses a single sign bit to indicate positive and negative numbers. 0 indicates positive values, and l indicates negative values. The remaining 15 digits indicate the value size. Therefore,
Binary Number 0010000000000011b = 8195
Binary Number 111111111100b =-4
For the DSP chip, the number involved in numerical calculation is the 16-bit integer number. However, in many cases, the number in the mathematical operation is not necessarily an integer. So how does the DSP chip process decimals? It should be said that the DSP chip itself is powerless. Does it mean that the DSP chip cannot process various decimals? Of course not. The key is that the programmer determines which of the 16 decimal places of a number. This is the number calibration.
By setting different decimal places in the 16-digit range, the decimal places of different sizes and different precision can be expressed. There are two types of data calibration: Q representation and s representation. Table 1.1 lists a 16-digit 16-digit Q representation, s representation, and the range of decimal values they can represent.
From table 1.1, we can see that if the decimal point is set to a different position, the number indicated by it is also different. For example,
Hexadecimal number: 2000 h = 8192, expressed as q0
Hexadecimal number 2000 h = 0.25, expressed in q15
However, for DSP chips, the processing method is the same.
From table 1.1, we can also see that different Q numbers have different ranges and accuracy. The larger the Q, the smaller the value range, but the higher the accuracy. On the contrary, the smaller the Q, the larger the value range, but the lower the accuracy. For example, the value range of q0 is 32768 to + 32767, and its precision is 1, while the value range of q15 is-1 to 0.9999695, and the accuracy is 1/32768 = 0.00003051. Therefore, for a fixed number of points, the value range and accuracy are a conflict. To express a large value range, a variable must sacrifice its precision, then the range of the number is reduced accordingly. In actual fixed-point algorithms, this must be fully taken into account to achieve optimal performance.
The Conversion Relationship between floating point numbers and fixed points can be expressed:
Convert a floating point number (X) to a fixed point number (XQ): XQ = (INT) x * 2q
Convert a fixed point (XQ) to a floating point (x): x = (float) XQ * 2-q
For example, if the floating point X = 0.5 and the calibration q = 15, the number of x q = l0.5 * 32768j = 16384. In the formula, LJ indicates the next integer. Conversely, a fixed number of points represented by Q = 15 is 16384, and its floating point number is 163 young * 2-15 = 16384/32768 = 0.5. When a floating point is converted to a fixed number of points, you can add 0.5 before the integer to reduce the tail truncation error.

Table 1.1 Q, S, and Numerical range
Q indicates the value of S in decimal format.
Q15 s0.15-1 ≤ x ≤ 0.9999695
Q14 s1.14-2 ≤ x ≤ 1.9999390
Q13 s2.13-4 ≤ x ≤ 3.9998779
Q12 s3.12-8 ≤ x ≤ 7.9997559
Q11 s4.11-16 ≤ x ≤ 15.9995117
Q10 s5.10-32 ≤x ≤ 31.9990234
Q9 s6.9-64 ≤ x ≤ 63.9980469
Q8 s7.8-128 ≤ x ≤ 127.9960938
Q7 S8.7-256 ≤ x ≤ 255.9921875
Q6 s9.6-512 ≤ x ≤ 511.9804375
Q5 s10.5-1024 ≤ x ≤ 1023.96875
Q4 s11.4-2048 ≤ x ≤ 2047.9375
Q3 s12.3-4096 ≤ x ≤ 4095.875
Q2 s13.2-8192 ≤ x ≤ 8191.75
Q1 s14.1-16384 ≤ x ≤ 16383.5
Q0 s15.0-32768 ≤ x ≤ 32767

2. Advanced Language: from floating point to fixed point
When writing DSP simulation algorithms, we generally use advanced languages (such as C) to compile simulation programs for convenience. The variables used in the program generally have both integer and floating point numbers. For example, in the 1.1 program, the variable I is an integer, the PI is a floating point, and the hamwindow is a floating point group.
Example 1.1 Hamming window computing
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 certain DSP chip, we need to rewrite the above program to the Assembly Language Program of the DSP chip. For the convenience of DSP program debugging and the algorithm performance when simulating fixed-point DSP Implementation, before writing a DSP assembly program, you generally need to rewrite the floating-point algorithm in advanced language to a fixed-point algorithm in advanced language. Next we will discuss the implementation of the fixed point for basic arithmetic operations.
2.1 addition/subtraction operations
The expression used to set the floating-point addition operation is:
Float x, y, z;
Z = x + y;
When converting floating-point addition/subtraction to fixed-point addition/subtraction, the most important thing 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
In Example 1.4, if the result is greater than 16 bits
If X = l5000 and Y = 20000 is set, the floating point value is Z = x + y = 35000. Obviously, z> 32767.
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. Here Z is a long integer. When the result of addition or addition exceeds the 16-bit expression range, if the programmer knows this situation in advance and needs to maintain the accuracy of the operation, the 32-bit result must be maintained. If the program performs 16-digit computation, exceeding 16 bits actually results in overflow. If appropriate measures are not taken, data overflow will cause serious deterioration of the computing accuracy. Generally, the fixed-point DSP chip does not have the overflow protection function. When the overflow protection function is effective, once the overflow occurs, the ACC result of The accumulators is the largest saturation value (overflow is 7 fffh, overflow is 8001 H), so as to prevent serious deterioration of precision caused by overflow.
2.2 fixed-point simulation of multiplication in C Language
Set the floating-point multiplication expression:
Float x, y, z;
Z = xy;
Assuming that the calibration value of X is QX, Y is QY, and Z is qz
Z = xy
ZQ * 2-qx = XQ * yq * 2-(qx + Qy)
ZQ = (xqyq) 2qz-(qx + Qy)
Therefore, the multiplication indicated by the fixed point is:
Int x, y, z;
Long temp;
Temp = (long) X;
Z = (temp * Y)> (qx + Qy-qz );
Example 1.5 fixed-point multiplication.
If X = 18.4 and Y = 36.8, the floating point value is = 18.4*36.8 = 677.12;
According to the above 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 Z calibration value is 5, the fixed point Z = 21666, that is, the floating point Z = 21666/32 = 677.08.
2.3 Division operations
The expression used to set the floating-point division operation is:
Float x, y, z;
Z = x/y;
Assume that after statistics, the calibration value of divisor X is QX, the calibration value of divisor y is QY, and the calibration value of quotient Z is qz.
Z = x/y
ZQ * 2-qz = (XQ * 2-qx)/(yq * 2-qy)
ZQ = (XQ * 2 (qz-qx + Qy)/yq
Therefore, the Division expressed by the fixed point is:
Int x, y, z;
Long temp;
Temp = (long) X;
Z = (temp <(qz-qx + Qy)/y;
Example 1.6 point division.
Set x = 18.4, y = 36.8, and the floating point value to Z = x/y = 18.4/36.8 = 0.5;
According to the above section, QX = 10, Qy = 9, qz = 15;
Z = 18841, y = 18841;
Temp = (long) 18841;
Z = (18841l <(15-10 + 9)/18841 = 3o8690944l/18841 = 16384;
Because the calibration value of commercial Z is 15, the fixed point Z = 16384, that is, floating point Z = 16384/215 = 0.5.
2.4 determine the Q value of the program variable
In the examples described in the previous sections, since the values of X, Y, and Z are all known, the Q value is well determined when the floating point is changed to a fixed point. In the actual DSP application, all variables involved in the calculation are involved in the program. How can we determine the Q value of the variables in the floating point program? From the previous analysis, we can know that determining the Q value of a variable is actually determining the dynamic range of the variable. If the dynamic range is determined, the Q value is also determined.
Set the maximum value of the absolute value of the variable to | max |, note | max | the value must be less than or equal to 32767. Take an integer n to satisfy
2n-1 <| max | <2N
Then there is
2-q = 2-15 * 2n = 2-(15-n)
Q = 15-n
For example, the value of a variable ranges from-1 to + 1, that is, | max | <1, so n = 0, q = 15-n = 15.
Since the value of the variable | max | can be determined, how can we determine the value of the variable | max |? In general, there are two methods to determine the variable | max |. One is theoretical analysis, and the other is statistical analysis.
1. Theoretical Analysis
The dynamic range of some variables can be determined through theoretical analysis. For example:
(1) trigonometric function. Y = sin (x) or Y = cos (x), which is known by trigonometric function knowledge. | Y | <= 1.
(2) Hamming window. Y (n) = 0.54 A 0.46cos [N π N/(N-1)], 0 <= n <= N-1. Because-1 <= cos [2 π N/(N-1)] <= 1, so 0.08 <= Y (n) <= 1.0.
(3) FIR convolution. Y (n) = Sigma H (k) x (n-k), set Sigma | H (k) | = 1.0, and x (n) is the 12-bit quantization value of the analog signal, I .e. | x (n) | <= 211, then | Y (n) | <= 211.
(4) It has been proved theoretically that in the programming of Self-correlation linear prediction coding (LPC), the reflection coefficient Ki satisfies the following inequality: | Ki | <1.0, I = 1, 2 ,..., P and P are the order of LPC.
2. Statistical analysis
For variables that cannot determine the range theoretically, statistical analysis is generally used to determine the dynamic range. The so-called statistical analysis is to use enough input signal sample values to determine the dynamic range of variables in the program. Here, the input signal must have a certain number, and on the other hand, it must involve various situations as much as possible. For example, in voice signal analysis, a sufficient amount of voice signal sample values must be collected during statistical analysis. In addition, the collected voice sample values should include situations as much as possible. Such as the volume and type of voice (male and female voice ). Only in this way can the statistical results be typical.
Of course, statistical analysis cannot cover all possible situations after all. Therefore, some protective measures can be taken for the statistical results during program design, such as sacrificing some precision, the Q value is slightly higher than the statistical value, and the overflow protection function provided by DSP chip is used.
2.5 example of a C program for floating point to fixed point transformation
In this section, we use an example to illustrate how C programs transform from floating point to fixed point. This is a voice signal (0.3 ~ 3.4 kHz) for low-pass filtering C Language Program, the low-pass filtering cutoff frequency is 800Hz, the filter uses 19 points of limited impact response FIR filter. The sampling frequency of the voice signal is 8 kHz. Each voice sample value is stored in the INsp. dat file in a 16-bit integer value.
Example 1.7 voice signal 800Hz 19-point FIR low-pass filter C language floating point program.
# I nclude <stdio. h>
Const int length = 180/* voice frame length: 180 points = 22.5ms @ 8 KHz sampling */
Void filter (INT Xin [], int xout [], int N, float H []);/* filter subroutine Description */
/* 19-point filter coefficient */
Static float H [19] =
{0.01218354,-0.009012882,-0.02881839,-0.04743239,-0.04584568,
-0.008692503, 0.06446265, 0.1544655, 0.2289794, 0.257883,
0.2289794, 0.1544655, 0.06446265,-0.008692503,-0.04584568,
-0.04743239,-0.02881839,-0.009012882, o.01218354 };
Static int XL [Length + 20];
/* Low-pass filter floating-point subroutine */
Void filter (INT Xin [], int xout [], int N, float H [])
{
Int I, J;
Float sum;
For (I = 0; I <length; I ++) x1 [n + I-1] = Xin;
For (I = 0; I <length; I ++)
{
Sums = 0.0;
For (j = 0; j <n; j ++) sum + = H [J] * X1 [I-j + n-1];
Xout = (INT) sum;
For (I = 0; I <(N-l); I ++) x1 [n-i-2] = Xin [length-1-i];
}
/* Main Program */
Void main ()
File * FP1, * fp2;
Int, indata [length], outdata [length];
FP1 = fopen (INsp. dat, "rb");/* input voice file */
Fp2 = fopen (outsp. dat, "WB");/* audio file after filtering */
= 0;
While (feof (FP1) = 0)
{
++;
Printf ("= % d \ n ",);
For (I = 0; I <length; I ++) indata = getw (FP1);/* Get one frame of Speech Data */
Filter (indata, outdata, 19, H);/* call the low-pass filter subroutine */
For (I = 0; I <length; I ++) putw (outdata, fp2);/* write the filtered sample value to the file */
}
Fcloseall ();/* close the file */
Return (0 );
}
Example 1.8 voice signal 800Hz point FIR low-pass filtering C language fixed point program.
# I nclude <stdio. h>
Const int length = 180;
Void filter (INT Xin [], int xout [], int N, int H []);
Static int H [19] = {399,-296,-945,-1555,-1503,-285, 2112, 5061,7503, 8450,
7503,5061, 2112,-285,-1503,-1555,-945,-296,399};/* q15 */
Static int X1 [Length + 20];
/* Fixed-point subroutine of low-pass filtering */
Void filter (INT Xin [], int xout [], int N, int H [])
Int I, J;
Long sum;
For (I = 0; I <length; I ++) x1 [n + i-111 = Xin];
For (I = 0; I <1 ength; I ++)
Sum = 0;
For (j = 0; j <n; j ++) sum + = (long) H [J] * X1 [I-j + n-1];
Xout = sum> 15;
For (I = 0; I <(n-1); I ++) x1 [n-i-2] = Xin [length-i-1];
}
The main program is exactly the same as the floating point program. "
3 DSP Fixed-Point Arithmetic Operation
The numeric representation of the fixed-point DSP chip is based on the 2's complement representation. Each 16-digit value is represented by an l sign bit, an I integer, and 15-I decimal places. Therefore:
00000010.10100000
The value is:
21 + 2-1 + 2-3 = 2.625
This number can be expressed in Q8 format (8 decimal places). The value range is-128 to + l27.996, And the decimal precision of a Q8 point is 1/256 = 0.004.
Although hybrid notation is required in special cases (such as dynamic range and accuracy requirements. However, generally all decimal points in q15 format or integers in q0 format are used. This is especially true for signal processing algorithms that mainly involve multiplication and accumulation. decimals are multiplied by decimal places, and integers are multiplied by integers. Of course, overflow may occur when the product is accumulated. In this case, the programmer should understand the physical process in mathematics to pay attention to the possible overflow. Next we will discuss the fixed-point operations of the DSP for multiplication, addition, and division. The assembler takes TMS320C25 as an example.
3.1 fixed-point Multiplication
When two fixed points are multiplied, they can be divided into the following three conditions:
1. decimal place
Example 1.9 q15 * q15 = Q30
0.5*0.5 = 0.25
0.100000000000000; q15
* 0.100000000000000; q15
--------------------------------------------
00.010000000000000000000000000000 = 0.25; Q30
After the decimal digits of two q15 values are multiplied, a decimal number of Q30 is obtained, that is, two signed digits. Generally, the number of full precision obtained after multiplication does not have to be fully retained, but only 16-bit single precision is retained. Because of the high 16-bit less than 15-bit small data degree obtained after multiplication, in order to reach the 15-bit precision, the product can be moved to the left. below is the TMS320C25 program of the above multiplication:
Lt OP1; OP1 = 4000 H (0.5/q15)
Mpy OP2; OP2 = 4000 H (0.5/ql5)
Pac
SACH ans, 1; ans = 2000 h (0.25/q15)
2. Integer multiplied by integer
Example 1.10 q0 * q0 = q0
17 * (-5) =-85
0000000000010001 = L7
* 1111111111111011 =-5
-------------------------------------------
11111111111111111111111110101011 =-85
3. Mixed Representation
In many cases, the representation between q0 and q15 must be used to satisfy the dynamic range of values and ensure certain accuracy. For example, if the value is 1.2345, it is obvious that q15 cannot be expressed. If q0 is used, the nearest number is 1, and the accuracy cannot be guaranteed. Therefore, the optimal representation of the number of 1.2345 is q14.
Example 1.11 1.5*0.75 = 1.125
01.10000000000000 = 1.5; q14
* 00.11000000000000 = 0.75; q14
---------------------------------------
0001.0010000000000000000000000000 = 1.125 Q28
The maximum value of q14 is not greater than 2. Therefore, the product of the two q14 numbers is not greater than 4.
Generally, if the integer of a number is I, the decimal point is J, the integer of the other number is m, and the decimal point is N, then the product of these two numbers is the (I + M) integer and (J + n) decimal place. The maximum 16 bits of this product may be accurate to (I + M) integer and (15-I-m) decimal places.
However, if you know the dynamic range of the number in advance, you can increase the precision of the number. For example, if a programmer understands that the above product will not exceed 1.8, he can use the q14 number to represent the product, rather than the theoretical best case q13. In Example 3.11, the TMS320C25 program is as follows:
Lt OP1; OP1 = 6000 H (1.5/ql4)
Mpy OP2; OP2 = 3000 h (0.75/q14)
Pac
SACH ans, 1; ans = 2400 H (1.125/q13)
In the above method, in order to calculate the accuracy of the result, the error generated by the result is equal to subtracting an LSB (percentile ). The following simple rounding method can reduce the error by 1/2.
Lt OP1
Mpy OP2
Pac
Add one, 14 (rounded up)
SACH ans, 1
The above procedures show that, no matter whether ANS is positive or negative, the error is l/2 LSB, where the value of the storage unit one is 1.
3.2 fixed-point addition
In the process of multiplication, programmers only need to adjust the decimal point in the calculation instead of considering overflow. Addition is a more complex process. First, the addition operation must be expressed with the same QPS. Second, the programmer may allow its results to be high enough to adapt to the increase of BITs, or be prepared to solve the overflow problem. If the operand is only 16 bits long, the result can be expressed by the number of double precision. The following example shows two ways to add 16 digits.
1. Retain 32-bit results
LAC OP1; (q15)
Add OP2; (ql5)
SACH Anshi; (16-bit high result)
SACL Anslo: (16-bit low)
2. Adjust the decimal point to retain 16 results
LAC OP1, 15; (q14 numbers are represented by acch)
Add OP2, 15; (q14 is represented by acch)
SACH ans; (q14)
The most likely problem with addition operations is that the operation results overflow. TMS320 provides a dedicated Command to check overflow BV. In addition, the overflow protection function can make the accumulators saturated with the largest integer or negative number when the cumulative result overflows. Of course, even so, the computing accuracy is greatly reduced. Therefore, the best way is to fully understand the basic physical process and select the number expression.
3.3 point Division
Generally, single-cycle division commands are not provided in general DSP Chips. Therefore, Division subprograms must be used. Binary Division is the inverse operation of multiplication. Multiplication includes a series of shift and addition methods, while division can be divided into a series of subtraction and shift. The following describes the process of division.
Set the accumulators to 8 bits and divide them by 10 and 3. The division process involves the gradual shift of divisor related to Division. Subtraction is performed in each step. If the Division can be reduced, bit is inserted into the operator.
(1) The lowest valid bits of the divisor are aligned with the highest valid bits of the divisor.
0000l0l0
-00011000
--------------------------------------
11110010
(2) because the subtraction result is negative, discard the subtraction result, move the devisor to the left and then subtract it.
00010100
-00011000
----------------------------------------
11111000
(3) The result is still negative. If the subtraction result is abandoned, the divisor shifts one digit left and then decreases.
00101000
-00011000
------------------------------------------
00010000
(4) If the result is positive, the subtraction result is shifted to the left and followed by 1 for the last subtraction.
00100001
-00011000
----------------------------------------
00001001
(5) The result is positive. Move the result to the left and Add 1 to get the final result. Four digits in height represent the remainder, and four digits in height represent the quotient.
00010011
That is, the quotient is 0011 = 3. The remainder is 0001 = 1.
TMS320 does not have a special division command, but subc can use the conditional subtraction command to complete the effective and flexible division function. The only limit for using this command is that the two operands must be positive. Programmers must first understand the features of their possible number operations, such as whether the operator can be expressed in decimal places and whether the accuracy of the operator can be calculated. Each of these considerations may affect how subc commands are used. In the following two cases, the TMS320C25 division program is provided.
(1) the numerator is smaller than the denominator.
Div_a:
Lt numera
Mpy denom
Pac
SACH temsgn; operator symbol
LAC denom
ABS
SACL denom; make the denominator positive
Zalh numera; positive molecule
ABS
Rptk 14
Subc denom; 15 cycles apart
SACL quot
LAC temsgn
Bgez A1; complete if the symbol is positive
Zac
Sub quot
SACL quot; if it is negative, the quotient is negative
A1: Ret
In this program, the numerator is in numera, the denominator is in denom, the operator exists in quot, and temsgn is the temporary storage unit.
(2) Accuracy of specified vendors
Div_ B:
Lt numera
Mpy denom
Pac
SACH temsgn; operator symbol
LAC denom
ABS
SACL denom; make the denominator positive
Lack 15
Add frac
SACL frac; Calculate the cyclic counter
LAC numera
ABS; make the molecule positive
RPT frac
Subc denom; except for 16 + frac cycles
SACL quot
LAC temsgn
Bgez B1; complete if the symbol is positive
Zac
Sub quot
SACL quot; if it is negative, the quotient is negative
B1: Ret
Similar to div_a, in this program, the numerator is in numera, the denominator is in denom, the operator is in quot, and temsgn is the temporary storage unit. Frac specifies the quotient precision. For example, if the quotient precision is q13, the value in the Frac Unit before the program is called should be 13.
4 fixed-point fast implementation of non-linear operations
In addition to basic addition, subtraction, multiplication, and Division operations, there are also many other non-linear operations, such as logarithm operations, square operations, exponential operations, and trigonometric operations, the methods for implementing these non-linear operations generally include: (1) Calling the library functions of the DSP compilation system; (2) Querying the table; (3) mixing. The three methods are described below.
1. Call the library function of the DSP compilation system
The C compiler of tms320c2x/c5x provides a wealth of library functions for running. These library functions include commonly used non-linear functions such as logarithm, square, trigonometric function, and exponent. In a C program (or in an assembler), you can directly call the function by using the same variable definition as the library function. For example, a library function defines the base-10 common logarithm log10 ():
# I nclude <math. h>
Double, log10 (Double X );
Call in C as follows:
Float X, Y;
X = 10.0;
Y = log10 (X );
From the above example, we can see that the input value required by the common logarithm log10 () in the library function is a floating point, and the return value is also a floating point. The operation accuracy is completely guaranteed. It is very convenient to directly call database functions, but it is difficult to be applied in real-time DSPs due to the large amount of computing.
2. look-up table method
To implement non-linear operations in real-time DSP Applications, we generally take appropriate measures to reduce the computational accuracy to increase the computing speed of the program. The lookup table method is the most commonly used method for Fast non-linear operations. To use this method, you must create a table based on the range and accuracy requirements of the independent variables. Obviously, the larger the input range, the higher the precision requirement, the larger the required table, that is, the larger the storage. The calculation required by the lookup table method is to determine the table address based on the input value. The corresponding value can be obtained based on the address, so the calculation workload is small. The look-up table method is applicable to the situations where a nonlinear function is a periodic function or the input value range of a known nonlinear function. Examples 1.12 and 1. 13 indicate the two situations respectively.
In Example 1.12, if the sine function y = cos (X) is known, create a 512-point table and describe the lookup method. Because the sine function is a periodic function, the function value ranges from-1 to + 1. It is more appropriate to use the lookup method. Since the range of q15 is between 1-to 32767/32768, in principle, the range of-1 to + 1 must be expressed in q14. However, in terms of convenience and overall accuracy, q15 is used for similar cases. At this time, + 1 is represented by 32767.
(1) the C language program that generates the 5L2 vertex value is as follows.
# Define n 512
# Define PI 3.14l59
Int sin_tab [5L2];
Void main ()
{
Int I;
For (I = 0; I <n; I ++) sin_tab = (INT) (32767 * sin (2 * pI * I/n ));
(2) query table
In fact, the table address is determined based on the input value. Set input x to 0 ~ Between 2 π, X corresponds to the address of the 512 vertex table: Index = (INT) (512 * X/2 π), then Y = sin (X) = sin_tab [Index] If X is expressed by q12 and 512/2 π is expressed by Q8 as 20861, the formula for calculating the address of the sine table is.
Index = (x * 20861l)> 20;
In Example 1.12, the base-2 logarithm is obtained using the lookup table method. The value range of the known independent variable is 0.5-1, and the range of independent variables must be evenly divided into 10 equal points. Create a table and describe how to look up the table.
(1) Table creation:
Y = log2 (x). Since X is between 0.5 and 1, Y is between-1 and 0. Both X and Y are expressed in q15. Since X is evenly divided into 10 segments, the 10 segments correspond to the range of input X, as shown in Table 3.2. If the logarithm value of each segment is the logarithm value of the first vertex, the logarithm value of the first segment in the table is y0 (q15) = (INT) (log (O.5) * 32768 ), the logarithm value of the second segment is Y1 (q15) = (INT) (log2 (0.55) * 32768), and so on, as shown in Table 3.2.
(2) query table:
When querying a table, the address of the table is calculated based on the input value. The calculation method is as follows:
Index = (x-16384) * 20)> 15;
Type, index is the address used for the query table. For example, if the input Person X = 26869 is known, the index is 6. Therefore, y =-10549.

Table 1.2 logtab0 10-point logarithm table
Address input value logarithm value (q15)
0 0.50-0.55-32768
1 0.55-0.60-28262
2 0.60-0.65-24149
3 0.65-0.70-20365
4 0.70-0.75-16862
5 0.75-0.80-13600
6 0.80-0.85-10549
7 0.85-0.90-7683
8 0.90-0.95-4981
9 0.95-1.00-2425

3. Mixed
(1) improve the accuracy of the table Search Method
The accuracy of the results obtained from the preceding method varies with the table size. The larger the table, the higher the accuracy, but the larger the storage. When the storage capacity of the system is limited and the precision requirement is high, the Table query method is not suitable. So can we increase the accuracy of non-linear operations with an appropriate increase in computing workload? The following describes a query table that combines a small number of operations to calculate the mixed law of non-linear functions. This method is applicable when the functions change monotonically within the range of input variables. The mixing method is based on the Table query to improve the accuracy when the input value is between two points in the table. An easy way to improve accuracy is to use the line approximation method, as shown in 1.1.
Figure 1.1 line approximation to improve accuracy"
Take the base 2 logarithm as an example (for example, 1.12 ). If the input value is set to X, the exact logarithm value is Y, and the two points in the Table value are always linear. If y' is used as the approximate value of Y, there are:
Y' = y0 + △y
Y0 is obtained from the table. Now, you only need to add △y on the basis of y0 in the table. The Calculation Method of △y is as follows: △y = (△x/△x0) △y = △x (△y0/△x0)
△Y0/△x0 is a constant value for each segment and can be directly searched as a table. In addition, the start value of each abscissa is required for calculation. This value can also be used as a table. This example contains three tables of 10 sizes, logtab0 is the table that stores the value of each start point, logtab1 is the table that stores the value of △y0/△x0 for each segment, and logtab2 is the table that stores the start value of x0 for each segment, tables logtab1 and logtab2 can be expressed in the following two arrays.
Int logtab1 [10] = {22529,20567, 18920,17517, 16308,
15255, 14330, 132.16,11680, 12124};/* △y0/△x0: q13 */
Int logtab2 [10] = {16384,18022, 19660,21299, 22938,
24576,26214, 27853,29491, 31130};/* x0: q15 */
To sum up, the method for calculating the logarithm using the mixed method can be summarized:
(1) According to the input value, calculate the table address: Index = (x-16384) * 20)> 15;
(2) query table: Y0 = logtab0 [Index];
(3) Calculate △x = x-logtab2 [Index];
(4) Calculate △y = (△x * logtab1 [Index])> 13;
(5) The calculated result is y = y0 + △y.
For example 1.13, if X = 0.54 is known, obtain log2 (X ).
The exact logarithm value of 0.54 is y = log2 (0.54) =-0.889.
The process of mixing valid logarithm values is:
(1) Calibration q15, calibration value x = 0.54*32768 = 17694;
(2) Table address Index = (x-16384) * 20)> 15 = 0;
(3) query table Y0 = logtab0 [0] =-32768;
(4) △x = x-logtab2 [0] = 17694-16384 = 1310;
(5) Calculate △y = (△xlogtab1 [0]> 13 = (13l0 * 22529l)> 13 = 3602
(6) The calculation result is y = y0 + △y =-32768 + 3602 =-29166.
Result Y is scaled to q15, and the result is parsed to-29166/32768 =-0.89, which indicates high accuracy.
(2) extend the range of Independent Variables
As mentioned above, The lookup method is more suitable for situations where the dynamic range of cyclic functions or independent variables is not too large. For non-linear functions such as logarithm, the range of input and function values is large. If the range of input values is large, it is difficult to create a table. So can we solve this problem better, that is, the table is too large, and the accuracy is relatively high? Next we will discuss a practical method.
If X is a number greater than 0.5, X can be expressed as follows:
X = m * 2e
Formula, 0.5 <= m <= 1.0, and E is an integer. The logarithm of X can be expressed:
Log2 (x) = log2 (M * 2e) = log2 (m) + log2 (2E) = E + log2 (m)
That is to say, the logarithm of X requires only the logarithm of M. However, since the logarithm of M is between 0.5 and 1.0, the method described above can be fully implemented. For example:
Log2 (10000) = log2 (0.61035*214) = log2 (0.61035) + 14 = 13.2877
It can be seen that if a number can be expressed as the above form in a simple way, it is more convenient to find the logarithm of any number. The tms320c2x/c5x Instruction Set provides a norm command used to normalize the numbers in ACC. This command is used to shift the number in the accumulators left until the maximum number is moved to the 30th bits in the accumulators. For example, the TMS320C25 program that standardizes the value 10000 is.
LAC #10000
SACL temp
Zarh temp
Lar AR1, # 0fh
RPT 14
Norm *-
After the preceding program is executed, AR1 = # 0eh, acch = 2000 (10 hexadecimal ). The above program processing a 16-bit integer x is actually Equivalent Transformation:
X = [(x * 2e)/32768] * 215-q
The value of register AR1 is the value of the high 16-bit acc of the 15-q accumulators. The value is between 16384 and 32768.
In Example 1.14, a base-2 C-point simulation program is implemented.
Int logtab0 [10] = {-32768,-28262,-24149,-20365,-16862,
-13600),-1o8.0,-7683,-4981,-2425};/* q15 */
Int logtab1 [10] ={ 122529,20567, 18920,175 L7, 16308,
15255, 14330, 132.16,11680, 12124};/* q13 */
Int logtab2 [10] = {16384, l8022, 19660,21299, 22938,
24576,26214, 27853,29491, 31130};/* q15 */
Int log2_fast (INT am)
{
Int point, point1;
Int index, x0, dx, Dy, Y;
Point = 0;
While (Am <16384) {point ++; AM = am <1 ;}/ * normalize am */
Point1 = (15-point-4) * 512;/* the input is Q4, and the output is Q9 */
Index = (Am-16384) * 20l)> 15;/* query table address */
DX = Am-logtab2 [Index];
DY = (long) dx * logtab1 [Index])> 13;
Y = (dy + longtab0 [Index])> 6;/* Q9 */
Y = point1 + Y;
Return (y );
}
In the above program, the input value am is expressed by Q4, and the output value is expressed by Q9. If the input and output Q values are different from those in the above program, the corresponding changes should be made.
The above discusses some basic problems involved in fixed-point operations on DSP chips, including number calibration, fixed-point simulation of DSP programs, the full-point operation of DSP chip and the fast implementation of fixed-point non-linear functions. Fully understanding these issues plays an important role in implementing DSP algorithms with a fixed-point chip.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.