1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ you might call that a 512-point DFT, but there is no leading "F" in it. $\endgroup$ – robert bristow-johnson Apr 9 '16 at 20:47
1
$\begingroup$

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.

$\endgroup$
  • 1
    $\begingroup$ or maybe a lot less, but they'll work better. $\endgroup$ – robert bristow-johnson Apr 9 '16 at 20:43
0
$\begingroup$

When a single key is pressed upon a piano, what we hear is not just one frequency of sound vibration, but a composite of multiple sound vibrations occurring at different mathematically related frequencies.

The elements of this composite of vibrations at differing frequencies are referred to as harmonics or partials. For instance, if we press the Middle C key on the piano, the individual frequencies of the composite's harmonics will start at 261.6 Hz as the fundamental frequency, 523 Hz would be the 2nd Harmonic, 785 Hz would be the 3rd Harmonic, 1046 Hz would be the 4th Harmonic, etc. The later harmonics are integer multiples of the fundamental frequency, 261.6 Hz ( ex: 2 x 261.6 = 523, 3 x 261.6 = 785, 4 x 261.6 = 1046 ). Below is a logarithmic sonogram of 3 seconds of a guitar solo on a mp3 recording. It will show you how the harmonics appear for an individual note on a guitar.

You could read the linked Wikipedia article on Pitch Detection to see a more elaborate definition of a note in musical terms. https://en.wikipedia.org/wiki/Transcription_(music)#Pitch_detection

And here's a link to an Answer which gives access to C++ source code that with do Pitch Detection by picking the most dominant note being plyed, such as the notes of a guitar or saxophone solo. Tips for improving pitch detection

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.