www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgprecpolyfix/cgprecpolyfix.m

    function PolyFixPrec = cgprecpolyfix(NumCoeff, DenCoeff, PhysRange, varargin)
%CGPRECPOLYFIX Constructor for the cgprecpolyfix class.
%
%   POLYFIXPREC = CGPRECPOLYFIX(NumCoeff, DenCoeff, PhysRange, FIXPREC)
%   constructs a cgprecpolyfix object POLYFIXPREC with a mapping from
%   physical values to hardware values specified by a ratio of polynomials
%   with coefficients NumCoeff (numerator) and DenCoeff (denominator), a
%   range PhysRange of admissible physical values and a parent cgprecfix
%   object FIXPREC.
%
%   POLYFIXPREC = CGPRECPOLYFIX constructs a cgprecpolyfix object
%   with default properties of NumCoeff = 1, DenCoeff = 1, PhysRange =
%   [-Inf Inf] and a default parent cgprecfix object.
%
%   POLYFIXPRC = CGPRECPOLYFIX(NumCoeff, DenCoeff) constructs a
%   cgprecpolyfix object with a default PhysRange = [-Inf Inf] and a
%   default parent cgprecfix object.
%
%   POLYFIXPREC = CGPRECPOLYFIX(NumCoeff, DenCoeff, PhysRange) constructs
%   a cgprecpolyfix object with a parent cgprecfix object FIXPREC.
%
%   POLYFIXPREC = CGPRECPOLYFIX(NumCoeff, DenCoeff, PhysRange, bits, ...
%   signed, ptpos, PREC) first constructs a parent cgprecfix object
%   cgprecfix(bits, signed, ptpos, PREC) and then uses this cgprecfix
%   object and to construct a cgprecpolyfix object.  POLYFIXPREC = ...
%   CGPRECPOLYFIX(NumCoeff, DenCoeff, PhysRange, bits, signed, ptpos, ...
%   Name) is similar.
%
%   See also CGPRECFIX, CGPREC

% ---------------------------------------------------------------------------
% Description : Constructor for the cgprecpolyfix class.
% Inputs      : NumCoeff    - numerator coefficients for the ratio of
%                             polynomials that defines the mapping between
%                             physical values and hardware values
%               DenCoeff    - denominator coefficients for the ratio of
%                             polynomials that defines the mapping between
%                             physical values and hardware values
%               PhysRange   - physical range (dbl,1x2)
% Opt Inputs  : FIXPREC     - cgprecfix object specifying the bits,
%                             un/signed, the fixed point position and the
%                             admissible physical range
%               bits        - bits in the mantissa (int)
%               signed      - sign flag to denote unsigned (0) or signed (1)
%               ptpos       - fixed point position (int)
%               PREC        - cgprec object
%               Name        - name used to construct a cgprec object
%                             (char)
% Outputs     : POLYFIXPREC - cgprecpolyfix object specifying physical to
%                             hardware mapping function
% ---------------------------------------------------------------------------

%   Copyright 2000-2008 The MathWorks, Inc. and Ford Global Technologies, Inc.


% Define object structure
PolyFixPrecStruct = struct('NumCoeff',[],...
    'DenCoeff',[],...
    'PhysRange',[]);

% Error check on NumCoeff, DenCoeff
if nargin==0
    % NumCoeff, DenCoeff not specified, use the default values
    NumCoeff = [1 0];
    DenCoeff = [0 1];
elseif (nargin==1)
    % Too few input arguments
    error(message('mbc:cgprecpolyfix:InvalidArgument'));
else
    % try to use NumCoeff, DenCoeff if valid
    NumCoeff = i_check(NumCoeff, 'NumCoeff');
    DenCoeff = i_check(DenCoeff, 'DenCoeff');
end

% Force NumCoeff, DenCoeff to be the same length by adding leading zeros
NumCoeff = [zeros(1,length(DenCoeff)-length(NumCoeff)), NumCoeff];
DenCoeff = [zeros(1,length(NumCoeff)-length(DenCoeff)), DenCoeff];

% Error check on PhysRange
if nargin<=2
    % PhysRange not specified, use the default value
    PhysRange = i_check([], 'PhysRange');
elseif nargin>2
    % try to use PhysRange if valid
    PhysRange = i_check(PhysRange, 'PhysRange');
end

if nargin<4
    % There are no extra inputs, construct a default cgprecfix object
    FixPrec = cgprecfix;
elseif nargin==4
    if isa(varargin{1}, 'cgprecfix')
        % There is one extra input that is a cgprecfix object
        FixPrec = varargin{1};
    else
        % The extra input is not a valid cgprecfix object
        error(message('mbc:cgprecpolyfix:InvalidArgument1'));
    end
else
    FixPrec = cgprecfix(varargin{:});
end

% Assemble object structure
PolyFixPrecStruct.NumCoeff  = NumCoeff;
PolyFixPrecStruct.DenCoeff  = DenCoeff;
PolyFixPrecStruct.PhysRange = PhysRange;

% Create object class
PolyFixPrec = class(PolyFixPrecStruct,'cgprecpolyfix',FixPrec);



% -------------------------------------------------------------------------
function out = i_check(in, VarName)

% Check input variables
switch VarName
    case {'NumCoeff','DenCoeff'}
        if ~isnumeric(in)
            % NumCoeff, DenCoeff is nonnumeric
            error(message('mbc:cgprecpolyfix:InvalidArgument2', VarName));
        elseif ~isreal(in)
            % HWValues is not real
            error(message('mbc:cgprecpolyfix:InvalidArgument3', VarName));
        elseif size(in,1)~=1
            % NumCoeff, DenCoeff must be a row vector
            out = in(:).';
        else
            % TablePhysData, TableHWData is valid
            out = in;
        end
    case 'PhysRange',
        if isnumeric(in) && isreal(in) && isequal(size(in), [1 2])
            % PhysRange is a real 1x2 array, sort in ascending order
            out = sort(in);
        else
            % PhysRange is not a real 1x2 array, use default value
            out = [-Inf Inf];
        end
end