Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
游客
帖子: n/a
|
![]()
by Darel A. Linebarger and Thomas A. Bryan
The new Fixed-Point Toolbox provides fixed-point data types and arithmetic in MATLAB. The toolbox can also pass fixed-point data types to and from Simulink to facilitate the analysis of fixed-point simulations and implementation of systems. This article describes how we modeled a fixed-point digital filter implementation in MATLAB using the Fixed-Point Toolbox. Before the release of this toolbox, engineers who were implementing fixed-point systems typically used C, and were restricted to integer operations (sometimes referred to as "fixed-point C"). Alternatively, they would mimic fixed-point operations in MATLAB by injecting quantization operations into their floating-point M-code. The new Fixed-Point Toolbox provides the same convenience for fixed-point systems that the original MATLAB did for floating-point systems. Fixed-Point Arithmetic in MATLAB The attributes of a fixed-point quantity can be represented as being sN,M where the s indicates that the quantity is signed (u for unsigned), the N indicates that there are N bits used to represent the quantity (word length N), and the M indicates that there are M-bits below the binary point (fraction length M). For example, the diagram below illustrates an s8,3 quantity: ![]() Quantities such as these are easily manipulated and visualized using the Fixed-Point Toolbox. To create a fixed-point quantity, use the fi1 constructor to create a fixed-point quantity: >> a=fi(pi) a = 3.1416 S16,13 Signed 16-bit quantities are the default, and the binary point was set automatically, to maximize precision while avoiding overflow. pi = 2+1+.125+...=21+20+2-3+... Expanding pi in powers of two: which we can see by displaying a in binary: >> bin(a) ans = 011.0010010001000 Only the 1s and 0s display in MATLAB. The binary point was inserted for clarity. Basic arithmetic is supported on fixed-point quantities: +, –, and * via overloaded operators and division via a function call. By default, the attributes of return values from +, –, and * are configured to return full-precision results. The return value will use the minimum number of bits subject to a guarantee of no overflow or loss of precision. This may lead to larger than expected word sizes returning from multiplication or addition, but will help you converge to a working fixed-point implementation more quickly. Once an implementation gives reasonable numerical results, other arithmetic modes are available to control the word sizes of the quantities involved. Also, if arithmetic is not executed using full precision, user-configurable overflow and rounding modes are available. Using full precision with s16,13 quantity a, a*a yields an s32,26 product: >> a*a ans = 9.8697 s32,26 We can also control the attributes of the product: >> a.productMode='SpecifyPrecision'; >> a.productFractionLength=0; >> a*a ans = 10 s32,0 The product’s value was influenced by the rounding mode (defaulted to round-to-nearest). Addition works similarly. Full precision first:>> a+a ans = 6.2832 s17,13 Next, we specify the attributes of the sum.We will configure addition to work as if we were modeling behavior on a device with a 40-bit accumulator: >> a.sumMode='SpecifyPrecision'; >> a.sumWordLength=40; >> a.sumFractionLength=13; >> a+a ans = 6.2832 s40,13 Fixed-Point Finite Impulse Response (FIR) Filter Example Using this simple example, we can now work toward implementing a fixed-point digital filter in MATLAB.We assume that we are modeling an implementation that is to run on a DSP device with 16-bit words, a 32-bit product register, and a 40-bit accumulator. An FIR filter is just a weighted moving average with input x(k) and constant coefficients b: y(k) = b(1)*x(k) + b(2)*x(k-1) + ... + b(m)*x(k-m+1) The selection of coefficients b(k) is the topic of the Filter Design Toolbox and the Signal Processing Toolbox. In this discussion, we assume that b is known. We have implemented a system described by the equation above in the M-file function listed below. This function will handle both fixed and floating-point data, without modification. % FIR filter – arbitrary data type function y = fir_filter(b,x,y) z = zeros(length(b),1); for k=1:length(x) z = [x(k); z(1:end-1)]; y(k) = b*z; % Inner product end We have done something slightly out of the ordinary for M-file functions: we have passed output variable y as an input variable so that the function will be aware of y’s data type. The variable z will inherit x’s data type from the concatenation with x. We assume that the filter’s input x has 12 bits. The double-precision filter coefficients b are obtained using the Parks-McClellan FIR filter design method from the Signal Processing Toolbox, and they are represented in fixed point with 16 bits. The output y also has 16 bits. We define variables for the word lengths so that they may easily be changed: % Set word sizes Wprod=32; Wacc=40; Wb=16; Wx=12; Wy=16; % % Initialize floating-point variables % [L,fo,mo,w] = firpmord([1500 2000],[1 0],[0.01 0.1], 8000); b_flt = firpm(L,fo,mo,w); N = 100; t = (0:N-1)'; x_flt = sin(2*pi*0.1*t) + sin(2*pi*0.2885*t); y_flt = zeros(size(x_flt)); % % Initialize fixed-point variables % b_fi = fi(b_flt, 1, Wb); x_fi = fi(x_flt, 1, Wx); y_fi = fi(zeros(size(x_fi)), 1, Wy, Wy-innerprodintbits(b_fi,x_fi)); % % Configure fixed-point arithmetic (fimath) % F = fimath('ProductMode','KeepLSB','ProductWordLength',Wprod, 'SumMode','KeepLSB', 'SumWordLength',Wacc); b.fimath = F; x.fimath = F; % Run fixed- and floating-point algorithms y_fi = fir_filter(b_fi, x_fi, y_fi); y_flt = fir_filter(b_flt, x_flt, y_flt); Plotting the results (see Figure 1), we note that the error between the fixed- and floating-point implementations is within 5 . 10–4, which is about one-half the scaling of the least-significant bit of the quantization of the input x. ![]() Figure 1: Contrasting FIR filter output for floating point versus fixed point. Click on image to see enlarged view MATLAB Based Design Flow We recommend the following design flow for developing fixed-point implementations in MATLAB:
1. In Wilkinson’s classic texts The Algebraic Eigenvalue Problem and Rounding Errors in Algebraic Processes, he used fi(x) to indicate fixed-point quantization, and fl(x) to indicate floating-point quantization. 更多... |
![]() |