Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > PLEASE HELP - Strange problem converting from bytes to float

Reply
Thread Tools

PLEASE HELP - Strange problem converting from bytes to float

 
 
cpptutor2000@yahoo.com
Guest
Posts: n/a
 
      03-23-2008
Could some Java guru please help me ? I am trying to create a simple
application that will capture audio from the PC's default listening
device and do some FFT with the data. The audio bytes are collected in
a byte array, and the main problem I am having right now is how to
convert them to the floating point numbers that would be used in the
computation of the FFT. For prototyping purposes, I am using a
floating point array as :

private final int len = 4096;

float [][] floatArray = new float[len][2];

The reason I am using using the 2-D array is that one dimension would
hold the real and the other the imaginary component of the complex
numbers to be used for the FFT. To convert consecutive chunks of 4
bytes to get a floating point number, I am using the following
function:

public static float arr2float (byte[] arr, int start)
{
int i = 0;
int len = 4;
int cnt = 0;
byte[] tmp = new byte[len];
for (i = start; i < (start + len); i++)
{
tmp[cnt] = arr[i];
cnt++;
}
int accum = 0;
i = 0;
for ( int shiftBy = 0; shiftBy < 32;
shiftBy += 8 )
{
accum |= ( (long)( tmp[i] & 0xff )
) << shiftBy;
i++;
}
return Float.intBitsToFloat(accum);
}

The actual conversion from the raw bytes to the flaoting point numbers
is done via a loop as:

for (int start = 0; start < 4096; start += 4)
{
floatArray[cnt][0] =
RecordUtility.arr2float(recordedSoundArray,
start);
floatArray[cnt][1] = 0;
System.out.println(cnt + " "
+floatArray[cnt][0]);
cnt++;
}

Unfortunately, the output of the print statement looks like:
0 12690.33
1 1.64703E-40
2 3428.0837
3 2.0703801E-19
4 2.24E-44
5 9.1837E-41
6 1.121E-41
7 2.2421E-41
8 1.469371E-39
9 2.8175146E20
10 1.64653E-40

This is what bothers me that the numbers are huge or very small, and
there are quite a few 'NaN's.

Could someone please kindly provide some hints as what I might be
doing wrong?

Thanks in advance for your help.

 
Reply With Quote
 
 
 
 
Mark Space
Guest
Posts: n/a
 
      03-23-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Could some Java guru please help me ? I am trying to create a simple
> application that will capture audio from the PC's default listening
> device and do some FFT with the data. The audio bytes are collected in



Well for starters, if you're using a Clip, then sample can take a
variable number of bits, not bytes. And they can be either big endian
or little endian.

<http://java.sun.com/javase/6/docs/api/javax/sound/sampled/AudioFormat.html>

You may want to clarify first what your data actually looks like. Then
we can give you a hand with the conversion.

 
Reply With Quote
 
 
 
 
cpptutor2000@yahoo.com
Guest
Posts: n/a
 
      03-23-2008
On Mar 22, 7:47 pm, Mark Space <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > Could some Java guru please help me ? I am trying to create a simple
> > application that will capture audio from the PC's default listening
> > device and do some FFT with the data. The audio bytes are collected in

>
> Well for starters, if you're using a Clip, then sample can take a
> variable number of bits, not bytes. And they can be either big endian
> or little endian.
>
> <http://java.sun.com/javase/6/docs/api/javax/sound/sampled/AudioFormat...>
>
> You may want to clarify first what your data actually looks like. Then
> we can give you a hand with the conversion.


I am sorry I did not provide that information. I am using PCM
encoding, with sampling rate = 8000 and channel = 1 (mono)
 
Reply With Quote
 
Logan Shaw
Guest
Posts: n/a
 
      03-23-2008
(E-Mail Removed) wrote:
> On Mar 22, 7:47 pm, Mark Space <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>> Could some Java guru please help me ? I am trying to create a simple
>>> application that will capture audio from the PC's default listening
>>> device and do some FFT with the data. The audio bytes are collected in

>> Well for starters, if you're using a Clip, then sample can take a
>> variable number of bits, not bytes. And they can be either big endian
>> or little endian.
>>
>> <http://java.sun.com/javase/6/docs/api/javax/sound/sampled/AudioFormat...>
>>
>> You may want to clarify first what your data actually looks like. Then
>> we can give you a hand with the conversion.

>
> I am sorry I did not provide that information. I am using PCM
> encoding, with sampling rate = 8000 and channel = 1 (mono)


Looking at AudioFormat.Encoding, it appears likely that your input
bytes represent plain old simple base-2 integer sample values. But
you're calling Float.intBitsToFloat(), which assumes they are IEEE
floating point values. Instead of all that, once you've converted
your bytes into an integer, you probably just want to use a cast to
convert the integer into a float.

Also, you appear to be assuming that one sample (for one channel)
is 32 bits wide based on this loop:

for ( int shiftBy = 0; shiftBy < 32;
shiftBy += 8 )

It's fairly uncommon (but not impossible, I guess) for anyone
to use 32 bit integers for transferring audio. 24-bit integers
are good enough, and I don't think anyone has ever built an A-to-D
that can give 32 bits worth of integer resolution and not have
the low order 8 bits be anything other than noise. So I would
suspect your incoming samples are either 16-bit integers or
24-bit integers. Or maybe even 8-bit integers if you are using
a sample rate as low as 8000 Hz.

- Logan
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-23-2008
On Sat, 22 Mar 2008 17:34:44 -0700 (PDT), "(E-Mail Removed)"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>This is what bothers me that the numbers are huge or very small, and
>there are quite a few 'NaN's.


The obvious first. See http://mindprod.com/jgloss/endian.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      03-23-2008
Logan Shaw wrote:
> floating point values. Instead of all that, once you've converted
> your bytes into an integer, you probably just want to use a cast to
> convert the integer into a float.


You don't need a cast because it's a widening conversion.

--
Lew
 
Reply With Quote
 
Logan Shaw
Guest
Posts: n/a
 
      03-23-2008
Lew wrote:
> Logan Shaw wrote:
>> floating point values. Instead of all that, once you've converted
>> your bytes into an integer, you probably just want to use a cast to
>> convert the integer into a float.

>
> You don't need a cast because it's a widening conversion.


Ah, good point. Not being terribly fresh on the kinds of conversions,
I simply assumed that any conversion that could result in loss of
information would require a cast. But as you pointed out, that's not
how Java works: some widening conversions can result in loss of
information; they will not lose information about rough magnitude
of the number, but they can lose precision.

- Logan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
converting 'float (*)[4]' to 'const float**' mathieu C++ 9 09-15-2007 09:10 AM
Converting 4 bytes to a float Larry.Martell@gmail.com Perl Misc 6 05-13-2007 07:30 PM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments