如何绘制使用FFT wav文件的谱? [英] How do I plot the spectrum of a wav file using FFT?

查看:273
本文介绍了如何绘制使用FFT wav文件的谱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:这不是一个重复的,我比相关的问题等具体要求

首先,我要绘制的音频文件的频谱(.WAV)就像什么大胆做(类似于:<一href=\"http://stackoverflow.com/questions/10664105/how-to-draw-a-frequency-spectrum-from-a-fourier-transform\">How绘制频谱从傅立叶变换)。

到目前为止,我能够读取和写入WAV文件。但我的问题是我不知道到底是什么值我需要传递给FFT功能。顺便说一句,我在C#中使用Exocortex进行FFT。 FFT功能要求我通过复数数组的大小合适(512,1024 ......我presume),长度为一个可选的整数参数,傅立叶方向(前/后)。

具体的问题:


  1. 情结(类)从Exocortex库有两个值,即实部和虚。我有样品阵列,所以这应该是真实的,这应该是虚构的?

  2. 我有wav文件,所以长度应该假设变量。我如何传递到FFT功能?我应该选择一个尺寸(512/1024 /等),划分了整个样本的大小,然后通过这一切的FFT?

  3. 我怎么知道什么频率应该在x轴上下上市?

  4. 如何绘制FFT'ed数据? (我想x轴是频率,并且以分贝为单位y轴)

如果你不明白我的意思,然后尝试使用Audacity的,导入音频文件,然后单击分析>频谱图。这些都是事要重新创建。请回答我的细节问题,因为我真的想学习这一点。我只是对这个有点背景。我只是在数字信号处理的新手。也尽可能请不要告诉我其他FFT网站,因为他们没有回答我的问题明确。


编辑:

我已经做了一些阅读和发现了如何FFT音频数据,但只能在2的幂那么,如何做同样在音频文件与长度是不是2的幂?据一些我需要使用的窗口。我也做了一些搜索一下,发现,只需要波形的部分稍后处理。还记得上面我想要得到的音频文件,而不是它的一部分的FFT。所以,我应该怎么办?请帮助:(


解决方案

签名是

 公共静态无效的FFT(浮动[]的数据,诠释长度,FourierDirection方向)


  1. 您传递复杂数字数组,再psented成对$ P $。既然你只有实数(样品),你应该把你的样品中的数组中的位置,甚至 - 数据[0],数据[2],数据[4]等。奇数位置应该是0,数据[1] =数据[3] = 0 ...

  2. 的长度是要计算在您的FFT样的量,应该是在数据阵列的长度正好一半。您可以FFT整个WAV或部分 - 取决于你希望看到什么。大胆将绘制文件的选定部分的功率谱,如果你想这样做,传递整个WAV或选定的部分。

  3. FFT将只显示你的频率到你的采样率的一半。所以,你应该有0和一半的采样率之间的值。值的大小取决于你有(样本量将影响计算的precision)样品的金额

  4. 无畏绘制功率谱。你应该把你收到阵列中的每个复数对,并计算出它的ABS。 ABS被定义为开方(R ^ 2 + I ^ 2)。每个ABS值将对应于单个频率。

下面是一个工作code的例子:

 浮动[]数据=新的浮动[8];
数据[0] = 1;数据[2] = 1;数据[4] = 1;数据[6] = 1;
Fourier.FFT(数据,data.Length / 2,FourierDirection.Forward);

我给它4个样本,都是一样的。因此,我希望得到的东西只在频率为0。事实上,它运行后,我得到

数据[0] == 1,数据[2] == 1,数据[4] == 1,数据[6] == 1

和其他都是0。

如果我想使用的复杂阵列过载

 复合[] = DATA2新的复杂[4];
数据2 [0] =新的复杂(1,0);
data2的[1] =新复合物(1,0);
data2的[2] =新复合物(1,0);
data2的[3] =新复合物(1,0);
Fourier.FFT(DATA2,data2.Length,FourierDirection.Forward);

请注意,这里的第二个参数等于所述阵列的长度,由于每个阵列构件是复数。我得到了相同的结果了。

我想我以前错过了复杂的过载。我似乎不太容易出错,更自然的使用,除非你的数据已经进来对。

NOTE: This is not a duplicate, I have specific requirements other than related questions.

To start with, I want to plot the spectrum of an audio file (.wav) just like what audacity does (similar: How to draw a frequency spectrum from a Fourier transform).

So far I am able to read and write wav files. But my problem is I don't know exactly what values I need to pass to the FFT function. By the way I am using Exocortex for FFT in C#. The FFT function requires me to pass an array of complex numbers with the right size (512, 1024, ... I presume), an optional integer parameter for length, and the fourier direction (forward/backward).

Specific Questions:

  1. The Complex (class) from the Exocortex library has two values namely Real and Imaginary. I have the array of samples, so which should be real and which should be Imaginary?
  2. I have the wav file, so the length should be assumed variable. How do I pass that to the FFT function? Should I select a size (512/1024/etc), divide the the entire samples to the size, then pass all of it to the FFT?
  3. How do i know what frequencies should be listed down on the x-axis?
  4. How do I plot the FFT'ed data? (I want the x-axis to be frequency, and y-axis in decibels)

If you don't get what I mean, then try to use Audacity, import an audio file, then click Analyze > Plot Spectrum. Those are the things want to recreate. Please answer my question in details because I really want to learn this. I only have a little background on this. I am just a newbie in digital signal processing. Also as much as possible please don't direct me to other FFT sites because they don't answer my question specifically.


EDIT:

I've done some reading and found out how to FFT an audio data but only in powers of 2. So how do I do the same in an audio file with a length that's not of powers of 2? According to some I need to use "window". I've also done some searching about it and found out that it only takes an portion of the waveform to be processed later. Remember above that I want to get the FFT of the audio file not a portion of it. So what should I do now? Please help :(

解决方案

The signature is

public static void  FFT( float[] data, int length, FourierDirection direction )

  1. You pass an array of complex numbers, represented as pairs. Since you only have real numbers (the samples), you should put your samples in the even locations in the array - data[0], data[2], data[4] and so on. Odd locations should be 0, data[1] = data[3] = 0...
  2. The length is the amount of samples you want to calculate your FFT on, it should be exactly half of the length of the data array. You can FFT your entire WAV or parts of it - depends on what you wish to see. Audacity will plot the power spectrum of the selected part of the file, if you wish to do the same, pass the entire WAV or the selected parts.
  3. FFT will only show you frequencies up to half of your sampling rate. So you should have values between 0 and half your sampling rate. The amount of values depends on the amount of samples you have (the amount of samples will affect the precision of the calculation)
  4. Audacity plots the power spectrum. You should take each complex number pair in the array you receive and calculate its ABS. ABS is defined as sqrt(r^2+i^2). Each ABS value will correspond to a single frequency.

Here's an example of a working code:

float[] data = new float[8];
data[0] = 1; data[2] = 1; data[4] = 1; data[6] = 1;
Fourier.FFT(data, data.Length/2, FourierDirection.Forward);

I'm giving it 4 samples, all the same. So I expect to get something only at frequency 0. And indeed, after running it, I get

data[0] == 1, data[2] == 1, data[4] == 1, data[6] == 1

And others are 0.

If I want to use the Complex array overload

Complex[] data2 = new Complex[4];
data2[0] = new Complex(1,0);
data2[1] = new Complex(1, 0);
data2[2] = new Complex(1, 0);
data2[3] = new Complex(1, 0);
Fourier.FFT(data2,data2.Length,FourierDirection.Forward);

Please note that here the second parameter equals the length of the array, since each array member is a complex number. I get the same result as before.

I think I missed the complex overload before. I seems less error prone and more natural to use, unless your data already comes in pairs.

这篇关于如何绘制使用FFT wav文件的谱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