DSPRelated.com
Forums

Fixed Point Toolbox

Started by porterboy September 15, 2004
I'm trying to learn the new fixed point toolbox for Matlab V7.0 R14.
I have the following Section of floating point code...

y = randn(1,2)/64;
X = zeros(2,1);
X(1) = y(1) + y(2); 
X(2) = (y(1) - y(2))/sqrt(2);

(which is a two point butterfly in a DCT if you are interested). What
do i do to convert this to fixed point? Here is my (not very good)
attempt.
*******************************************************************
% first generate 18 bit random numbers for y 
y = randn(1,2)/64;                       
y = fi(y,1,18,17,'RoundMode','floor');  

% then define fixed point representation of X
F = fimath('ProductMode','SpecifyPrecision',...
    'SumMode','SpecifyPrecision',...
    'ProductWordLength',32,...
    'ProductFractionLength',16,...
    'SumWordLength',32,...
    'SumFractionLength',16,...
    'RoundMode','floor');

% do we need to initialize X? (I used the zeros function in floating
point)
    X(1) = F.add(x(1),x(2));
    X(2) = F.sub(x(1),x(2));

% eh, no idea how to divide, because divide is defined for
numerictypes
    X(2) = F.divide(X(2),fi(sqrt(2));???
***********************************************************************
Please help?
Hi,
Your fixed-point implementation could look something like this:

% Create your fimath (and attach it to y too)
F = fimath('ProductMode','SpecifyPrecision',...
   'SumMode','SpecifyPrecision',...
    'ProductWordLength',32,...
    'ProductFractionLength',16,...
    'SumWordLength',32,...
    'SumFractionLength',16,...
    'RoundMode','floor');

% Set up y with this fimath F
yfi = fi(y,1,18,17); yfi.fimath = F;

% For X, use the fimath you created and lets say (since that info is not
here)
% that X too is a s18q17 (same as y)
N = numerictype(yfi);
X = fi(zeros(2,1),N,F);

% Then
X(1) = yfi(1) + yfi(2);
X(2) = N.divide(yfi(1)-yfi(2),fi(sqrt(2)));

% You could also compute the 1/sqrt(2) and store it in  a fi
% to get X(2) using the * operator
onebysqrt2 = fi(1/sqrt(2),1,Somewordlength,Somefractionlength);
onebysqrt2.fmath = F;
X(2) = (yfi(1)-yfi(2))*onebysqrt2;

Hope this helps,

Ashok Charry
The Mathworks

"porterboy" <porterboy76@yahoo.com> wrote in message
news:c4b57fd0.0409150441.71f4e3ed@posting.google.com...
> I'm trying to learn the new fixed point toolbox for Matlab V7.0 R14. > I have the following Section of floating point code... > > y = randn(1,2)/64; > X = zeros(2,1); > X(1) = y(1) + y(2); > X(2) = (y(1) - y(2))/sqrt(2); > > (which is a two point butterfly in a DCT if you are interested). What > do i do to convert this to fixed point? Here is my (not very good) > attempt. > ******************************************************************* > % first generate 18 bit random numbers for y > y = randn(1,2)/64; > y = fi(y,1,18,17,'RoundMode','floor'); > > % then define fixed point representation of X > F = fimath('ProductMode','SpecifyPrecision',... > 'SumMode','SpecifyPrecision',... > 'ProductWordLength',32,... > 'ProductFractionLength',16,... > 'SumWordLength',32,... > 'SumFractionLength',16,... > 'RoundMode','floor'); > > % do we need to initialize X? (I used the zeros function in floating > point) > X(1) = F.add(x(1),x(2)); > X(2) = F.sub(x(1),x(2)); > > % eh, no idea how to divide, because divide is defined for > numerictypes > X(2) = F.divide(X(2),fi(sqrt(2));??? > *********************************************************************** > Please help?
Thanks Ashok, that was very useful.

> % For X, use the fimath you created and lets say (since that info is not > here) > % that X too is a s18q17 (same as y) > N = numerictype(yfi); > X = fi(zeros(2,1),N,F);
Suppose X is not the same as y. Will the divide function still work, even though the numerictypes are different? Or do they have to be cast to the same type?
Hi,

Yes, X can have a different numerictype and the divide would still work.
% Say X is a s16q15
Nx = numerictype(X);
Nx.divide(X,yfi) % will work and the result will be a s16q15

However, I'd rather not use divide in your fixed-point implementation. The
better practice is to
compute the 1/sqrt(2) and store that as a constant (fi) and use multiply
instead.


"porterboy" <porterboy76@yahoo.com> wrote in message
news:c4b57fd0.0409152341.41f14c2c@posting.google.com...
> Thanks Ashok, that was very useful. > > > % For X, use the fimath you created and lets say (since that info is not > > here) > > % that X too is a s18q17 (same as y) > > N = numerictype(yfi); > > X = fi(zeros(2,1),N,F); > > Suppose X is not the same as y. Will the divide function still work, > even though the numerictypes are different? Or do they have to be cast > to the same type?