Collection
zero Useful+1
zero

Fixed point calculation

Fixed-point operation
synonym Fixed-point operation (fixed-point operation) generally specified point calculation
In a given rectangular coordinate system, points whose coordinates are all integers are called whole points; The group composed of all whole points is called spatial grid. The operations in the spatial grid are called Fixed-point operation In addition to fixed-point operations, of course, floating point operations.
Chinese name
Fixed point calculation
Foreign name
fixed-point calculation
Alias
Fixed-point operation
Discipline
Mathematics, physics

brief introduction

Announce
edit
Fixed point number: Generally speaking, the number with a fixed decimal point. Taking RMB as an example, we often talk about 123. 45¥,789。 34 ¥ and so on. By default, there are two decimal places after the decimal point, namely, angle and minute. If the decimal point is in front of the most significant digit, such a number is called the fixed point number of pure decimal, such as 0. 12345,0。 78934, etc. If the decimal point is behind the least significant digit, such number is called the fixed point number of pure integer, such as 1234578934.
A method of representing numbers in which the decimal point is fixed. When representing a number, the decimal point is usually fixed before the highest position of the number, so that the absolute value of the number (i.e. mantissa or digit) is less than 1, which is in the form of number sign and mantissa. For integer numbers, the decimal point is fixed at the lowest position of the number. The number expressed by fixed-point representation is called fixed-point number. [1]
Floating point number: Generally speaking, a number with an unfixed decimal point. The easier way to understand it is to consider the following Scientific notation , take our numbers above for example, 123.45, which can be written in the following forms: 12.345x1011; 2345x1020; 12345x103 In order to represent a number, the position of the decimal point can change, that is, the decimal point is not fixed.
In order to ensure the speed of signal calculation, assembly language and fixed-point number are mostly used in the language of data medium, small computers and microcomputers when they are used for simulation calculation or as special control computers. When calculating with fixed point numbers, it is required that all quantities involved in the operation, whether the initial value or the intermediate result, or the value of the final result, must be strictly limited to the (- 1,+1) interval, or overflow will occur during the operation.

Fixed point operation of DSP chip

Announce
edit
At a fixed point DSP chip Medium, using Number of fixed points For numerical operation, the operands are generally expressed as integer numbers. The maximum representation range of an integer depends on DSP chip The given word length is generally 16 bits or 24 bits. Obviously, the longer the word length is, the larger the range of numbers can be expressed, accuracy The higher. Unless otherwise specified, this book takes 16 bit word length as an example. The number of DSP chips is 2 Complement Formal representation. Each 16 digit number uses a sign bit to represent the positive and negative of the number, 0 means the value is positive, and 1 means the value is negative. The remaining 15 bits represent the size of the value.
therefore , binary number 001000000000011b=8195 Binary number 1111111111111100b=- 4 pairs DSP chip In other words, the number participating in numerical operation is a 16 bit integer number. But in many cases, the number in the process of mathematical operation is not necessarily an integer. Then, how does the DSP chip handle decimal What about? It should be said that the DSP chip itself is powerless. Does that mean that the DSP chip can't handle all kinds of decimal What about? Of course not. The key is that the programmer determines a number decimal point Which of the 16 bits is it. This is the scaling of numbers. By setting different positions of the decimal point in 16 digits, different sizes and accuracy Is a decimal of. The fixed number is marked with Q representation and S representation. Table 3.1 lists 16 Q and S representations of a 16 digit number and what they can represent decimal system Range of values. It can be seen from Table 3.1 that for the same 16 digit number, if the decimal point is set at different positions, the number it represents will be different. For example: 16 Base The number 2000H=8192. Q0 is used to represent the hexadecimal number 2000H=0.25, and Q15 is used to represent it. But for DSP chips, the processing method is completely the same. It can also be seen from Table 3.1 that different Q not only has different ranges, but also accuracy Not the same. The larger the Q, the smaller the numerical range, but accuracy Higher; On the contrary, the smaller the Q, the larger the numerical range, but the lower the accuracy. For example, the value range of Q0 is - 32768 to+32767 accuracy Is 1, while the value range of Q15 is - 1 to 0.9999695, and the precision is 1/32768=0.00003051. Therefore, for Number of fixed points For, the range of values is accuracy It is a pair of contradictions. If a variable wants to represent a relatively large numerical range, it must sacrifice precision; If you want to improve the accuracy, the number representation range will be reduced accordingly. At the actual fixed point algorithm In order to achieve the best performance, this must be fully considered. Floating point number And Number of fixed points The conversion relationship of can be expressed as: Floating point number (x) Convert to Number of fixed points (): fixed-point number () is converted to floating point number (x): for example, floating point number x=0.5, calibration Q=15, which means rounding down. On the contrary, a Number of fixed points 16384, which Floating point number 16384 × 2-15=16384/32768=0.5.

