www.gusucode.com > simulinkcoder 案例源码程序 matlab代码 > simulinkcoder/FloatingPointMultiplicationNetSlopeCorrectionExample.m

    %% Floating-Point Multiplication to Handle a Net Slope Correction
% This example shows how to use floating-point multiplication to handle
% a net slope correction. When converting floating-point data types to
% fixed-point data types in the generated code, a net slope correction is one method of scaling
% fixed-point data types.  Scaling the fixed-point data types avoids
% overflow conditions and minimizes quantization errors.
%
% For processors that support efficient multiplication, using
% floating-point multiplication to handle a net slope correction improves
% code efficiency. If the net slope correction has a value that is
% not a power of two, using division improves precision.
%
% *Note:* This example requires a Fixed-Point Designer™ license. 
%
%   Copyright 2015 The MathWorks, Inc.
%% Example 
% In the model <matlab:rtwdemo_float_mul_for_net_slope_correction rtwdemo_float_mul_for_net_slope_correction>, a Convert block converts an
% input signal from a floating-point data type to a fixed-point data type.
% The net slope correction has a value of |3|.
% 
model = 'rtwdemo_float_mul_for_net_slope_correction';
open_system(model);


%% Generate Code
% Create a temporary folder for the build and inspection process.
currentDir = pwd;
[~,cgDir] = rtwdemodir();

%%
% Build the model.
rtwbuild(model)

%%
% In these lines of |rtwdemo_float_mul_for_net_slope_correction.c| code,
% the code generator divides the input signal by |3.0F| .
% 
cfile = fullfile(cgDir,'rtwdemo_float_mul_for_net_slope_correction_ert_rtw',...
    'rtwdemo_float_mul_for_net_slope_correction.c');
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);

%% Enable Optimization 
% 
% # Open the Configuration Parameters dialog box.
% # On the *Optimization* pane, select *Use floating-point multiplication to
% handle net slope corrections*. This optimization is on by
% default.
% 
% Alternatively, you can use the command-line API to enable the
% optimization.
set_param(model, 'UseFloatMulNetSlope', 'on');

%% Generate Code with Optimization
%
rtwbuild(model)
%%
% In the optimized code, the code generator multiplies the input signal by the reciprocal of |3.0F| , that is |0.333333343F| .
rtwdemodbtype(cfile,'/* Model step', '/* Model initialize', 1, 0);
%%
% Close the model and the code generation report.
bdclose(model)
rtwdemoclean;
cd(currentDir)