title: Calculate FFT from audio file
body: <p>Before, I asked question about <a href="https://stackoverflow.com/questions/17429407/get-frequency-wav-audio-using-fft-and-complex-class">Get frequency wav audio using FFT and Complex class</a> ,</p>
  
  <p>There, I need to calculate FFT value from AudioRecord input --> from microphone , I somehow managed to get the FFT value...</p>
  
  <p>Now I need to calculate FFT value from *.wav audio file that I saved before, 
  I saved the audio in 'raw' folder inside 'res' folder from my project</p>
  
  <p>I still using the same FFT Class: <a href="http://www.cs.princeton.edu/introcs/97data/FFT.java" rel="noreferrer">http://www.cs.princeton.edu/introcs/97data/FFT.java</a></p>
  
  <p>The complex class to go with it: <a href="http://introcs.cs.princeton.edu/java/97data/Complex.java.html" rel="noreferrer">http://introcs.cs.princeton.edu/java/97data/Complex.java.html</a></p>
  
  <p>I use this method to read audio file from my raw foldern, then I call method calculateFFT to go with it</p>
  
  <pre><code>private static final int RECORDER_BPP = 16;
    private static final int RECORDER_SAMPLERATE = 44100;
    private static final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
    private static final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;
  
  
  private void asli(){
  
              int counter = 0;
              int data;
              InputStream inputStream  = getResources().openRawResource(R.raw.b1);
              DataInputStream dataInputStream = new DataInputStream(inputStream);
              List&lt;Integer&gt; content = new ArrayList&lt;Integer&gt;(); 
  
              try {
                  while ((data = dataInputStream.read()) != -1) {
                      content.add(data);
                      counter++; }
              } catch (IOException e) {
                  e.printStackTrace();}
  
                  int[] b = new int[content.size()];
                  int cont = 0;
                  byte[] audio = convertArray(b);
          }
  </code></pre>
  
  <p>Method to convert to byte</p>
  
  <pre><code>public byte[] convertArray(int[] array) { 
  
              int minBufferSize = AudioTrack.getMinBufferSize(RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING);
                  AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING,minBufferSize, AudioTrack.MODE_STREAM);
  
          byte[] newarray = new byte[array.length];
          for (int i = 0; i &lt; array.length; i++) {
          newarray[i] = (byte) ((array[i]) &amp; 0xFF);       }
  
              absNormalizedSignal = calculateFFT(newarray);
              return newarray;
          }
  </code></pre>
  
  <p>And this is the CalculateFFT method</p>
  
  <pre><code>public double[] calculateFFT(byte[] signal)
          {           
              final int mNumberOfFFTPoints =1024;
              double mMaxFFTSample;
              double temp;
              Complex[] y;
              Complex[] complexSignal = new Complex[mNumberOfFFTPoints];
              double[] absSignal = new double[mNumberOfFFTPoints/2];
  
              for(int i = 0; i &lt; mNumberOfFFTPoints; i++){
                  temp = (double)((signal[2*i] &amp; 0xFF) | (signal[2*i+1] &lt;&lt; 8)) / 32768.0F;
                  complexSignal[i] = new Complex(temp,0.0);
              }
  
              y = FFT.fft(complexSignal);
  
              mMaxFFTSample = 0.0;
              mPeakPos = 0;
              for(int i = 0; i &lt; (mNumberOfFFTPoints/2); i++)
              {
                   absSignal[i] = Math.sqrt(Math.pow(y[i].re(), 2) + Math.pow(y[i].im(), 2));
                   if(absSignal[i] &gt; mMaxFFTSample)
                   {
                       mMaxFFTSample = absSignal[i];
                       mPeakPos = i;
                   } 
              }
  
              return absSignal;
  
          }
  </code></pre>
  
  <p>I used this CalculateFFT method too to process audio from AudioRecorder --> that one with microphone input before... I managed to get value from the AudioRecorder, but I failed to get value from my audio file... I'm not planning to play the audio.. I just need to process it with FFT.</p>
  
  <p>Is there any wrong with my code ?? :o Seems like I fail at getting value from method Asli(); But I dont know which part is wrong..</p>
  
  <p>Any help would be appreciated... :)
  Thanks</p>