I am trying to get FFT results of a pre-recorded sound which is saved as a .wav file.
For my FFT I used FFT and Complex class that was published here (FFT) and here (Complex).
This is the code that I have right now and when I checked the output file, I have almost similar values for each sound recordings. Why is that?
double [] fftdata;
File file;
file = new File(AudioSavePathInDevice);
File file2;
file2 = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + number + "FFTvalues.txt");
byte[] soundBytes;
try {
InputStream inputStream = getContentResolver().openInputStream(Uri.fromFile(file));
soundBytes = toByteArray(inputStream);
fftdata = calculateFFT(soundBytes); <-- this is where I call my calculate FFT method
String sfftdata = Arrays.toString(fftdata);
FileOutputStream fos = new FileOutputStream(file2);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF(sfftdata);
dos.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
This part of my code calls the file saved at "AudioSavePathInDevice", convert it into Byte Array with "toByteArray" method, and then calculate FFT with "calculateFFT" method. These methods are as follows.
Method to convert .wav file data to byte array.
public byte[] toByteArray(InputStream in) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int read = 0;
byte[] buffer1 = new byte [1024];
while (read != -1) {
read = in.read(buffer1,0,buffer1.length);
if (read != -1)
out.write(buffer1,0,read);
}
out.flush();
return out.toByteArray();
}
Method to calculate FFT.
public double[] calculateFFT(byte[] signal)
{
final int mNumberOfFFTPoints = 4096;
double mMaxFFTSample;
double mPeakPos;
double temp;
Complex[] y;
Complex[] complexSignal = new Complex[mNumberOfFFTPoints];
double[] absSignal = new double[mNumberOfFFTPoints/2];
for(int i = 0; i < mNumberOfFFTPoints; i++){
temp = (double)((signal[2*i] & 0xFF) | (signal[2*i+1] << 8)) / 32768.0F;
complexSignal[i] = new Complex(temp,0.0);
}
y = FFT.fft(complexSignal);
mMaxFFTSample = 0.0;
mPeakPos = 0;
for(int i = 0; i < (mNumberOfFFTPoints/2); i++)
{
absSignal[i] = Math.sqrt(Math.pow(y[i].re(), 2) + Math.pow(y[i].im(), 2));
if(absSignal[i] > mMaxFFTSample)
{
mMaxFFTSample = absSignal[i];
mPeakPos = i;
}
}
return absSignal;
}