MIT nyitóoldal
Térkép

Opening page

Contents list (extended with web addendum)

Matlab M-files for fixed-point and floating-point simulation of roundoff

Data files

Matlab function M-files for the Book "Quantization Noise"
Simulation tools for arbitrary-precision floating-point and fixed-point roundoff

Several function M-files have been developed for Matlab, for the simulation and analysis of roundoff in fixed-point and floating-point calculations, to support the ideas in the book. For a short overview, see the appendix on simulation of finite word length in Matlab.

The basic idea is that roundoff can be modelled as ideally precise operations followed by a roundoff quantizer. "Ideally precise" means a very well approximating and a precise solution:

  • the built-in Matlab precision (IEEE double, that is, p = 53), see the roundoff toolbox - this is very good approximation;
  • increased-precision (110-bit) calculations with exact roundoff, see the qdsp toolbox.

Roundoff Toolbox

An example of the use of files to simulate calculations in any precision lower than in IEEE double precision (that is, p < 53, e.g. IEEE single precision: p = 24):

  Q = qstruct('single'); %IEEE single precision quantizer
  a = roundq(1.2,Q); b=roundq(1.24,Q); %two quantized numbers
  c = roundq(a*b,Q); %This rounds result after multiplication

The native number representation of Matlab is IEEE double precision, therefore to use this, no extra simulation file is needed.

Installation:

  • click (Shift-click) on the link "Roundoff toolbox", and download the file roundoff.zip,
  • extract its contents (files roundoff\*.m) into a directory you prefer (this will be denoted "[DIR]" below),
  • add the path to Matlab, e.g. execute
    addpath('[DIR]\roundoff','-end'),
    or add this line to your own file startup.m in the working directory you use,
  • type 'help roundoff',
  • try a few examples:
    a = 1; b = 1+2^(-23);
    one_plus_eps = roundq(a*b,'single') %basic usage: operation + quantization
    two = roundq(1 + roundq(a*b,'single'),'single') %exactly 2
    ulp(two,'single')  %ULP for 2: 1.1921e-007=2*2^(-24)=2^(-p+1)
    lsb(two,'single')  %LSB for 2: 2.3842e-007=2*2^(-23)=2^(-p+1)
    binstr = num2bin(one_plus_eps,'single')  %binary string
    binplusdecstr = num2bin(one_plus_eps,'single','decimal')  %exponent shown as decimal
    roundq(1+2^(-24),'single')-1  %single precision, returns 0 
    roundq(1+2^(-23),'single')-1  %single precision, returns 2^(-23)=1.1921e-007
    roundq(realmin/2,struct('ufl','f'),'list')  %flush to zero, returns 0
    roundq(realmin/2,struct('ufl','g'))  %gradual underflow 
              %realmin/2=2^(-1022-1)=1.1125e-308
    

Quantization and DSP Simulation Toolbox (QDSP)

More advanced tools are also available. These allow the use of 110 mantissa bits, thus allow to study the roundoff of IEEE double precision, and perform bitwise precise calculations. Installation:
  • click (Shift-click) on the link "QDSP toolbox", and download the file qdsp.zip,
  • extract its contents (files qdsp\*.m, qdsp\private\*.m, etc.) into a directory you prefer (this will be denoted "[DIR]" below),
  • add the path to Matlab, e.g. execute
    addpath('[DIR]\qdsp','-end'),
    or add this line to your own file startup.m in the working directory you use,
  • type 'help qdsp',
  • try a few examples:
    qdspstart %hints how to start
    Q=qmake('Qs','single'); %IEEE single prec
    a=qdata(1.22,Q); b=qdata(2.1,Q); %define numbers
    c=a*b; %perform quantized multiplication 
    get(c) %show number and associated quantizer
    x=qdata(1,'BigFP') %110-bit number
    y=randn(x) %normally distributed 110-bit number
    y=rand(x) %uniformly distributed 110-bit number
    
--------------------
These pages and the associated software are maintained by István Kollár.