2

for my project i need to calculate or get the frequency values form a wav file. so far i have managed to record an crated a wav file.

    private static final int RECORDER_BPP = 16;
         private static final String AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
         private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
         private static final String AUDIO_RECORDER_TEMP_FILE = "record_temp.raw";
         private  final int RECORDER_SAMPLERATE = 44100;
         private  final int RECORDER_CHANNELS = AudioFormat.CHANNEL_IN_STEREO;
         private  final int RECORDER_AUDIO_ENCODING = AudioFormat.ENCODING_PCM_16BIT;

        bufferSize = udioRecord.getMinBufferSize(44100,AudioFormat.CHANNEL_IN_STEREO,      AudioFormat.ENCODING_PCM_16BIT);

recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
                                             RECORDER_SAMPLERATE, RECORDER_CHANNELS,RECORDER_AUDIO_ENCODING, bufferSize);

i need to get the frequency values and save it on a array to display it on a graph.. my problem is how can i mange to get the frequency values form this type of WAV file.? thank you...

gamal
  • 1,587
  • 2
  • 21
  • 43

1 Answers1

0

Basically, you need to use FFT.

Here is an example of a FFT Java Class

And Here is a sample code:

Creating a method to calculate array value from audio...

public double[] calculateFFT(byte[] signal)
    {           
        final int mNumberOfFFTPoints =1024;
        double mMaxFFTSample;

        double temp;
        Complex[] y;
        Complex[] complexSignal = new Complex[mNumberOfFFTPoints];
        double[] absSignal = new double[mNumberOfFFTPoints/2];

        for(int i = 0; i < mNumberOfFFTPoints; i++){
            temp = (double)((signal[2*i] & 0xFF) | (signal[2*i+1] << 8)) / 32768.0F;
            complexSignal[i] = new Complex(temp,0.0);
        }

        y = FFT.fft(complexSignal); // --> Here I use FFT class

        mMaxFFTSample = 0.0;
        mPeakPos = 0;
        for(int i = 0; i < (mNumberOfFFTPoints/2); i++)
        {
             absSignal[i] = Math.sqrt(Math.pow(y[i].re(), 2) + Math.pow(y[i].im(), 2));
             if(absSignal[i] > mMaxFFTSample)
             {
                 mMaxFFTSample = absSignal[i];
                 mPeakPos = i;
             } 
        }

        return absSignal;

    }

Based on answer from Here.

Community
  • 1
  • 1
gilonm
  • 1,829
  • 1
  • 12
  • 22
  • i try to use this method... but still i couldn't print the values... i write for loop to print the array values which is return from the calculateFFt() method. does it return the frequency values from the calculateFFT() .??? if it is working for you can you give me the android project for me as a download link also i want to know why mNumberofFFTPonits declare as 1024 and what does 0X FF and 32768.0F means?? – gamal Sep 29 '14 at 10:53
  • for(int i= 0;i – gamal Sep 29 '14 at 10:59