How to convert a 16-bit bit of a PCM byte array to a double or floating array?

I am trying to execute Fast Fourier Transform in a .3gpp audio file. The file contains a small 5-second recording at 44100 kHz from the telephone microphone.

Every Java FFT algorithm I can find only accepts double [], float [], or complex [] inputs for obvious reasons, but I read the audio file in a byte array, so I'm kind of confused where I go from here. The only thing I could find was to answer the previous question:

Android FFT to get a specific frequency using audio recordings

But I'm not sure if this is so or not, this is the right procedure. Anyone with understanding?

+8
java android fft
source share
2 answers

There is no alternative. You must start the loop and discard each element of the array separately.

I do the same for shorts that fft as floats:

public static float[] floatMe(short[] pcms) { float[] floaters = new float[pcms.length]; for (int i = 0; i < pcms.length; i++) { floaters[i] = pcms[i]; } return floaters; } 

EDIT 4/26/2012 based on comments

If you really have 16-bit PCM, but it's like a byte [], you can do this:

 public static short[] shortMe(byte[] bytes) { short[] out = new short[bytes.length / 2]; // will drop last byte if odd number ByteBuffer bb = ByteBuffer.wrap(bytes); for (int i = 0; i < out.length; i++) { out[i] = bb.getShort(); } return out; } 

then

 float[] pcmAsFloats = floatMe(shortMe(bytes)); 

If you are working with a strange and poorly designed class that first gave you an array of bytes, the developers of this class should have packed the bytes to match how Java converts bytes (2 at a time) to shorts.

+10
source share
 byte[] yourInitialData; double[] yourOutputData = ByteBuffer.wrap(bytes).getDouble() 
-3
source share

All Articles