title: Android mic audio, not getting expected frequency
body: <p>I have a code which detects Android mic audio and calculates frequency.
  Though, the frequency what I'm getting is not the same as tested.
  E.g. sound 260Hz is outputted as 62Hz.<br>
  I have no clue why I'm not getting the expected frequency.<br>
  FFT I'm using is from the website
  <a href="http://jfftpack.sourceforge.net/" rel="nofollow">http://jfftpack.sourceforge.net/</a><br>
  Please help me solve this problem.</p>
  
  <pre><code>/**
   * 
   * @reference 
   *            http://stackoverflow.com/questions/5511250/capturing-sound-for-
   *            analysis -and-visualizing-frequencies-in-android
   * 
   */
  public class RecordAudio extends AsyncTask&lt;Void, Double, Void&gt; {
  
      @Override
      protected Void doInBackground(Void... arg0) {
  
          try {
              int bufferSize = AudioRecord.getMinBufferSize(sampleRate,
                      channelConfiguration, audioEncoding);
  
              AudioRecord audioRecord = new AudioRecord(
                      MediaRecorder.AudioSource.MIC, sampleRate,
                      channelConfiguration, audioEncoding, bufferSize);
  
              short[] buffer = new short[blockSize];
              double[] toTransform = new double[blockSize];
  
              audioRecord.startRecording();
  
              while (started) {
                  double peakFreq = 0;
                  int bufferReadResult = audioRecord.read(buffer, 0,
                          blockSize);
                  int elementSize = blockSize / numSplit;
                  double max = 0;
  
                  for (int i = 0; i &lt; blockSize &amp;&amp; i &lt; bufferReadResult; i++) {
                      toTransform[i] = (double) buffer[i] / 32768.0;
                  }
  
                  for (int i = 0; i &lt; numSplit; i++) {
                      double[] chunk = new double[elementSize];
  
                      System.arraycopy(toTransform, i * elementSize, chunk,
                              0, elementSize);
  
                      HanningWindow(chunk, 0, chunk.length);
  
                      transformer.ft(chunk);
  
                      max = getMaxFrequency(chunk);
  
                      if (max &gt; peakFreq)
                          peakFreq = max;
                  }
  
                  publishProgress(peakFreq);
              }
  
              audioRecord.stop();
  
          } catch (Throwable t) {
              t.printStackTrace();
              Log.e("AudioRecord", "Recording Failed");
          }
          return null;
      }
  
      @Override
      protected void onProgressUpdate(Double... frequency) {
          if (frequency[0] &gt; 25d)
              textView.setText(Double.toString(frequency[0]));
      }
  
      private double getMaxFrequency(double[] fft) {
          int i, mgIndex = 0, size = fft.length / 2;
          double[] re = new double[size];
          double[] im = new double[size];
          double[] mg = new double[size];
  
          i = 0;
          re[i] = fft[i];
          im[i] = 0;
          mg[i] = Math.sqrt(re[i] * re[i]);
  
          for (i = 1; i &lt; size - 1; i++) {
              // set real part
              re[i] = fft[i * 2 - 1];
              // set imaginary part
              im[i] = fft[i * 2 - 2];
              // Calculate magnitude
              mg[i] = Math.sqrt(re[i] * re[i] + im[i] * im[i]);
          }
  
          re[i] = fft[fft.length - 1];
          im[i] = 0;
          mg[i] = Math.sqrt(re[i] * re[i]);
  
          for (i = 1; i &lt; mg.length; i++) {
              if (mg[i] &gt; mg[mgIndex])
                  mgIndex = i;
          }
  
          return mgIndex * sampleRate / blockSize;
      }
  
      /**
       * 
       * @reference 
       *            http://stackoverflow.com/questions/11600515/hanning-von-hann
       *            -window
       * @param signal_in
       * @param pos
       * @param size
       */
      public void HanningWindow(double[] signal_in, int pos, int size) {
          for (int i = pos; i &lt; pos + size; i++) {
              int j = i - pos; // j = index into Hann window function
              signal_in[i] = signal_in[i] * 0.5
                      * (1.0 - Math.cos(2.0 * Math.PI * j / size));
          }
      }
  }
  </code></pre>