www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgprecpolyfix/hw2phys.m
function PhysValues = hw2phys(Prec, HWValues) %HW2PHYS Convert hardware values to physical values. % % PhysValues = HW2PHYS(POLYFIXPREC, HWValues) converts hardware values % HWValues to physical values PhysValues using a polynomial-based mapping % from physical values to hardware values specified by the cgprecpolyfix % object POLYFIXPREC. % Copyright 2000-2006 The MathWorks, Inc. and Ford Global Technologies, Inc. % Error check on HWValues HWValues = i_check(Prec, HWValues, 'HWValues'); % Fetch polynomial coefficients for the physical to hardware conversion NumCoeff = get(Prec, 'NumCoeff'); DenCoeff = get(Prec, 'DenCoeff'); PhysRange = get(Prec, 'PhysRange'); NPolyCoeffs = max(length(NumCoeff), length(DenCoeff)); if length(NumCoeff)<NPolyCoeffs tmp = zeros(1, NPolyCoeffs); tmp(end-length(NumCoeff)+1:end) = NumCoeff; NumCoeff = tmp; end if length(DenCoeff)<NPolyCoeffs tmp = zeros(1, NPolyCoeffs); tmp(end-length(DenCoeff)+1:end) = DenCoeff; DenCoeff = tmp; end % Resolve to achievable fixed point hardware values HWValues = hwresolve(Prec, HWValues); % Initialise PhysValues PhysValues = zeros(size(HWValues)); for idx = 1:numel(HWValues) if isfinite(HWValues(idx)) % Solve (soln is the solution) soln = roots(NumCoeff-HWValues(idx)*DenCoeff); % Discard complex solutions soln = soln(abs(imag(soln))<1e-6); soln = real(soln); % Discard solutions that are outside the range of physical values soln = soln(soln>=PhysRange(1) & soln<=PhysRange(2)); if isempty(soln) % No real solutions, flag with NaN soln = NaN; else % Multiple valid real solutions: To avoid problems with nonunique % roots of the polynomial, keep only the largest valid real % solution soln = max(soln); end else % We cannot find roots of equations that contain NaN or inf, so the % default output for these is NaN soln = NaN; end % Add this solution to PhysValues PhysValues(idx) = soln; end % ------------------------------------------------------------------------- function out = i_check(Prec, in, VarName) % Check input variables switch VarName case 'HWValues' if pCheckNumeric(Prec, in, true, 'HWValues') % HWValues is valid out = in; end end