DSPRelated.com
Forums

1.15 format

Started by tio tio October 10, 2003
Dear Matlab Users,
I am new in this exciting world - DSP and Matlab.

I would like to know if there is any matlab function, which convert decimal
numbers to 1.15 format or m.n format in general. If there is not any function,
please help me to build this one.

thanks in advance,
Tio





Hi Tio,

In the 1.15 format, there is one sign bit (the MSB) and fifteen fractional
bits representing values from -1 up to one LSB less than +1.

To convert a negative decimal number, say -0.242146, to 1.15 format:

Multiply the positive, ie. 0.242146 by 2^15 and convert to HEX, which gives
you 1EFE. Take the two's compliment of the number, which will give you
correct value in 1.15 format, i.e. 0xE101 in this example.

The following function will do this for you:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
% Function to convert a decimal number into 1.15 hex format
function [hex_out] = convert_onedotfifteen(indecimal)

temp = indecimal; % Ensure positive number
quant = abs(round((2^15)*-temp)); % Quantize to 15 bits and round to the
nearest integer

if indecimal < 0 % i.e. negative
quant = bitcmp(quant,16) + 1; % Take the 2's complement
end

hex_out2hex(quant); % Convert the decimal number to hex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%

To use this function, copy the code into a new .m file and save as
convert_onedotfifteen.m (make sure Matlab can see the directory you save
your file in).

I tried a few examples:

>> convert_onedotfifteen(0.999969)

ans =

7FFF

>> convert_onedotfifteen(-1)

ans =

8000

>> convert_onedotfifteen(-0.000031)

ans =

FFFF Hope this helps,

Jeff -----Original Message-----
From: tio tio [mailto:]
Sent: Friday, October 10, 2003 8:14 AM
To:
Subject: [matlab] 1.15 format Dear Matlab Users,
I am new in this exciting world - DSP and Matlab.

I would like to know if there is any matlab function, which convert decimal
numbers to 1.15 format or m.n format in general. If there is not any
function, please help me to build this one.

thanks in advance,
Tio _____________________________________
Note: If you do a simple "reply" with your email client, only the author of
this message will receive your answer. You need to do a "reply all" if you
want your answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join:

To Post:

To Leave:

Archives: http://www.yahoogroups.com/group/matlab

More DSP-Related Groups: http://www.dsprelated.com/groups.php3

">http://docs.yahoo.com/info/terms/


Tio-

> In the 1.15 format, there is one sign bit (the MSB) and fifteen fractional
> bits representing values from -1 up to one LSB less than +1.
>
> To convert a negative decimal number, say -0.242146, to 1.15 format:
>
> Multiply the positive, ie. 0.242146 by 2^15 and convert to HEX, which gives
> you 1EFE. Take the two's compliment of the number, which will give you
> correct value in 1.15 format, i.e. 0xE101 in this example.

Or just multiply by -32768, the largest possible 16-bit negative number; i.e.
-1.0 in
Q15 format.

-Jeff > The following function will do this for you:
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> %%%%%%%%%%%%%%%%
> % Function to convert a decimal number into 1.15 hex format
> function [hex_out] = convert_onedotfifteen(indecimal)
>
> temp = indecimal; % Ensure positive number
> quant = abs(round((2^15)*-temp)); % Quantize to 15 bits and round to the
> nearest integer
>
> if indecimal < 0 % i.e. negative
> quant = bitcmp(quant,16) + 1; % Take the 2's complement
> end
>
> hex_out2hex(quant); % Convert the decimal number to hex
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> %%%%%%%%%%%%%%%%%%
>
> To use this function, copy the code into a new .m file and save as
> convert_onedotfifteen.m (make sure Matlab can see the directory you save
> your file in).
>
> I tried a few examples:
>
> >> convert_onedotfifteen(0.999969)
>
> ans =
>
> 7FFF
>
> >> convert_onedotfifteen(-1)
>
> ans =
>
> 8000
>
> >> convert_onedotfifteen(-0.000031)
>
> ans =
>
> FFFF
>
> Hope this helps,
>
> Jeff
>
> -----Original Message-----
> From: tio tio [mailto:]
> Sent: Friday, October 10, 2003 8:14 AM
> To:
> Subject: [matlab] 1.15 format
>
> Dear Matlab Users,
> I am new in this exciting world - DSP and Matlab.
>
> I would like to know if there is any matlab function, which convert decimal
> numbers to 1.15 format or m.n format in general. If there is not any
> function, please help me to build this one.
>
> thanks in advance,
> Tio



Hi,

    But do remember that when you multiply two 1.15 numbers,the result WILL be in 2.30...you can convert this to either 1.31...by left shifting the result by 1 bit(multiply by two) or in 1.15 by right shifting by 15 bits and taking the 16 LSBs...it depends on what you want...

But at any rate the above algorthim doesnt take care of the -1 * -1 multiplication...

So,  -1 * -1 is a speacial case and it requires you to handle it accordingly...-1 * -1 should actually result in 1.But 1 is not representable exactly in 1.15,what you can get is .99999 ie 7fffh...so if the result is greater than 8000h then it should be hard coded to 7fffh...

