Question

I am trying to perform an FFT on a Signal produced by a .wav file that has 1 channel and 64064 samples (approximately 4 seconds long at 16k). I am using Accord.NET and the following code to attempt to create a ComplexSignal object, which is required to perform an FFT.

string fileName = "mu1.wav"; //the name of my wave file
WaveDecoder sourceDecoder = new WaveDecoder(fileName); //Accord.Audio.Formats.WaveDecoder
Signal s = sourceDecoder.Decode(); //SampleFormat says Format32bitIeeeFloat
ComplexSignal = s.ToComplex(); //This throws the following exception:

//InvalidSignalPropertiesException 
//Signals length should be a power of 2. 

Reading the source code of Signal, this should only be thrown if the Signal.SampleFormat isn't Format32bitIeeeFloat, which it is.

I'm really surprised it isn't easier to manipulate the audio features (specifically the frequencies) of a wav file in C#.

Was it helpful?

Solution

You need to create a Hamming (or other method) window with a size in a power of 2 (I chose 1024 here). Then, apply the window to the Complex Signal before performing the Forward Fourier Transform.

        string fileName = "mu1.wav";
        WaveDecoder sourceDecoder = new WaveDecoder(fileName);
        Signal sourceSignal = sourceDecoder.Decode();

        //Create Hamming window so that signal will fit into power of 2:           
        RaisedCosineWindow window = RaisedCosineWindow.Hamming(1024);

        // Splits the source signal by walking each 512 samples, then creating 
        // a 1024 sample window. Note that this will result in overlapped windows.
        Signal[] windows = sourceSignal.Split(window, 512);

        // You might need to import Accord.Math in order to call this:
        ComplexSignal[] complex = windows.Apply(ComplexSignal.FromSignal);

        // Forward to the Fourier domain
        complex.ForwardFourierTransform(); //Complete!

        //Recommend building a histogram to see the results
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top