Reprinted from: http://bbs.21ic.com/icview-841266-1-1.html
In the application of DSP, in fact, hardware is generally not a problem, the main is the software, is the algorithm! The following about the operation of the essence of DSP hope that some value!
A DSP fixed-point arithmetic operation
1-Count Calibration
In the fixed-point DSP chip, the fixed-point number is used for numerical operation, and its operand is generally represented by the integer number. The maximum representation range for an integer number depends on the length of the DSP chip given, typically 16-bit or 24-bit. Obviously, the longer the word length, the greater the range of numbers that can be expressed, and the higher the accuracy. If not specifically stated, this book is 16-bit word length as an example. The number of DSP chips is expressed in 2 complement form. Each 16-digit number uses a sign bit to represent the positive or negative of the number, 0 for the value to be positive, and l to indicate a negative value. The remaining 15 bits represent the size of the numeric value. So
Binary number 0010000000000011b=8195
Binary number 1111111111111100b=-4
For a DSP chip, the number of participating numeric operations is the integer number of 16 bits. In many cases, however, the numbers in the counting process are not necessarily integers. So, how does the DSP chip deal with decimals? It should be said that the DSP chip itself is powerless. So is it that the DSP chip can not handle a variety of decimals it? Of course not. The key to this is that the programmer determines which of the 16 digits the decimal point of a number is. This is the calibration of the number. By setting the decimal point to a different position in the 16-digit number, you can represent the decimals of different sizes and different precision.
The calibration of the number has two types: Q notation and S notation.
Table 1.1 shows a 16-digit 16 Q representation, S representation, and the range of decimal values that they can represent. As can be seen from table 1.1, the same 16-digit number, if the decimal point is set to a different position, it represents a different number. For example
16 binary number 2000h=8192, denoted by Q0
16 binary number 2000h=0.25, denoted by Q15
But for the DSP chip, the processing method is exactly the same. As can be seen from table 1.1, the numbers represented by different q are not only different in scope, but also differ in accuracy. The larger the Q, the smaller the range, but the higher the precision; Conversely, the smaller the Q, the larger the range of values, but the lower the accuracy. For example, the range of values for Q0 is 132,768 to +32767, with a precision of 1, and a Q15 value of 1 to 0.9999695 with a precision of 1/32768=0.00003051. Therefore, for fixed-point numbers, the range of values and precision is a pair of contradictions, a variable to be able to represent a larger range of values, it must be at the expense of precision, and to improve the accuracy, the number of the representation of the corresponding reduction in the range. In the actual fixed-point algorithm, in order to achieve the best performance, this must be fully taken into account. The transition between floating-point numbers and fixed-points can be expressed as:
floating point (x) converted to fixed-point number (XQ): xq= (int) x* 2Q
Fixed-point number (XQ) converted to floating point (x): x= (float) xq*2-q
For example, floating-point number x=0.5, calibration q=15, then fixed-point numbers xq=l0.5*32768j=16384, the type of LJ in the next rounding.
Conversely, a fixed-point number 16384 is represented by q=15, and its floating point is 16384*2-15=16384/32768=0.5.
In order to reduce the truncation error when the floating-point number is converted to fixed-point, 0.5 can be added before rounding.
Table 1.1 Q Representation, s representation and range of values
Q indicates that s represents a decimal number representation range
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. High-level language: From floating point to fixed point
When we write the DSP simulation algorithm, in order to facilitate, generally use high-level language (such as C language) to write the simulation program. The variables used in the program generally have both integer and floating-point numbers.
As in example 1.1 the variable I in the program is the integer number, and pi is a floating-point, Hamwindow is a floating-point group.
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 use the above program with some kind of sufficient DSP chip to achieve, then we need to rewrite the above program as a DSP chip assembly language Program. For the convenience of DSP program debugging and the algorithm performance of analog fixed-point DSP, it is generally necessary to rewrite the high-level language floating-point algorithm into high-level language fixed-point algorithm before writing DSP assembler program. Here we discuss the fixed-point implementation of basic arithmetic operations.
2.1 The C language fixed point of addition/subtraction operation
The expression for floating-point addition operations is:
float x, y, Z;
Z=x+y;
The most important point when converting floating-point addition/subtraction to fixed-point addition/subtraction is the need to ensure the calibration of two operands temp=x+temp;
Z=temp>> (QX-QZ), if QX>=QZ
z=temp<< (QZ-QX), if QX<=QZ
Example 1.4 results more than 16 bits of fixed-point addition
Set x=l5000,y=20000, the floating-point arithmetic value is z=x+y=35000, obviously z>32767, so qx=1,qy=0,qz=0, the fixed-point addition is:
x=30000;y=20000;
temp=20000<<1=40000;
temp=temp+x=40000+30000=70000;
z=70000l>>1=35000;
Because Z has a Q value of 0, the fixed-point value z=35000 is a floating-point value, where z is a long integer number.
When the result of an addition or addition exceeds the 16-bit representation range, a 32-bit result must be maintained if the programmer is aware of the situation beforehand and needs to maintain the precision of the operation. If the program is based on a 16-digit number, then more than 16 bits is actually an overflow. If appropriate measures are not taken, data overflow can lead to a severe deterioration of the computational accuracy. General fixed-point DSP chip has no overflow protection function, when overflow protection function, once overflow, the result of accumulator ACC is the maximum saturation value (overflow is 7FFFH, underflow is 8001H), so as to prevent overflow caused by severe deterioration of precision.
2.2 C-language fixed-point simulation of multiplication operation
The expression for a floating-point multiplication operation is:
float x, y, Z;
Z=xy;
Assuming that the calibration value of x is qx,y after the statistic is qy, and the calibration value of product Z is QZ, then
Z=xy
zq*2-qz= (XQ*2-QX) * (yq*-qy) =xq*yq*2-(qx+qy)
Zq= (XQYQ) 2qz-(qx+qy)
So the multiplication of the fixed-point representation is:
int x, y, Z;
Long temp;
temp= (long) x;
z= (temp*y) >> (QX+QY-QZ);
Example 1.5 fixed-point multiplication.
Set x=18.4,y=36.8, the floating-point arithmetic 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 is the floating-point z=21666/32=677.08.
2.3 C-Language fixed point of division operation
The expression for a floating-point division operation is:
float x, y, Z;
z=x/y;
Assuming that after the statistical divisor x is the calibration value of QX, divisor y is qy, 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 of the 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.
Set x=18.4,y=36.8, floating-point arithmetic value is z=x/y=18.4/36.8=0.5;
According to the above section, get qx=10,qy=9,qz=15;
So there are
z=18841,y=18841;
temp= (long) 18841;
Z= (18841l<< (15-10+9)/18841=308690944l/18841=16384;
Because the calibration value of quotient z is 15, the fixed-point z=16384 is a floating-point z=16384/215=0.5.
2.4 Determining the Q value of the program variable
In the example described in the previous sections, the Q value is well-defined when the value from floating point to point is known because the values of x, Y, Z are all. In the actual DSP application, the program participates in the operation is the variable, then how to determine the floating point program in the variable Q value?
From the previous analysis, it can be known that the Q-value of a variable is actually determined by the dynamic range of the variable, and the Q value is determined.
The absolute value of the set variable is |max|, and note that |max| must be less than or equal to 32767. Take an integer n to satisfy the 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 is between 1 and +1, which is |max|<1, so n=0,q=15-n=15.
Now that the |max| of a variable can be determined by its Q-value, how is the |max| of the variable determined?
In general, there are two ways to determine the |max| of a variable. One is the theory analysis method, the other is the statistical analysis method.
1. Theoretical analysis method
The dynamic range of some variables can be determined by theoretical analysis.
For example:
(1) Trigonometric functions. Y=sin (x) or Y=cos (x), known by trigonometric functions, |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) =∑h (k) x (n-k), set ∑|h (k) |=1.0, and X (n) is the analog signal 12-bit quantization value, that is, |x (n) |<=211, then |y (n) |<=211.
(4) theory has proved that in the program design of autocorrelation Linear Predictive Coding (LPC), the reflection coefficient Ki satisfies the following inequalities: |ki|<1.0,i=1,2,...,p,p is the order of LPC.
2. Statistical analysis method
For variables that cannot be defined in theory, statistical analysis is used to determine its dynamic range. The so-called statistical analysis, is to use enough input signal sample value to determine the dynamic range of variables in the program, where the input signal on the one hand to have a certain number, on the other hand must be as far as possible to deal with a variety of situations. For example, in the speech signal analysis, the statistical analysis must be to set a sufficient number of voice signal samples, and in the collected voice sample value, should contain as many situations as possible. such as the size of the volume, the type of sound (male, female, etc.). Only in this way can the results of statistics be typical.
Of course, statistical analysis is not likely to involve all possible situations, therefore, the results of the statistics can be taken in the design of some protection measures, such as the appropriate sacrifice of some precision, the Q value of the statistical value is slightly larger, using the DSP chip provides overflow protection function.
2.5 Examples of C programs for floating-point to fixed-point transformations
In this section we use an example to illustrate how C programs move from floating-point to fixed-point.
This is a speech signal (0.3~3.4khz) low-pass filtering of the C language program, low-pass filter cutoff frequency of 800Hz, the filter uses 19 points of limited impact response FIR filter. The voice signal is sampled at 8kHz, and each voice sample is stored in the Insp.dat file as a 16-bit integer.
Example 1.7 speech signal 800Hz 19-point Fir Low-pass filter C language floating-point program.
#i nclude
const int length=180/* Voice frame length is 180 point =22.5ms@8khz sample */
void filter (int xin[],int xout[],int n,float h[]);/* Filter subroutine Description */
/*19 Point Filter coefficients */
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 filtering floating PIP program */
void filter (int xin[],int xout[],int n,float h[])
{
int i,j;
float sum;
for (i=0;i
for (i=0;i
{
sum=0.0;
for (j=0;j
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");/* Enter a voice file */
Fp2=fopen (Outsp.dat, "WB"); */* Filtered voice file */
= 0;
while (feof (FP1) ==0)
{
+ +;
printf ("=%d\n",);
for (i=0;i
Filter (INDATA,OUTDATA,19,H);/* Call Low-pass filter subroutine */
for (i=0;i
}
Fcloseall ();/* Close file */
return (0);
}
Example 1.8 speech signal 800Hz L9 point fir low-pass filtering C language fixed-point program.
# include
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];
/* Low-pass filter fixed-point subroutine */
void filter (int xin[],int xout[],int n,int h[])
int i,j;
Long sum;
for (i=0;i
for (i=0;i<1ength;i++)
sum=0;
for (j=0;j
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 floating point.
?
3 DSP fixed point arithmetic operation Point
The digital representation of the DSP chip is based on the 2 complement representation. Each 16-digit number is represented by an L-sign, I-Integer, and 15-i decimal digits. So:
A value of 00000010.10100000 represents: 21+2-1+2-3=2.625
This number can be represented by the Q8 format (8 decimal places), which represents a range of values from 128 to +l27.996, and a decimal precision of Q8 fixed-point number is 1/256=0.004. Although special cases such as dynamic range and precision are required, mixed notation must be used. However, it is more common to work with decimals in Q15 format or integers represented in Q0 format. This is especially true for signal processing algorithms that are primarily multiplicative and additive, with decimals multiplied by decimals, integers multiplied by integers. Of course, there may be overflow in the accumulation of overtime, in which case the programmer should be aware of the physical processes in mathematics to pay attention to possible overflow situations.
Here we discuss the DSP fixed-point operation of multiplication, addition and division, and the assembler takes tms320c25 as an example. 3.1 Fixed-point multiplication
Two fixed-point numbers can be divided into the following three cases:
1. Decimal by decimal
Example 1.9
Q15*q15=q30
0.5*0.5=0.25
0.100000000000000;q15
* 0.100000000000000;q15
--------------------------------------------
00.010000000000000000000000000000=0.25;q30
Two decimal Q15 are multiplied to get a Q30 decimal, that is, two sign bits. In general, the number of full-precision numbers obtained by multiplying does not have to be preserved, but only 16 bits of single precision are reserved. As a result of multiplying the high 16 bit less than 15 bits of small data degrees, in order to achieve 15-bit accuracy, the product can be shifted to the left one bit, the following is the multiplication of the TMS320C25 program:
LT OP1;
op1=4000h (0.5/q15)
MPY op2;op2=4000h (0.5/QL5)
Pac
SACH ans,1;ans=2000h (0.25/q15)
2. integers multiplied by integers
Example 1.10
Q0*q0=q0
17* (-5) =-85
0000000000010001=l7
*1111111111111011=-5
-------------------------------------------
11111111111111111111111110101011=-85
3. Mixed notation
In many cases, the representation between Q0 and Q15 must be used in order to satisfy both the dynamic range of numerical value and the precision of operation. For example, the value 1.2345, obviously Q15 can not be expressed, and if Q0, the closest number is 1, the accuracy is not guaranteed. Therefore, the best representation of the number 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, so the multiplication of two Q14 numbers is not greater than 4.
Generally, if the integer bit of one number is the I bit, the decimal place is J bit, the integer bit of the other number is M bit, and the decimal place is n, then the product of these two numbers is (i+m) bit integer and (j+n) bit decimal place. The highest possible 16-bit precision for this product is (i+m) integer digits and (15-i-m) decimal digits.
However, if you know the dynamic range of the number in advance, you can increase the precision of the number. For example, the programmer knows that the product above is not greater than 1.8, and that the product can be expressed as a Q14 number, rather than a theoretical best case Q13.
The TMS320C25 procedure for example 3.11 is as follows:
LT OP1;OP1 = 6000H (1.5/QL4)
MPY op2;op2 = 3000H (0.75/q14)
Pac
SACH ans,1;ans=2400h (1.125/q13)
The above method, in order to achieve the accuracy of the results of the position, the result of the error equivalent to subtract an LSB (the lowest bit). Using the simple method below, the error can be reduced by one-second.
LT OP1
MPY OP2
Pac
ADD one,14 (rounding up)
SACH ans,1
The above procedure shows that, regardless of whether ans is positive or negative, the error generated is L/2 LSB, where the value of the storage unit one is 1.
3.2 Fixed-point addition
In the process of multiplication, the programmer can simply adjust the decimal point of the operation regardless of overflow. And addition is a more complicated process. First, the addition operation must be represented by the same Q point, and secondly, the programmer either allows the result to be sufficiently high to accommodate the bit growth, or must be prepared to resolve the overflow problem. If the operand is only 16 bits long, the result can be expressed as a double precision number. The following examples illustrate two ways to add 16-digit numbers.
1. Retain 32-bit results
LAC OP1; (Q15)
ADD OP2; (QL5)
SACH Anshi; (high 16-bit result)
SACL Anslo: (Low 16-bit result)
2. Adjust the decimal point to preserve 16-bit results
LAC op1,15; (Q14 number is represented by ACCH)
ADD op2,15; (Q14 number is represented by ACCH)
SACH ANS; (Q14)
The most likely problem with addition operations is that the results of the operation overflow. TMS320 provides dedicated instruction BV to check for overflow, in addition, the overflow protection function allows the accumulator to saturate to the largest integer or negative number when the accumulated result overflows. Of course, even so, the computational accuracy is greatly reduced. Therefore, the best way is to fully understand the basic physical processes and pay attention to the expression of the number of choices.
3.3 Fixed-point division
In a general-purpose DSP chip, the single-cycle division instruction is generally not provided, which must be achieved by means of a program. Binary Division is the inverse of multiplication. Multiplication consists of a series of shifts and additions, and division can be decomposed into a series of subtraction and displacement. Let's illustrate the process of division implementation.
The accumulator is set to 8 bits, and the division operation is 10 divided by 3. The process includes a gradual shift of the divisor associated with the division, which is subtracted at each step, and if it can be reduced, the bit is inserted into the quotient.
(1) The least significant bit of the divisor aligns to the most significant bit of the dividend.
0000l0l0
-00011000
--------------------------------------
11110010
(2) because the subtraction result is negative, discard the subtraction result, the divisor will be shifted left one bit, then minus.
00010100
-00011000
----------------------------------------
11111000
(3) The result is still negative, the subtraction result is discarded, the divisor is shifted to the left one bit, and then minus.
00101000
-00011000
------------------------------------------
00010000
(4) The result is positive, the subtraction result is shifted to the left one after plus 1 for the last time minus.
00100001
-00011000
----------------------------------------
00001001
(5) The result is positive, and the result is shifted left one plus 1 to the final result. The high 4 bits represent the remainder, and the lower 4 bits represent the quotient.
00010011
That is, the quotient is 0011 = 3. The remainder is 0001 = 1
TMS320 does not have a special division instruction, but the use of conditional reduction instruction SUBC can be used to achieve effective and flexible in addition to * *. The only limit for using this directive is that two operands must be positive. The programmer must know in advance the characteristics of its possible operands, such as whether the quotient can be expressed in decimals and whether the quotient's precision can be computed. Each of these considerations can affect how the SUBC directive is used. Below we give two different cases of TMS320C25 Division program.
(1) Numerator less than denominator
Div_a:
LT NUMERA
MPY Denom
Pac
SACH TEMSGN; The sign of the dealer
LAC Denom
Abs
SACL denom; make denominator positive
Zalh NUMERA; the molecule is positive.
Abs
RPTK 14
SUBC Denom; In addition to loops 15 times
SACL QUOT
LAC TEMSGN
Bgez A1; If the symbol is positive, complete
ZAC
SUB QUOT
A SACL QUOT; if negative, the quotient is negative.
A1:ret
In this program, the numerator is in the Numera, the denominator is in the denom, the quotient exists quot, and the TEMSGN is the staging unit.
(2) The accuracy of the specified quotient
Div_b:
LT NUMERA
MPY Denom
Pac
SACH TEMSGN; The sign of the dealer
LAC Denom
Abs
SACL Denom; To make the denominator positive
LACK 15
ADD FRAC
SACL FRAC; calculation loop counter
LAC NUMERA
ABS; To make molecules positive
RPT FRAC
SUBC Denom; In addition to cyclic 16+FRAC times
SACL QUOT
LAC TEMSGN
Bgez B1; If the symbol is positive, complete
ZAC
SUB QUOT
A SACL QUOT; if negative, the quotient is negative.
B1:ret
As with Div_a, in this program, the numerator is in the Numera, the denominator is in Denom, the quotient exists quot, and the TEMSGN is the staging unit. Frac the accuracy of the specified quotient, such as the quotient of the accuracy of Q13, the calling program before the Frac cell value should be 13.
4 fixed-point fast implementation of nonlinear operation
In numerical operations, in addition to the basic subtraction operations, there are many other non-linear operations, such as logarithmic operations, root operations, exponential operations, trigonometric functions, and so on, the methods of implementing these nonlinear operations are generally: (1) Call the library function of DSP compiling system, (2) Check table method, (3) mixing method. Here are three ways to do this separately.
1. Call the library function of the DSP compilation system
The tms320c2x/c5x C compiler provides a richer run support library function. In these library functions, such as logarithm, root, trigonometric function, exponent and other commonly used nonlinear functions are included. In a C program (which is also available in assembler), it can be called directly with the same variable definition as the library function. For example, in a library function, the common logarithm log10 () with base 10 is defined:
#i nclude
DOUBLE,LOG10 (double x);
Called in the C program as follows:
float x, y;
x=10.0;
Y=LOG10 (x);
As can be seen from the above example, the common logarithm log10 () in the library function requires a floating-point number, the return value is also a floating-point number, and the precision of the operation can be guaranteed. It is very convenient to call the library function directly, but it is difficult to get the application in real-time DSP because of the large computational capacity.
2. Table-checking method
In the application of real-time DSP, it is generally necessary to reduce the operation precision to improve the operation speed of the program. The tabular method is the most common method for fast implementation of nonlinear operations. Using this method, a table must be made according to the range and accuracy requirements of the independent variable. Obviously the larger the scope of the loss, the higher the accuracy requirement, the larger the required table, that is, the greater the amount of storage. The calculation required to evaluate the tabular method is to determine the address of the table according to the input value, and the corresponding value can be obtained according to the address, thus the computation amount is small. The tabular method is more suitable for the nonlinear function is a periodic function or a known nonlinear function input value range of two cases, example 1.12 and Example 1. 13 The two cases are described separately.
Example 1.12 known sine function y=cos (x), make a 512-point table, and explain the method of table check. Since the sine function is a periodic function, the value of the function is between 1 and +1, and it is more appropriate to use the table-checking method. Since the Q15 range is between 1 and 32767/32768, in principle 1 to +1 of the range must be expressed in Q14. But generally from the convenience and overall precision considerations, the similar situation is still used Q15, at this time +1 with 32767来 expression.
(1) The C language program that generates the 5L2 point value is shown below.
#define N 512
#define PI 3.14l59
int SIN_TAB[5L2];
void Main ()
{
int i;
for (i=0;i
(2) Check list
Tabular actually determines the address of the table based on the value of the loser. Set the input x between 0~2π, then x corresponds to the address of the 512-point table is: index= (int) (512*x/2π), then Y=sin (x) =sin_tab[index] If X is indicated by the Q12 fixed point number, 512/2π is represented by Q8 as 20861, The formula for calculating the address of the sine table is.
index= (x*20861l) >>20;
Example 1.12 using the table method to find the logarithm of 2, known as the value range of the independent variable is 0.5-1, it is required to divide the range of the independent variable into 10 equal. Try making this form and explaining how to look up the table.
(1) Make a table:
Y=LOG2 (x), since x is between 0.5 and 1, so y is between 1 and 0, and X and Y can be represented by Q15. Since x is evenly divided into 10 segments, the 10 segment corresponds to the range of input x as shown in Table 3.2. If the pairs of values in each segment take the 1th pair of values, the logarithm of the first paragraph in the table is y0 (Q15) = (int) (log (o.5) *32768), the logarithm value of the second paragraph is y1 (Q15) = (int) (LOG2 (0.55) *32768), and so on, As shown in Table 3.2.
(2) Check table:
When tabular, the address of the table is calculated based on the value of the input, and the calculation method is:
Index= ((x-16384) *20) >>15;
, index is the address used to look up the table. For example, it is known that the loser x=26869, then index=6, therefore, y=-10549.
Table 1.2 logtab0 10-point tables
Address input value pair 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. Hybrid method
(1) Improve the accuracy of table-checking method
The accuracy of the results obtained by the above method varies with the size of the table, the larger the table, the higher the precision, but the greater the storage capacity. When the storage capacity of the system is limited and the precision requirement is higher, the method of checking table is not suitable. Is it possible to improve the precision of the nonlinear operation with the appropriate increase of the computational capacity? The following is a hybrid method for calculating nonlinear functions with a table of tables combined with a small number of operations, which is suitable for a monotone variation of functions in the range of input variables. The Hybrid method is based on a table-checking method to improve the accuracy of the input value when it is between the table two points. An easy way to improve accuracy is to use the polyline approximation method, which is shown in 1.1.
Figure 1.1 Line approximation method for improving accuracy "
Still try to take the logarithm of the base 2 as an example (example 1.12). Set the input value to x, the exact logarithmic value is Y, a straight line between two points of the table value, and Y ' as the approximate value of y:
Y ' =y0+ y
Among them, y0 is evaluated by the table. Now it is only possible to add Y on the basis of the look-up table for y0. Y is calculated as follows: y= (x/x0) y= x (y0/x0)
Where y0/x0 is a constant value for each paragraph, it can be directly found in a table. It is also possible to calculate the starting value for each section of the horizontal axis at this time, and this value can be used as a table. This is a total of three sizes are 10 tables, respectively, to store each beginning of the value of the table Logtab0, store each segment y0/x0 value of the table LOGTAB1 and store each paragraph input starting value x0 table logtab2, table logtab1 and table logtab2 can be represented by the following two arrays.
int logtab1[10]={22529,20567,18920,17517,16308,
15255,14330,13511,12780,12124};/* y0/x0:q13*/
int logtab2[10]={16384,18022,19660,21299,22938,
24576,26214,27853,29491,31130};/*x0:q15*/
In summary, the method of calculating numerical value by mixing method can be summed up as:
(1) According to the value of the output, calculate the table address: index= ((x-16384) *20) >>15;
(2) Check the table to get Y0=logtab0[index];
(3) calculation X=x-logtab2[index];
(4) Calculation y= (X*logtab1[index]) >>13;
(5) The calculated result y=y0+ y.
Example 1.13 known x=0.54, seeking log2 (x).
The exact pair value of 0.54 is y=log2 (0.54) =-0.889.
The process of solving the numerical value by mixing method is:
(1) Calibration Q15, calibration value x=0.54*32768=17694;
(2) Table Address index= ((x-16384) *20) >>15=0;
(3) Check the table to get y0=logtab0[0]=-32768;
(4) Calculating x=x-logtab2[0]=17694-16384=1310;
(5) Calculation y= (xlogtab1[0]>>13= (13l0*22529l) >>13=3602
(6) Calculation result y=y0+ y=-32768+3602=-29166.
The results of Y are Q15, and the floating-point number is -29166/32768=-0.89 and the visible precision is higher.
(2) Expanding the range of independent variables
As mentioned above, it is not too large to compare the dynamic range of a periodic function or an independent variable to a table-checking method. For nonlinear functions like logarithms, the range of values of input and function varies greatly. It is difficult to make a table if the input value varies in a large range. Then can better solve this problem, that is, the inconvenience table is too large, but also to get relatively high precision? Let's discuss a practical approach.
If x is a number greater than 0.5, X can be represented in the following form:
x=m*2e
, 0.5<=m<=1.0,e is an integer. Then the logarithm of X can be expressed as:
LOG2 (x) =log2 (m*2e) =log2 (m) +log2 (2e) =e+log2 (m)
That is to say, the logarithm of x is actually only required m logarithm, and because the value of m between 0.5 and 1.0, the method described above is fully achievable. For example:
LOG2 (10000) =log2 (0.61035*214) =log2 (0.61035) +14 =13.2877
Therefore, if a number can be represented as the above form in a relatively simple way, then it is convenient to find the logarithm of any size number. The tms320c2x/c5x instruction set provides a directive norm that normalizes the number in ACC by moving the number in the accumulator to the left, until the highest bit of the number is moved to the 30th position of the accumulator. For example, the TMS320C25 program that normalizes the value 10000 is.
LAC #10000
SACL TEMP
Zalh TEMP
LAR AR1, #0FH
RPT 14
NORM *-
After the above procedure is executed, ar1= #0eH, acch=2000 (10 binary). The process of processing a 16-bit integer x is essentially an equivalent transformation:
x=[(x*2e)/32768]*215-q
Where register AR1 contains a value of 15-Q accumulator ACC high 16 bits contains a value of x.2q, whose value is between 16384 and 32768.
Example 1.14 implements a C-fixed-point simulator with base 2 logarithm.
int logtab0[10]={-32768,-28262,-24149,-20365,-16862,
-13600), -1o549,-7683,-4981,-2425};/*q15*/
int logtab1[10]={122529,20567,18920,175l7,16308,
15255,14330,13511,12780,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;}/* normalizes Am */
point1= (15-point-4) *512;/* input is Q4, output is q9*/
Index= ((Am-16384) *20l) >>15;/* Find 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 procedure, the input value is expressed as Q4, the output is expressed in Q9, if the input and output Q value is different from the above program, it should be modified accordingly.
In this paper, some basic problems involved in fixed-point operation of DSP Chip are discussed, including: Number calibration, fixed-point simulation of DSP program, Foot-point arithmetic of DSP chip and fast realization method of non-linear function of fixed point. Fully understanding these problems is very important for realizing DSP algorithm with fixed-point chip.
Fundamentals of DSP Data operation