Why is this FFT code not working to extract frequencies from a wav audio? I'm quite new to audio processing and i'm trying to extract frequencies from a wav file. As research papers stated i put the wav audio into a byte array and segmented and trying to apply FFT to get the frequencies from the audio. However the problem is even when i enter a audio track with only one note played (With no noise) i get a lot of frequencies as a result where as i'm supposed to get one.

My guess is something is wrong with the FFT code because the audio gets segmented properly.

Thanks everyone in advance,

My fft code

public void getFFT(double[] x, int a) {

double amp[] = new double[512];

double firstSum;
double secSum;
double twoPInjk;
double[][] s = new double[512][2];
for (int j = 0; j &lt; 512; j++) {
    firstSum = 0;
    secSum = 0;
    FreqArr[j] = (j * sampleRate) / (512);
    for (int k = 0; k &lt; 512; k++) {
        twoPInjk = ((2 * Math.PI) / 512) * (j * k);
        firstSum += x[k] * Math.cos(twoPInjk);
        secSum += x[k] * Math.sin(twoPInjk);
    }
    amp[j] = Math.abs(Math.sqrt(Math.pow(firstSum, 2) + Math.pow(secSum, 2)));
}
highest(amp, a);
}

public void highest(double[] amp, int a) {
String note;
double all = 0;
double max_magnitude = -1;
int max_index = -1;

for (int i = 0; i &lt; (512); i++) {

    all = all + amp[i];

    if (amp[i] &gt; max_magnitude) {
        max_magnitude = amp[i];
        max_index = i;
    }
}
double freq = 0;
int cnt = 0;

freq = max_index * (sampleRate / 2) / (512);
System.out.println("Maximum_index: " + max_index);
System.out.println("Sample rate: " + sampleRate);

  System.out.println(freq + " is the frequnecy");
}


my result (for the 1 second audio with only one note played)

Sample rate 44100 My data size 70376 My myBitsPerSample 16 My channels 2 My myByteRate 176400 My Length 0.7983219954648526 0.7983219954648526 44100.0 Max_index: 56 Sample rate: 44100.0 2411.71875 is the frequnecy Max_index: 256 Sample rate: 44100.0 11025.0 is the frequnecy Max_index: 256 Sample rate: 44100.0 11025.0 is the frequnecy Max_index: 129 Sample rate: 44100.0 5555.56640625 is the frequnecy Max_index: 511 Sample rate: 44100.0 22006.93359375 is the frequnecy Max_index: 256 Sample rate: 44100.0 11025.0 is the frequnecy Max_index: 152 Sample rate: 44100.0 6546.09375 is the frequnecy Max_index: 422 Sample rate: 44100.0 18174.0234375 is the frequnecy Max_index: 26 Sample rate: 44100.0 1119.7265625 is the frequnecy Max_index: 462 Sample rate: 44100.0 


and it goes on.
Your DFT is working as expected at extracting frequency information.  One musical note at one pitch usually contains a lot of spectral frequencies: from harmonics and overtones plus broadband noise from any transients, such as note attack and decay.  Furthermore, the max magnitude frequency from an actual sound recording is often not the same as the heard note pitch frequency (one possibility is the magnitude peak is instead a strong overtone).

Instead, search for pitch detection/estimation algorithms.  The good (working, more reliable) ones will do a lot more than just an FFT peak magnitude search.