Wyodrębnij częstotliwość audio z instrumentu, aby znaleźć nutę

Próbuję opracować aplikację na Androida, która wyodrębnia częstotliwość audio z instrumentu. Używam metody szybkiej transformacji Fouriera z Jtransforms. Oto, co mam do tej pory:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new readFrequencies().execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

private class readFrequencies extends AsyncTask<Void,Integer,Integer> {

        @Override
        protected Integer doInBackground(Void... arg0) {
            AudioRecord recorder = null;
            int bufferSize = 0;
            boolean recording = true;

            int rate = 8000;
            short audioFormat = AudioFormat.ENCODING_PCM_16BIT;
            short channelConfig = AudioFormat.CHANNEL_IN_MONO;

            try {
                bufferSize = AudioRecord.getMinBufferSize(rate,channelConfig, audioFormat);

                recorder = new AudioRecord(AudioSource.DEFAULT, rate, 
                    channelConfig, audioFormat, bufferSize);

                if (recorder.getState() == AudioRecord.STATE_INITIALIZED) {
                    /*
                     *  Android 4.1.2
                     * 
                    int recorder_id = recorder.getAudioSessionId();
                    if (NoiseSuppressor.isAvailable()) NoiseSuppressor.create(recorder_id);
                    */
                }
                else {
                    Toast.makeText(getApplicationContext(), 
                            "Error en la inicialización", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {}

            short[] audioData = new short[bufferSize];

            if (recorder != null) {
                while (recording) {
                    if (recorder.getRecordingState() == AudioRecord.RECORDSTATE_STOPPED) {
                        recorder.startRecording();
                    }
                    else {
                        int numshorts = recorder.read(audioData,0,audioData.length);
                        if ((numshorts != AudioRecord.ERROR_INVALID_OPERATION) && 
                            (numshorts != AudioRecord.ERROR_BAD_VALUE)) {

                            //  Hann
                            double[] preRealData = new double[bufferSize];
                            double PI = 3.14159265359;
                            for (int i = 0; i < bufferSize; i++) {
                                double multiplier = 0.5 * (1 - Math.cos(2*PI*i/(bufferSize-1)));
                                preRealData[i] = multiplier * audioData[i];
                            }

                            DoubleFFT_1D fft = new DoubleFFT_1D(bufferSize);
                            double[] realData = new double[bufferSize * 2];

                            for (int i=0;i<bufferSize;i++) {
                                realData[2*i] = preRealData[i];
                                realData[2*i+1] = 0;    
                            }
                            fft.complexForward(realData);

                            double magnitude[] = new double[bufferSize / 2];

                            for (int i = 0; i < magnitude.length; i++) {
                                double R = realData[2 * i];
                                double I = realData[2 * i + 1];

                                magnitude[i] = Math.sqrt(I*I + R*R);
                            }

                            int maxIndex = 0;
                            double max = magnitude[0];
                            for(int i = 1; i < magnitude.length; i++) {
                                if (magnitude[i] > max) {
                                    max = magnitude[i];
                                    maxIndex = i;
                                }
                            }

                            int frequency = rate * maxIndex / bufferSize;
                            publishProgress(frequency);
                        }
                        else {
                            if (numshorts == AudioRecord.ERROR_BAD_VALUE) {
                                Toast.makeText(getApplicationContext(), 
                                        "ERROR_BAD_VALUE", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                Toast.makeText(getApplicationContext(), 
                                        "ERROR_INVALID_OPERATION", Toast.LENGTH_SHORT).show();
                            }

                            return -1;
                        }
                    }
                }

                if (recorder.getState() == AudioRecord.RECORDSTATE_RECORDING) 
                    recorder.stop(); //stop the recorder before ending the thread
                recorder.release();
                recorder=null;
            }
            return 0;
        }

        protected void onProgressUpdate(Integer... f) {
            TextView texto = (TextView) findViewById(R.id.texto);
            texto.setText(String.valueOf(f[0]));
        }

        protected void onPostExecute(Integer f) {
            TextView texto = (TextView) findViewById(R.id.texto);
            int frecuencias = f.intValue();
            texto.setText(String.valueOf(frecuencias));
        }
}

}

Dzięki temu kodowi jestem w stanie uzyskać dokładną częstotliwość z generatora częstotliwości, który wytwarza czyste sygnały. Jednak gdy próbuję tego samego z instrumentem, otrzymuję losowe częstotliwości. Wiem, że jeśli chodzi o rzeczywiste instrumenty, wytwarzane sygnały zawierają harmoniczne, które mogą mieć wpływ na wynik końcowy, ale nie wiem, jak uzyskać rzeczywistą częstotliwość w tym przypadku. Czy ktoś może mi pomóc?

Użyłem TarsosDSP i próbowałem też metody autokorelacji, ale nie byłem w stanie uzyskać tego, co chciałem.

Z góry dziękuję.

questionAnswers(2)

yourAnswerToTheQuestion