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]

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.
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.
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)