Hope this helps...

Bhooshan



Contact brides grooms FREE! Only on www.shaadi.com. Register now!

Bhooshan-

> But do remember that when you multiply two 1.15 numbers,the result WILL be
in
> 2.30...you can convert this to either 1.31...by left shifting the result by 1
> bit(multiply by two) or in 1.15 by right shifting by 15 bits and taking the 16
> LSBs...it depends on what you want...

Some DSPs have an option that does the extra left shift automatically.

> But at any rate the above algorthim doesnt take care of the -1 * -1
> multiplication...
>
> So, -1 * -1 is a speacial case and it requires you to handle it
accordingly...-1 *
> -1 should actually result in 1.But 1 is not representable exactly in 1.15,what
you
> can get is .99999 ie 7fffh...so if the result is greater than 8000h then it
should
> be hard coded to 7fffh...

Some DSPs take care of this also, using saturation option.

All in the name of processing speed.

-Jeff



Bhooshan-

> But do remember that when you multiply two 1.15 numbers,the result WILL be
in
> 2.30...you can convert this to either 1.31...by left shifting the result by 1
> bit(multiply by two) or in 1.15 by right shifting by 15 bits and taking the 16
> LSBs...it depends on what you want...

Some DSPs have an option that does the extra left shift automatically.

> But at any rate the above algorthim doesnt take care of the -1 * -1
> multiplication...
>
> So, -1 * -1 is a speacial case and it requires you to handle it
accordingly...-1 *
> -1 should actually result in 1.But 1 is not representable exactly in 1.15,what
you
> can get is .99999 ie 7fffh...so if the result is greater than 8000h then it
should
> be hard coded to 7fffh...

Some DSPs take care of this also, using saturation option.

All in the name of processing speed.

-Jeff



Tio-

> thank you for your valuable help.
> I made a little change in your code to fit to a general m.n format,

I think the excellent code was provided by Jeff Winter.

> but the problem which I can't solve is to display result in binary
> format. For example 0.678 in decimal format
> give 56C9 in hex format, but I would like to display respective
> binary number:
> 0101 0110 1100 1001. How I can do this ?

Well I don't know, but I've posted on the group for you and I bet you get an
answer,
because MATLAB can basically do anything -- maybe not very fast, but almost
anything
is possible.

-Jeff Here is the modified code:

% MDOTN(x,m,n) convert decimal numbers in m.n format, where
% x is vector with decimal numbers
% m is integer bits
% n is fractional bits
%
%
% Example
% [hex]=mdotn(-1,1,15)
%
% hex =
% 8000
%
%
function [hex] = mdotn(x,m,n)
if nargin==0, help mdotn;
return;
end
x=x(:);
if (x<-2^(m-1) | x>2^(m-1)-2^(-n)) % check if input number x is in the range of
m.n
format
error('Input is out of range')
end
quant = abs(round((2^n)*-x)); % Quantize to 15 bits and round to the
nearest
integer
if x < 0 % i.e. negative
quant = bitcmp(quant,m+n) + 1; % Take the 2's complement
end
hex2hex(quant); % Convert the decimal number to hex



Hi Jeff,

>Some DSPs have an option that does the extra left shift automatically.

yeah, even c6x has the option but it seems like you can exploit it only if u program in asm and iff you select SMPY intsruction...mmm...what if i dont want to use SMPY but just mpy...is there a way?(am using some vague ways to do this...like detecting the first 2 bits...)
 
And ofcourse if i program in C am left with the reality of handling the shifting myself !(are there any intrinsics to do this in C?)
 
> But at any rate the above algorthim doesnt take care of the -1 * -1   multiplication...
> So, -1 * -1 is a speacial case and it requires you to handle it accordingly...-1 *
> > -1 should actually result in 1.But 1 is not representable exactly in 1.15,what you
> > can get is .99999 ie 7fffh...so if the result is greater than 8000h then it should
> > be hard coded to 7fffh...

>Some DSPs take care of this also, using saturation option.

Again...in C its quite difficult...infact the only way i have i figured to detect overflow for ffff8000h * ffff8000h  is to check if the result(40000000h) is greater than 80000000h and if so make it equal to 7fffffffh...is there a CLEANER way than this to do it in C? like the way SMPY instn does it by detecting if has two different sign bits(like 01)

I have seen some good fixed point C coding techniques in basic_op.c of the g729a code...thats what i based my understanding upon...

Bhooshan

Ps:I have noticed that even -1 and +1 to be a speacial case and requires some amount of extra work....



Keep up with the pace of change. Register for My Tech Ed. Realise your potential!

Hi,
 > but the problem which I can't solve is to display result in binary  format.
 
there is no difference between hex and binary!
 
> For example 0.678 in decimal format  give 56C9 in hex format, but I would like to display > respective  binary number: 0101 0110 1100 1001. How I can do this ?
 
