BestTechie Forums: How To Truncate A 32 Float To A 16bit Float In C# - BestTechie Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Truncate A 32 Float To A 16bit Float In C#


#1 User is offline   iccaros 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1292
  • Joined: 31-August 04
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 04 June 2007 - 03:27 PM

I am processing audio, and sending it over TCP/IP and the people reciving teh package would like to do it this way to make packets smaller.

in c (or C++ )
I found an example of
short float32to16(float f)
{
	short s_val;
	short *s=&s_val;
	*s = (* ((int *)&f) >> 16);
	return *s;
}



so would I create a byte array of a fixed size, convert the float to a byte array and then put them in the first array and then put it out as short?

or would this do the same

short truncate(float f)
{
   short output = Convert.ToSByte(f);
  return output;
}

This post has been edited by iccaros: 04 June 2007 - 03:45 PM


#2 User is offline   jcl 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1304
  • Joined: 30-August 04
  • Location:The Internet
  • Operating System:Arch

Posted 05 June 2007 - 12:58 AM

View Posticcaros, on Jun 4 2007, 01:27 PM, said:

or would this do the same

Convert.ToSByte() returns a signed byte based on the value of the argument, so no.

Do you really need 16-bit floats? Would, say, a 16-bit fixed-point (e.g., eight bit whole part, eight bit fraction, no exponent) type be adequate? And if you do need floats, do you really want want truncated IEEE singles? And is there any reason to roll your own compression instead of using one of the bazillion free audio codecs?

#3 User is offline   iccaros 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1292
  • Joined: 31-August 04
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 05 June 2007 - 01:28 AM

View Postjcl, on Jun 5 2007, 05:58 AM, said:

View Posticcaros, on Jun 4 2007, 01:27 PM, said:

or would this do the same

Convert.ToSByte() returns a signed byte based on the value of the argument, so no.

Do you really need 16-bit floats? Would, say, a 16-bit fixed-point (e.g., eight bit whole part, eight bit fraction, no exponent) type be adequate? And if you do need floats, do you really want want truncated IEEE singles? And is there any reason to roll your own compression instead of using one of the bazillion free audio codecs?

I am not really trying to create my own, the Navy does this with Time series data (Sonar), and I was given the C code above as an example , but I do not under stand it, it looks like it returns the first 16 most significant bits of a float as a signed short. I guess that when audio is recorded from an hydrophone it is doen at 32 bits and put in to a byte array looking like this.. 00100111011100101010001111110111, and to save broad cast(using HF so very limited bandwidth) space they chop off the last 16 bits and add them back with zeros for play back. so that is what I really need to do, I could change the whole number to a byte array and then fill a 16 bit array with the firt 16 bits and ignore the rest

do something like

byte[] truncate (float f)
{
   byte[] output = new byte[16];
   byte[] temp = BitConverter.GetBytes( f );
   for (int x = 0; x < 16; x++)
		{
		   output[x] = temp[x];
		 }
	return output;
}


In real life this will already be done for me by real equipment and I just add zeros, but for testing I will not have real data and I need to make sure I can create data that will look as close as I can.

Does this sound like it would do the same as the C++ example, or am I way off?

Thanks..

#4 User is offline   jcl 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1304
  • Joined: 30-August 04
  • Location:The Internet
  • Operating System:Arch

Posted 05 June 2007 - 01:59 AM

View Posticcaros, on Jun 4 2007, 11:28 PM, said:

I am not really trying to create my own, the Navy does this with Time series data (Sonar)

Ah. That's annoying.

Quote

and I was given the C code above as an example , but I do not under stand it, it looks like it returns the first 16 most significant bits of a float as a signed short.

Indeed it does. If you know that code works, you can just use it in C# in an unsafe block (with one extra cast). If you'd rather use BitConverter (it does seem more .NETy), you want the last two bytes of the array returned by GetBytes(). Perhaps

BitConverter.ToInt16(BitConverter.GetBytes(f), 2);

This post has been edited by jcl: 05 June 2007 - 02:09 AM


#5 User is offline   iccaros 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1292
  • Joined: 31-August 04
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 05 June 2007 - 11:12 AM

View Postjcl, on Jun 5 2007, 06:59 AM, said:

View Posticcaros, on Jun 4 2007, 11:28 PM, said:

I am not really trying to create my own, the Navy does this with Time series data (Sonar)

Ah. That's annoying.

Quote

and I was given the C code above as an example , but I do not under stand it, it looks like it returns the first 16 most significant bits of a float as a signed short.

Indeed it does. If you know that code works, you can just use it in C# in an unsafe block (with one extra cast). If you'd rather use BitConverter (it does seem more .NETy), you want the last two bytes of the array returned by GetBytes(). Perhaps

BitConverter.ToInt16(BitConverter.GetBytes(f), 2);




are the last two bytes the most significant, I thought they were the lest, or does that return the first two bytes.

#6 User is offline   jcl 

  • UberTechie
  • Group: Linux Experts
  • Posts: 1304
  • Joined: 30-August 04
  • Location:The Internet
  • Operating System:Arch

Posted 06 June 2007 - 12:03 AM

View Posticcaros, on Jun 5 2007, 09:12 AM, said:

are the last two bytes the most significant, I thought they were the lest, or does that return the first two bytes.

x86 is little-endian: the MSB is at the highest address.

This post has been edited by jcl: 06 June 2007 - 12:11 AM


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users