1

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 19896.6796875 is the frequnecy Max_index: 450 Sample rate: 44100.0 19379.8828125 is the frequnecy Max_index: 201 Sample rate: 44100.0 8656.34765625 is the frequnecy Max_index: 383 Sample rate: 44100.0 16494.43359375 is the frequnecy Max_index: 6 Sample rate: 44100.0 258.3984375 is the frequnecy Max_index: 385 Sample rate: 44100.0 16580.56640625 is the frequnecy Max_index: 100 Sample rate: 44100.0 4306.640625 is the frequnecy Max_index: 15 Sample rate: 44100.0 645.99609375 is the frequnecy Max_index: 421 Sample rate: 44100.0 18130.95703125 is the frequnecy Max_index: 256 Sample rate: 44100.0 11025.0 is the frequnecy Max_index: 0 Sample rate: 44100.0 0.0 is the frequnecy Max_index: 483 Sample rate: 44100.0 20801.07421875 is the frequnecy Max_index: 0 Sample rate: 44100.0 0.0 is the frequnecy Max_index: 134 Sample rate: 44100.0 5770.8984375 is the frequnecy Max_index: 123 Sample rate: 44100.0 5297.16796875 is the frequnecy Max_index: 2 Sample rate: 44100.0 86.1328125 is the frequnecy Max_index: 378 Sample rate: 44100.0 16279.1015625 is the frequnecy Max_index: 491 Sample rate: 44100.0 21145.60546875 is the frequnecy Max_index: 125 Sample rate: 44100.0 5383.30078125 is the frequnecy Max_index: 242 Sample rate: 44100.0 10422.0703125 is the frequnecy Max_index: 267 Sample rate: 44100.0 11498.73046875 is the frequnecy Max_index: 257 Sample rate: 44100.0 11068.06640625 is the frequnecy Max_index: 128 Sample rate: 44100.0 5512.5 is the frequnecy Max_index: 496 Sample rate: 44100.0 21360.9375 is the frequnecy Max_index: 350 Sample rate: 44100.0 15073.2421875 is the frequnecy Max_index: 75 Sample rate: 44100.0 3229.98046875 is the frequnecy Max_index: 127 Sample rate: 44100.0 5469.43359375 is the frequnecy Max_index: 473 Sample rate: 44100.0 20370.41015625 is the frequnecy Max_index: 35 Sample rate: 44100.0 1507.32421875 is the frequnecy Max_index: 420 Sample rate: 44100.0 18087.890625 is the frequnecy Max_index: 413 Sample rate: 44100.0 17786.42578125 is the frequnecy Max_index: 136 Sample rate: 44100.0 5857.03125 is the frequnecy Max_index: 93 Sample rate: 44100.0 4005.17578125 is the frequnecy Max_index: 251 Sample rate: 44100.0 10809.66796875 is the frequnecy Max_index: 431 Sample rate: 44100.0 18561.62109375 is the frequnecy Max_index: 490 Sample rate: 44100.0 21102.5390625 is the frequnecy Max_index: 221 Sample rate: 44100.0 9517.67578125 is the frequnecy Max_index: 364 Sample rate: 44100.0 15676.171875 is the frequnecy Max_index: 19 Sample rate: 44100.0 818.26171875 is the frequnecy Max_index: 487 Sample rate: 44100.0 20973.33984375 is the frequnecy Max_index: 350 Sample rate: 44100.0 15073.2421875 is the frequnecy Max_index: 243 Sample rate: 44100.0 10465.13671875 is the frequnecy Max_index: 384 Sample rate: 44100.0 16537.5 is the frequnecy Max_index: 403 Sample rate: 44100.0 17355.76171875 is the frequnecy Max_index: 56 Sample rate: 44100.0 2411.71875 is the frequnecy Max_index: 151 Sample rate: 44100.0 6503.02734375 is the frequnecy Max_index: 447 Sample rate: 44100.0 19250.68359375 is the frequnecy Max_index: 501 Sample rate: 44100.0 21576.26953125 is the frequnecy Max_index: 377 Sample rate: 44100.0 16236.03515625 is the frequnecy Max_index: 465 Sample rate: 44100.0 20025.87890625 is the frequnecy Max_index: 417 Sample rate: 44100.0 17958.69140625 is the frequnecy Max_index: 259 Sample rate: 44100.0 11154.19921875 is the frequnecy Max_index: 34 Sample rate: 44100.0 1464.2578125 is the frequnecy Max_index: 327 Sample rate: 44100.0 14082.71484375 is the frequnecy Max_index: 280 Sample rate: 44100.0 12058.59375 is the frequnecy Max_index: 271 Sample rate: 44100.0 11670.99609375 is the frequnecy Max_index: 92 Sample rate: 44100.0 3962.109375 is the frequnecy Max_index: 141 Sample rate: 44100.0 6072.36328125 is the frequnecy Max_index: 467 Sample rate: 44100.0 20112.01171875 is the frequnecy Max_index: 123 Sample rate: 44100.0 5297.16796875 is the frequnecy Max_index: 388 Sample rate: 44100.0 16709.765625 is the frequnecy Max_index: 122 Sample rate: 44100.0 5254.1015625 is the frequnecy Max_index: 386 Sample rate: 44100.0 16623.6328125 is the frequnecy Max_index: 464 Sample rate: 44100.0 19982.8125 is the frequnecy Max_index: 231 Sample rate: 44100.0 9948.33984375 is the frequnecy Max_index: 95 Sample rate: 44100.0 4091.30859375 is the frequnecy Max_index: 112 Sample rate: 44100.0 4823.4375 is the frequnecy Max_index: 260 Sample rate: 44100.0 11197.265625 is the frequnecy Max_index: 383 Sample rate: 44100.0 16494.43359375 is the frequnecy Max_index: 458 Sample rate: 44100.0 19724.4140625 is the frequnecy Max_index: 17 Sample rate: 44100.0 732.12890625 is the frequnecy Max_index: 129 Sample rate: 44100.0 5555.56640625 is the frequnecy Max_index: 262 Sample rate: 44100.0 11283.3984375 is the frequnecy Max_index: 259 Sample rate: 44100.0 11154.19921875 is the frequnecy Max_index: 0 Sample rate: 44100.0 0.0 is the frequnecy Max_index: 404 Sample rate: 44100.0 17398.828125 is the frequnecy Max_index: 471 Sample rate: 44100.0 20284.27734375 is the frequnecy Max_index: 119 Sample rate: 44100.0 5124.90234375 is the frequnecy Max_index: 239 Sample rate: 44100.0 10292.87109375 is the frequnecy Max_index: 385 Sample rate: 44100.0 16580.56640625 is the frequnecy Max_index: 485 Sample rate: 44100.0 20887.20703125 is the frequnecy Max_index: 14 Sample rate: 44100.0 602.9296875 is the frequnecy Max_index: 384 Sample rate: 44100.0 16537.5 is the frequnecy Max_index: 12 Sample rate: 44100.0 516.796875 is the frequnecy Max_index: 342 Sample rate: 44100.0 14728.7109375 is the frequnecy Max_index: 369 Sample rate: 44100.0 15891.50390625 is the frequnecy Max_index: 15 Sample rate: 44100.0 645.99609375 is the frequnecy Max_index: 22 Sample rate: 44100.0 947.4609375 is the frequnecy Max_index: 395 Sample rate: 44100.0 17011.23046875 is the frequnecy Max_index: 26 Sample rate: 44100.0 1119.7265625 is the frequnecy Max_index: 386 Sample rate: 44100.0 16623.6328125 is the frequnecy

dav191
  • 75
  • 2
  • 7
  • 1
    1) It's pretty hard for me to tell what you're trying to do. 2) You actually have a DFT, not an FFT (just terminology). 3) You would normally convert the DFT result to polar coordinates. 3) Unless it's a pure sine tone, what a person hears as a single "sound" or "note" is actually made of many harmonics at many frequencies. 4) This sort of thing is waaaay beyond the scope of the site. You should read textbooks or if you just want to tinker you should use a library. – Radiodef Apr 08 '16 at 06:09
  • One resource I found helpful is here: http://www.dspguide.com/pdfbook.htm – Radiodef Apr 08 '16 at 06:26
  • @dav191 Your sampling frequency is 44100 Hz. But what is the actual frequency of the note played? – LMR Apr 08 '16 at 06:51
  • You might perhaps use an existing DFT implementation (e.g. one that is tested and known to work). It will also be vastly more efficient. – marko Apr 08 '16 at 08:11
  • Maybe try asking at http://dsp.stackexchange.com/ That site is dedicated to digital signal processing questions. – Phil Freihofner Apr 08 '16 at 23:34
  • thank you brother @PhilFreihofner – dav191 Apr 09 '16 at 18:33

0 Answers0