As i said, there is absolutely no difference between what you are getting and what you want!
I mean,   501, 610 C00 and 901 so whats the problem??!! and if you so sure that you want it in binary form write a look up table print from it...or write a hex to bin converter...
 
See if this matlab code can help...just wrote this to find out the decimal equivalent of 1.15 numbers...please note that it will give DECIMAL equivalent of the fraction in 1.15 that is if you want to find out the equivant of .678 it will give you 22217 which is nothing but 56C9 which is nothing but 0101 0110 1100 1001!
 
-------------
function y = twocomplement(x)
% function y=twocomplement(x)
% compute 2's complement equivalent of x in 16 bits
% x is a number in [-1,1)
i = find((x<-1) | (x>=1));
if isempty(i)
 y = floor(32768*x);
 index = find(y < 0);
 y(index) = 65536+y(index);
 return;
else
 disp('error in twocomplement(): floating number must be in [-1,1)');
 return;
end;
-------------
 
Bhooshan


Attention all artisans! Sell Diwali creations online. Register now!

Hi Tio,

Pleased it helped.

All you need to do get get the result in binary is to use the following:
BinOut2bin(quant);

You can either add an additional output argument in your function to return
both hex and bin, or replace the dec2hex call. It's up to you.

Jeff **************************************************
Jeff Winter
Snr Design Engineer
Aeroflex
************************************************** -----Original Message-----
From: tio tio [mailto:]
Sent: Saturday, October 11, 2003 4:21 PM
To: ;
Subject: RE: [matlab] 1.15 format Jeff,
thank you for your valuable help.
I made a little change in your code to fit to a general m.n format, but
the problem which I can't solve is to display result in binary format. For
example 0.678 in decimal format give 56C9 in hex format, but I would like to
display respective binary number:
0101 0110 1100 1001.
How I can do this ?

Here is the modified code:

% MDOTN(x,m,n) convert decimal numbers in m.n format, where
% x is vector with decimal numbers
% m is integer bits
% n is fractional bits
%
%
% Example
% [hex]=mdotn(-1,1,15)
%
% hex =
% 8000
%
%
function [hex] = mdotn(x,m,n)
if nargin==0, help mdotn;
return;
end
x=x(:);
if (x<-2^(m-1) | x>2^(m-1)-2^(-n)) % check if input number x is in the
range of m.n format
error('Input is out of range')
end
quant = abs(round((2^n)*-x)); % Quantize to 15 bits and round to the
nearest integer
if x < 0 % i.e. negative
quant = bitcmp(quant,m+n) + 1; % Take the 2's complement
end
hex2hex(quant); % Convert the decimal number to hex Jeff Winter <> wrote:

Hi Tio,

In the 1.15 format, there is one sign bit (the MSB) and fifteen
fractional
bits representing values from -1 up to one LSB less than +1.

To convert a negative decimal number, say -0.242146, to 1.15 format:

Multiply the positive, ie. 0.242146 by 2^15 and convert to HEX, which
gives
you 1EFE. Take the two's compliment of the number, which will give you
correct value in 1.15 format, i.e. 0xE101 in this example.

The following function will do this for you: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%
% Function to convert a decimal number into 1.15 hex format
function [hex_out] = convert_onedotfifteen(indecimal)

temp = indecimal; % Ensure positive number
quant = abs(round((2^15)*-temp)); % Quantize to 15 bits and round to
the
nearest integer

if indecimal < 0 % i.e. negative
quant = bitcmp(quant,16) + 1; % Take the 2's complement
end

hex_out2hex(quant); % Convert the decimal number to
hex

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%

To use this function, copy the code into a new .m file and save as
convert_onedotfifteen.m (make sure Matlab can see the directory you save
your file in).

I tried a few examples:

>> convert_onedotfifteen(0.999969)

ans =

7FFF

>> convert_onedotfifteen(-1)

ans =

8000

>> convert_onedotfifteen(-0.000031)

ans =

FFFF Hope this helps,

Jeff -----Original Message-----
From: tio tio [mailto:]
Sent: Friday, October 10, 2003 8:14 AM
To:
Subject: [matlab] 1.15 format Dear Matlab Users,
I am new in this exciting world - DSP and Matlab.

I would like to know if there is any matlab function, which convert
decimal
numbers to 1.15 format or m.n format in general. If there is not any
function, please help me to build this one.

thanks in advance,
Tio _____________________________________
Note: If you do a simple "reply" with your email client, only the author
of
this message will receive your answer. You need to do a "reply all" if
you
want your answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join:

To Post:

To Leave:

Archives: http://www.yahoogroups.com/group/matlab

More DSP-Related Groups: http://www.dsprelated.com/groups.php3

">http://docs.yahoo.com/info/terms/ _____________________________________
Note: If you do a simple "reply" with your email client, only the author
of this message will receive your answer. You need to do a "reply all" if
you want your answer to be distributed to the entire group.

_____________________________________
About this discussion group:

To Join:

To Post:

To Leave:

Archives: http://www.yahoogroups.com/group/matlab

More DSP-Related Groups: http://www.dsprelated.com/groups.php3 ----
--