# v_bitsprec

## PURPOSE

V_BITSPREC round values to a specified fixed or floating precision (X,N,MODE)

## SYNOPSIS

function y=v_bitsprec(x,n,mode)

## DESCRIPTION

    V_BITSPREC round values to a specified fixed or floating precision (X,N,MODE)

     mode is of the form 'uvw' where:
         u: s - n significant bits (default)
            f - fixed point: n bits after binary point
         v: n - round to nearest (default)
            p - round towards +infinity
            m - round towards -infinity
            z - round towards zero
     w is only needed if v=n in which case it dictates what to
            do if x is min-way between two rounded values:
         w: p,m - as above
            e - round to nearest even number (default)
            o - round to nearest odd number
            a - round away from zero
     mode='*ne' and '*no' are convergent rounding and introduce
     no DC offset into the result so long as even and odd integer parts are
     equally common.

     Examples of y=v_bitsprec(x,0,'***'):

          x    fp-  fm-  fz-  fne  fno  fnp  fnm  fna

         2.5    3    2    2    2    3    3    2    3
         1.5    2    1    1    2    1    2    1    2
         1.1    2    1    1    1    1    1    1    1
         1.0    1    1    1    1    1    1    1    1
         0.9    1    0    0    1    1    1    1    1
         0.5    1    0    0    0    1    1    0    1
         0.1    1    0    0    0    0    0    0    0
        -0.1    0   -1    0    0    0    0    0    0
        -0.5    0   -1    0    0   -1    0   -1   -1
        -0.9    0   -1    0   -1   -1   -1   -1   -1
        -1.5   -1   -2   -1   -2   -1   -1   -2   -2

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function y=v_bitsprec(x,n,mode)
    0002 %V_BITSPREC round values to a specified fixed or floating precision (X,N,MODE)
    0003 %
    0004 % mode is of the form 'uvw' where:
    0005 %     u: s - n significant bits (default)
    0006 %        f - fixed point: n bits after binary point
    0007 %     v: n - round to nearest (default)
    0008 %        p - round towards +infinity
    0009 %        m - round towards -infinity
    0010 %        z - round towards zero
    0011 % w is only needed if v=n in which case it dictates what to
    0012 %        do if x is min-way between two rounded values:
    0013 %     w: p,m - as above
    0014 %        e - round to nearest even number (default)
    0015 %        o - round to nearest odd number
    0016 %        a - round away from zero
    0017 % mode='*ne' and '*no' are convergent rounding and introduce
    0018 % no DC offset into the result so long as even and odd integer parts are
    0019 % equally common.
    0020 %
    0021 % Examples of y=v_bitsprec(x,0,'***'):
    0022 %
    0023 %      x    fp-  fm-  fz-  fne  fno  fnp  fnm  fna
    0024 %
    0025 %     2.5    3    2    2    2    3    3    2    3
    0026 %     1.5    2    1    1    2    1    2    1    2
    0027 %     1.1    2    1    1    1    1    1    1    1
    0028 %     1.0    1    1    1    1    1    1    1    1
    0029 %     0.9    1    0    0    1    1    1    1    1
    0030 %     0.5    1    0    0    0    1    1    0    1
    0031 %     0.1    1    0    0    0    0    0    0    0
    0032 %    -0.1    0   -1    0    0    0    0    0    0
    0033 %    -0.5    0   -1    0    0   -1    0   -1   -1
    0034 %    -0.9    0   -1    0   -1   -1   -1   -1   -1
    0035 %    -1.5   -1   -2   -1   -2   -1   -1   -2   -2
    0036
    0037 %      Copyright (C) Mike Brookes 1997
    0038 %      Version: $Id: v_bitsprec.m 10865 2018-09-21 17:22:45Z dmb $
    0039 %
    0040 %   VOICEBOX is a MATLAB toolbox for speech processing.
    0041 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
    0042 %
    0043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0044 %   This program is free software; you can redistribute it and/or modify
    0045 %   it under the terms of the GNU Lesser General Public License as published by
    0046 %   the Free Software Foundation; either version 3 of the License, or
    0047 %   (at your option) any later version.
    0048 %
    0049 %   This program is distributed in the hope that it will be useful,
    0050 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
    0051 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    0052 %   GNU Lesser General Public License for more details.
    0053 %
    0054 %   You can obtain a copy of the GNU Lesser General Public License from
    0055 %   https://www.gnu.org/licenses/ .
    0056 %
    0057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0058
    0059 if nargin<3
    0060    mode='sne';
    0061 end
    0062 if mode(1)=='f'
    0063    e=0;
    0064 else
    0065    [x,e]=log2(x);
    0066 end
    0067 switch mode(2)
    0068 case 'p'
    0069    y=pow2(ceil(pow2(x,n)),e-n);
    0070 case 'm'
    0071    y=pow2(floor(pow2(x,n)),e-n);
    0072 case 'z'
    0073    y=pow2(fix(pow2(x,n)),e-n);
    0074 otherwise
    0075    switch mode(3)
    0076    case 'a'
    0077       y=pow2(round(pow2(x,n)),e-n);
    0078    case 'p'
    0079       y=pow2(floor(pow2(x,n)+0.5),e-n);
    0080    case 'm'
    0081       y=pow2(ceil(pow2(x,n)-0.5),e-n);
    0082    otherwise
    0083       z=pow2(x,n-1);
    0084       switch mode(3)
    0085       case 'e'
    0086          y=pow2(floor(pow2(x,n)+0.5)-floor(z+0.75)+ceil(z-0.25),e-n);
    0087       case 'o'
    0088          y=pow2(ceil(pow2(x,n)-0.5)+floor(z+0.75)-ceil(z-0.25),e-n);
    0089       end
    0090    end
    0091 end
    0092