concatenating two consequtive bytes in a byte array to a float array

Hello everyone,

I am working on a signal processing project. In this program I will be reading a wav file using audioInputStream class.

I use the method read( bytes ) ie audioInputStream.read(chunk) where chunk is an byte array to read the wav file.

When I read the wav file, the wav file will be stored in a byte array. For example 600000*1 array.

But I want it to store in a float array, such that two consecutive bytes in the above array forms a single float value in this float array.

ie, I should get a float array of size 300000*1 array.

I dont know how to concatenate the two consequtive bytes into a float value.

Thanks for reading my query and if you can please help me.

I thank you very much,

chanu_26.

[774 byte] By [chanu_26a] at [2007-10-2 10:59:30]
# 1

to concatenate up to four bytes to an int, you can do the following:

byte[] bytes = /* an array of up to four bytes */

int result = 0;

boolean littleEndian = /* true if the least significant byte comes first */;

for (int i=0; i<bytes.length; i++) {

result |= (bytes[i] >< 0 ? 256 + bytes[i] : (int) bytes[i]) << ((littleEndian ? i : bytes.length - i - 1) * 8);

}

From there, you should be able to cast the int to a float.

th1rd3y3a at 2007-7-13 3:28:02 > top of Java-index,Java Essentials,New To Java...
# 2
@Op. What do you mean? A byte in java is always 8 bits, that makes 16 bits for two bytes, but a float is always 32 bits in java. So how will you create a float out of two bytes?Kaj
kajbja at 2007-7-13 3:28:02 > top of Java-index,Java Essentials,New To Java...
# 3
convert two bytes (8 bits) to an int (16 bits in Java), then cast this int to a float
th1rd3y3a at 2007-7-13 3:28:02 > top of Java-index,Java Essentials,New To Java...
# 4

Hello everyone,

Thanks for the response. I will make my question more simple.

Actually I done this project in matlab. Its much slower so I switched to java to use the threads concept and make it much faster.

My project takes a bird song that is stored in a storage media and identifies the syllables in the song. There are 5 unique syllables in these bird songs that will be repeated. My program should identify the syllable.

So I have to read the wav files and analyse them to find the syllables. The wav files are stored in samples and when I use matlab method wavread it returns an array of n*1 size and each entry is in the form of example -0.00023 a float type.

When I used java audioInputStream class which has a read method. I am getting an array of 2n*1 size and each entry is byte. In order to use my existing algorithm I should some how convert the bytes to float type.

I hope that I made my question clearer.

Thanks for reading, If possible please help me.

chanu_26.

chanu_26a at 2007-7-13 3:28:02 > top of Java-index,Java Essentials,New To Java...
# 5

OK, here's some code that should do the whole byte array - to - float array conversion...hopefully this works for you :-)

// byte[] bytes is your bytes array of size 2n*1

// boolean littleEndian is true if your data is little endian (i.e. least significant byte comes first)

float[] floats = new float[bytes.length / 2];

// initialize the float[] array to 0's; this may or may not be necessary

for(int i=0; i<floats.length; i++) { floats[i] = (float) 0; }

// perform the bytes-to-float conversion

for (int i=0; i><bytes.length; i+=2) {

floats[i / 2] |= (bytes[i] >< 0 ? 256 + bytes[i] : (int) bytes[i]) << ((littleEndian ? i : bytes.length - i - 1)*8);

floats[i / 2] |= (bytes[i+1] < 0 ? 256 + bytes[i+1] : (int) bytes[i+1]) << ((littleEndian ? (i+1) : bytes.length - (i+1) - 1)*8);

}

// now the float[] array has n*1 entries

(note that if you see something like "><", it means "less than"...my character set is messed up at the moment)

th1rd3y3a at 2007-7-13 3:28:02 > top of Java-index,Java Essentials,New To Java...
# 6
> // initialize the float[] array to 0's; this may or> may not be necessary> for(int i=0; i<floats.length; i++) { floats > (float) 0; }That will never be necessary since they already are set to zero when the array is created.Kaj
kajbja at 2007-7-13 3:28:02 > top of Java-index,Java Essentials,New To Java...