Floating Point to Fixed Point & Fixed Point to Floating Point Conversion Utilities

Convert numbers to/from fixed point/floating point automatically or under user specification.
252 Downloads
Updated 15 Dec 2015

View License

The utilitiy Float2Qpt allow the user to convert floating point numbers to either signed or unsigned fixed point numbers in a Q(QI.QF) format with automatically selected or user definable word lengths, and automatically selected or user definable error tolerances (epsilon). Alternatively a user can pass in optional parameters to force the type of rounding used in the conversion, enable/disable verbose reporting, and even open the reference paper the untilty is based on. The Utility FxdPt2Float converts a fixed point number back to a floating point number given a fixed point number in a Q(QI.QF) format based on the fixed point number and the number of fractional bits QF.
These utilities are useful for development of fixed point versions of algorithms & the referenced white paper provides example of typical multiply/accumulate/saturate types of operations done in fixed point.

function[FxdPt, QI, QF] = Float2Qpt(num,varargin)
% Calculate the Fixed Point Approximation of a Floating point number "num"
% in Q Fixed Point Format - Q(QI.QF)
% FxdPt - is the fixed point coverted value
% QI - is the number of iteger bits required for range of num
% QF - is the number of fractional bits required for a specified epsilon or
% the number of fractional bits implemented when using a shortest standard
% word length and living whith the resolution you get for the required
% range
%
% OptionalInputs
% 'signed' - Create a signed fixed point number *Default
% 'unsigned' - Create an unsigned fixed poin number
% 'ShortestSWL' - Creates the shortest Standard Word Length
% 'WordLength' 8,16,32,64,128,256
% 'epsilon',number - specifies an epsilon error tolerance used to set the
% number of fractional bits QF
% 'QF',number - The user can specify a "number' of fractional bits and
% covert afloating point "num" to fixed point directly
% without use of other parameters other than rouding options
% 'round_closest' - rounds to the closest integer when computing FxdPt
% 'round_zero' - rounds the towards zero when computing FxdPt
% *** round towards zero is default behavior
% 'verbose' - displays conversion process diagnostics based on user inputs
% in addition to coversion range & precision information
% VERY useful to turn on - off by default
% 'Paper' - opens URL to a PDF copy of the paper below:
% "Fixed-Point Representation & Fractional Math Revision 1.2"
% August 30 2007
% DOI: 10.13140/RG.2.1.3602.8242
% Link1 http://www.superkits.net/whitepapers/Fixed%20Point%20Representation%20%26%20Fractional%20Math.pdf
% Link2 https://www.researchgate.net/publication/235791711_Fixed-Point_Representation_Fractional_Math_Revison_12
%
% Written By Erick L. Oberstar 12/15/2015
% U.W. Madison Mechatronics Lab & Intellivention LLC
% oberstar@engr.wisc.edu & oberstare@intellivention.com
%**************************************************************************

function [ FloatingPtNum ] = FxdPt2Float( FxdPt, QF, varargin )
% Calculate the Floating point value of a fixed point number
% in Q Fixed Point Format - Q(QI.QF)
% FxdPt - is the fixed point coverted (integer) value
% QI - is the number of iteger bits used for range of FxdPt (not required)
% QF - is the number of fractional bits used that sets the epsilon
%
% OptionalInputs
% 'verbose' - displays conversion process diagnostics based on user inputs
% in addition to coversion range & precision information
% VERY useful to turn on - off by default
% 'Paper' - opens URL to a PDF copy of the paper below:
% "Fixed-Point Representation & Fractional Math Revision 1.2"
% August 30 2007
% DOI: 10.13140/RG.2.1.3602.8242
% Link1 http://www.superkits.net/whitepapers/Fixed%20Point%20Representation%20%26%20Fractional%20Math.pdf
% Link2 https://www.researchgate.net/publication/235791711_Fixed-Point_Representation_Fractional_Math_Revison_12
%
% Written By Erick L. Oberstar 12/15/2015
% U.W. Madison Mechatronics Lab & Intellivention LLC
% oberstar@engr.wisc.edu & oberstare@intellivention.com
%**************************************************************************

Cite As

Erick Oberstar (2024). Floating Point to Fixed Point & Fixed Point to Floating Point Conversion Utilities (https://www.mathworks.com/matlabcentral/fileexchange/54521-floating-point-to-fixed-point-fixed-point-to-floating-point-conversion-utilities), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R10
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.0.0