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 < 512; j++) {
firstSum = 0;
secSum = 0;
FreqArr[j] = (j * sampleRate) / (512);
for (int k = 0; k < 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 < (512); i++) {
all = all + amp[i];
if (amp[i] > 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.