简体   繁体   中英

Audio file reading for FFT

first time asking though i have been visiting for some time.

Here's the problem:

I'm currently trying to isolate the base frequenciy of a signal contained in a WAVE data file with these properties:

  • PCM Audio Format ie Liner Quantization
  • 8000 Hz Sample Rate
  • 16 Bits Per Sample
  • 16000 Byte Rate

One Channel only there is no interleaving.

Getting the byte value:

System.IO.FileStream WaveFile = System.IO.File.OpenRead(@"c:\tet\fft.wav");
        byte[] data = new byte[WaveFile.Length];
        WaveFile.Read(data,0,Convert.ToInt32(WaveFile.Length));

Converting it to an Array of Doubles:

 for (int i = 0; i < 32768; i++)//this is only for a relatively small chunk of the file 
        {
           InReal[i] =BitConverter.ToDouble(data, (i + 1) * 8 + 44);
        }

and finanly passing it to a Transform Function.

       FFT FftObject = new FFT();
       FftObject.Transform(InReal, InImg, 0, 32768, out outReal, out outImg, false);

Now the first question, as i understand the PCM values of the wav file should be in the boundaries of -1 and 1, but when converting to Double i get this values:

 2.65855908666825E-235
 2.84104982662944E-285
-1.58613492930337E+235
-1.25617351166869E+264
 1.58370933499389E-242
 6.19284549187335E-245
-2.92969500042228E+254
-5.90042665390976E+226
 3.11954507295188E-273
 3.06831908609091E-217
 NaN
 2.77113146323761E-302
 6.76597919848376E-306
-1.55843653898344E+291

These are the firs few of the array in those limits is the rest of array too.

My conclusion of this is that i have some sort of code malfunction but i can seem to be able to find it. Any help would be appreciated.

And the second question, because i'm only providing real data to the FFT algorithm in the response vector should i expect only Real part data too??

Thank you very much.

I was finally able to find out what was going wrong it seems that i didn't accounted for the pulse code modulation of the signal in the data representation, and because i found many unanswered questions here on wave file preparing for Fourier transformation here is the code in a function that prepares the wave file.

public static Double[] prepare(String wavePath, out int SampleRate)

    {
        Double[] data;
        byte[] wave;
        byte[] sR= new byte[4];
        System.IO.FileStream WaveFile = System.IO.File.OpenRead(wavePath);
        wave = new byte[WaveFile.Length];
        data = new Double[(wave.Length - 44) / 4];//shifting the headers out of the PCM data;
        WaveFile.Read(wave,0,Convert.ToInt32(WaveFile.Length));//read the wave file into the wave variable
        /***********Converting and PCM accounting***************/
        for (int i = 0; i < data.Length - i * 4; i++)
        {
            data[i] = (BitConverter.ToInt32(wave, (1 + i) * 4)) / 65536.0;
            //65536.0.0=2^n,       n=bits per sample;
        }
        /**************assigning sample rate**********************/
        for (int i = 24; i < 28; i++)
        {
            sR[i-24]= wave[i];
        }
        SampleRate = BitConverter.ToInt32(sR,0);
        return data;
    }

all you need to do now is to send the sample rate and the returned result to your FFT algorithm. The code is not handled so do your own handling as needed. I has been tested for phone recordings, of busy, ringing and speech, it functions correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM