FFT audio input

后端 未结 2 491
刺人心
刺人心 2020-12-12 03:21

I want to apply FFT on a signal recorded by AudioRecorder and saved to a wav file. the FFT i am using has a Complex[] input parameter. I am confused, is there a difference b

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 03:46

    If you are working with audio with a bit depth of 16 bits (each sample has 16 bits), then each byte will only have half of a sample.What you need to do is cast your bytes to 16 bit samples then divide the resulting number by 32768 (This is the magnitude of the smallest number a 2's complement 16 bit number can store i.e 2^15) to get the actual audio sample which is a number between -1 and 1.You will then convert this number to a complex number by setting it's imaginary component to 0.

    A small C# sample can be seen below (indicative code):

        byte[] myAudioBytes = readAudio();
        int numBytes = myAudioBytes.Length;
    
        var myAudioSamples = new List();
    
        for( int i = 0; i < numBytes; i = i + 2)
        {
          //Cast to 16 bit audio and then add sample
           short sample = (short) ((myAudioBytes[i] << 8 | myAudioBytes[i + 1]) / 32768 ); 
           myAudioSamples.Add(sample);
        }
    
        //Change real audio to Complex audio
    
        var complexAudio = new Complex[myAudioSamples.Length];
    
        int i = 0;
        foreach(short sample in myAudioSamples)
           complexAudio[i++] = new Complex(){ Real = sample, Imaginary = 0 };
    
       //Now you can proceed to getting the FFT of your Audio here
    

    Hope the code has guided you on how you should handle your audio.

提交回复
热议问题