2014-11-20 96 views
0

我想看看在一段音频中,某些频率,特别是20 - 60Hz的低音。我有音频作为一个字节数组,我将它转换为短阵列,然后通过(short [i] /(double)short.MaxValue,0)转换为一个复数。然后我把它传给Aforge的FFT。FFT哪些频率在哪些分箱?

音频是单声道的,采样率为44100.据我所知,我只能在^ 2处通过FFT加载卡盘。例如4096。我不明白输出箱中的频率是多少。

如果我从44100采样率的音频采样4096个采样。这是否意味着我正在花费毫秒级的音频?或只获得一些将出现的频率?

我将FFT的输出添加到一个数组中,我的理解是当我取4096时,bin 0将包含0 * 44100/4096 = 0hz,bin 1将保存1 * 44100/4096 = 10.7666015625hz和等等。它是否正确?或者我在这里做一些根本性错误?

我的目标是平均说20-60赫兹之间的频率,所以对于低低重低音的歌曲,这个数字要比低音很少的软钢琴片要高。

这是我的代码。

OpenFileDialog file = new OpenFileDialog(); 
file.ShowDialog(); 
WaveFileReader reader = new WaveFileReader(file.FileName); 

byte[] data = new byte[reader.Length]; 
reader.Read(data, 0, data.Length); 

samepleRate = reader.WaveFormat.SampleRate; 
bitDepth = reader.WaveFormat.BitsPerSample; 
channels = reader.WaveFormat.Channels; 

Console.WriteLine("audio has " + channels + " channels, a sample rate of " + samepleRate + " and bitdepth of " + bitDepth + "."); 


short[] shorts = data.Select(b => (short)b).ToArray(); 

int size = 4096; 
int window = 44100 * 10; 
int y = 0; 
Complex[] complexData = new Complex[size]; 
for (int i = window; i < window + size; i++) 
{ 
    Complex tmp = new Complex(shorts[i]/(double)short.MaxValue, 0); 

    complexData[y] = tmp; 
    y++; 

} 




FourierTransform.FFT(complexData, FourierTransform.Direction.Forward); 


double[] arr = new double[complexData.Length]; 
//print out sample of conversion 
for (int i = 0; i < complexData.Length; i++) 
{ 
    arr[i] = complexData[i].Magnitude; 

} 

Console.Write("complete, "); 


return arr; 

编辑:改到FFT来回DFT

+1

那么你似乎在做一个DFT(它比FFT更精确),但是返回的数据是如何构造的,我不知道。应该在您正在使用的库的文档中。从根本上说,如果数据的结构是线性的,那么它就是对的,但它也可以用对数结构。 – MrPaulch 2014-11-20 18:30:47

+0

感谢您指出了这一点,我的意思是运行fft,当我正在玩DFT时只是复制了代码。 – 2014-11-20 18:47:14

+0

你基本上是在正确的轨道上 - 你的箱子在你计算的时候宽度大约为10赫兹 - 见[这个答案](http://stackoverflow.com/questions/4364823/how-to-get-frequency-from-fft -result/4371627#4371627)以获得更全面的解释。 – 2014-11-20 20:07:25

回答

1

这是你的代码的修改版本。注意以“***”开头的注释。

OpenFileDialog file = new OpenFileDialog(); 
file.ShowDialog(); 
WaveFileReader reader = new WaveFileReader(file.FileName); 

byte[] data = new byte[reader.Length]; 
reader.Read(data, 0, data.Length); 

samepleRate = reader.WaveFormat.SampleRate; 
bitDepth = reader.WaveFormat.BitsPerSample; 
channels = reader.WaveFormat.Channels; 

Console.WriteLine("audio has " + channels + " channels, a sample rate of " + samepleRate + " and bitdepth of " + bitDepth + "."); 

// *** NAudio "thinks" in floats 
float[] floats = new float[data.Length/sizeof(float)] 
Buffer.BlockCopy(data, 0, floats, 0, data.Length); 

int size = 4096; 
// *** You don't have to fill the FFT buffer to get valid results. More noisy & smaller "magnitudes", but better freq. res. 
int inputSamples = samepleRate/100; // 10ms... adjust as needed 
int offset = samepleRate * 10 * channels; 
int y = 0; 
Complex[] complexData = new Complex[size]; 
// *** get a "scaling" curve to make both ends of sample region 0 but still allow full amplitude in the middle of the region. 
float[] window = CalcWindowFunction(inputSamples); 
for (int i = 0; i < inputSamples; i++) 
{ 
    // *** "floats" is stored as LRLRLR interleaved data for stereo audio 
    complexData[y] = new Complex(floats[i * channels + offset] * window[i], 0); 
    y++; 
} 
// make sure the back portion of the buffer is set to all 0's 
while (y < size) 
{ 
    complexData[y] = new Complex(0, 0); 
    y++; 
} 


// *** Consider using a DCT here instead... It returns less "noisy" results 
FourierTransform.FFT(complexData, FourierTransform.Direction.Forward); 


double[] arr = new double[complexData.Length]; 
//print out sample of conversion 
for (int i = 0; i < complexData.Length; i++) 
{ 
    // *** I assume we don't care about phase??? 
    arr[i] = complexData[i].Magnitude; 
} 

Console.Write("complete, "); 


return arr; 

一旦获得满意的结果,并假设一个44100赫兹的采样率和大小= 4096,元件2 - 4应该是你正在寻找的值。有一种方法可以将它们转换为dB,但我不记得它。

祝你好运!

+0

非常感谢。我不能让你明白我的意思。这一直困扰着我一段时间。 – 2014-11-21 23:34:18

+0

你用什么为你的calcWindowsFunction(),有没有一个包可用,或者我将不得不考虑实现这个自己。谢谢 – 2014-11-21 23:54:44

+0

你可以返回一个float数组(带有“size”元素)和全部1。有更好的窗口,但你需要从http://en.wikipedia.org/wiki/Window_function找出正确的窗口。一般来说,只要实施Hamming或Blackman-Harris窗户,你就应该有很好的形状。这里有很多示例代码。 – ioctlLR 2014-11-23 02:55:56