如何从Accord.NET中的Signal对象创建ComplexSignal对象?

时间:2013-11-20 06:02:32

标签: c# signal-processing fft audio-processing accord.net

我正在尝试对由.wav文件生成的信号执行FFT,该文件具有1个通道和64064个样本(大约4秒,16k)。我正在使用Accord.NET和以下代码来尝试创建一个ComplexSignal对象,这是执行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. 

阅读Signal的源代码,只有在Signal.SampleFormat不是Format32bitIeeeFloat时才会抛出它。

我真的很惊讶在C#中操作wav文件的音频功能(特别是频率)并不容易。

1 个答案:

答案 0 :(得分:1)

你需要创建一个汉明(或其他方法)窗口,其大小为2(我在这里选择1024)。然后,在执行前向傅里叶变换之前,将窗口应用于复合信号。

        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