From floating point to fixed point

Announce
edit
When writing DSP simulation algorithm, for convenience, we usually use high-level language (such as C language). The variables used in the program generally include both integer numbers and Floating point number As shown in Example 3.1, the variable i in the program is an integer, while pi is Floating point number Hamwindow is a floating point array.
Example 3.1 256 point Hamming window calculation
inti;
floatpi=3.14159;
floathamwindow[256];
for(i=0;iQy, addition / subtraction The calibration value of result z is Qz, then
z=x+yÞ
=Þ
So the fixed point addition It can be described as:
intx,y,z;
longtemp;
Temp=y>(Qx - Qz)), if Qx ≥ Qz
z=(int)(temp>2)=29491;
Since the Q value of z is 13, the fixed point value z=29491 is floating-point The value z=29491/8192=3.6.
Example 3.3 Fixed point subtraction
Let x=3.0, y=3.1, then the floating point operation result is z=x-y=3.0-3.1=- 0.1;
Qx=13, Qy=13, Qz=15, then the fixed-point subtraction is:
x=24576; y=25295;
temp=25395;
temp=x-temp=24576-25395=-819;
Because QxQy, addition The calibration value of z is Qz, so the fixed-point addition is:
intx,y;
longtemp,z;
Temp=y>(Qx Qz), if Qx ≥ Qz
Z=temp32767, so
Qx=1, Qy=0, Qz=0, then the fixed point addition For:
x=30000; y=20000;
temp=20000>1=35000;
Since 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 addition or the result of addition exceeds the 16 bit representation range, if the programmer can understand this situation in advance and needs to ensure the operation accuracy Must maintain 32-bit results. If 16 digit If the number of bits exceeds 16, an overflow occurs. If proper measures are not taken, the data overflow will lead to serious deterioration of the calculation accuracy. General fixed point DSP chip Both are equipped with overflow protection function. When the overflow protection function is effective, once overflow occurs, the result of accumulator ACC is the maximum saturation value( Overflow Is 7FFFH, Underflow 8001H) to prevent overflow accuracy The purpose of serious deterioration.
3.2.2 C language fixed-point simulation of multiplication
set up floating-point The expression of multiplication is:
floatx,y,z;
z=xy;
Assuming that the scaling value of x after statistics is Qx, the scaling value of y is Qy, and the scaling value of product z is Qz, then
z=xyÞ
=Þ
=
So the multiplication of fixed-point representation is:
intx,y,z;
longtemp;
temp=(long)x;
z=(temp×y)>>(Qx+Qy-Qz);
Example 3.5 Fixed point multiplication
Let x=18.4, y=36.8, then floating-point The operation value is z=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;
Since the scaling value of z is 5, the fixed point z=21666 is the floating point z=21666/32=677.08.
3.2.3 Division C language Fixed-point simulation
Set floating point division The expression of the operation is:
floatx,y,z;
z=x/y;
Assume that after statistics Dividend The scaling value of x is Qx, the scaling value of divisor y is Qy, and the scaling value of quotient z is Qz, then
z=x/yÞ
=Þ
So the division of fixed-point representation is:
intx,y,z;
longtemp;
temp=(long)x;
z=(temp
constintlength=180
voidfilter(intxin[],intxout[],intn,floath[]);
staticfloath[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,0.01218354};
staticintx1[length+20];
voidfilter(intxin[],intxout[],intn,floath[])
{
inti,j;
floatsum;
for(i=0;i
constintlength=180;
voidfilter(intxin[],intxout[],intn,inth[]);
staticinth[19]={399,-296,-945,-1555,-1503,-285,2112,5061,7503,8450,
7503,5061,2112,-285,-1503,-1555,-945,-296,399};
staticintx1[length+20];
voidfilter(intxin[],intxout[],intn,inth[])
{
inti,j;
longsum;
for(i=0;i>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.