如何在 C++ 中使用 fft 生成音频频谱?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4675457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:07:39  来源:igfitidea点击:

How to generate the audio spectrum using fft in C++?

c++audiofftspectrum

提问by MRashid

I want to generate an audio spectrum (as seen in this video) of a mp3 audio file. Basically this problem requires calculating the fft of the audio signal. How do I program this in C/C++?

我想生成一个 mp3 音频文件的音频频谱(如本视频所示)。基本上这个问题需要计算音频信号的fft。我如何在 C/C++ 中编程?

I've looked at a couple of open source libraries such as FFTWand I really don't know how to use these for my problem. Any help would be greatly appreciated. Thanks in advance!

我查看了几个开源库,例如FFTW,但我真的不知道如何将它们用于解决我的问题。任何帮助将不胜感激。提前致谢!

回答by Paul R

There are quite a few similar/related questions on SO already which are well worth reading as the answers contain a lot of useful information and advice, but in essence you need to do this:

已经有很多关于 SO 的类似/相关问题非常值得一读,因为答案包含很多有用的信息和建议,但本质上你需要这样做:

  • convert audio data to format required by FFT (e.g. int -> float, separate L/R channels)
  • apply suitable window function(e.g. Hann aka Hanning window)
  • apply FFT (NB: if using typical complex-to-complex FFT then set imaginary parts of input array to zero)
  • calculate magnitude of first N/2 FFT output bins (sqrt(re*re + im*im))
  • optionally convert magnitude to dB (log) scale (20 * log10(magnitude))
  • plot N/2 (log) magnitude values
  • 将音频数据转换为 FFT 所需的格式(例如 int -> float,单独的 L/R 通道)
  • 应用合适的窗口函数(例如Hann aka Hanning window
  • 应用 FFT(注意:如果使用典型的复数到复数 FFT,则将输入数组的虚部设置为零)
  • 计算前 N/2 个 FFT 输出箱的幅度 ( sqrt(re*re + im*im))
  • 可选择将幅度转换为 dB(对数)标度 ( 20 * log10(magnitude))
  • 绘制 N/2(对数)幅度值

Note that while FFTW is a very good and very fast FFT it may be a little overwhelming for a beginner - it's also very expensive if you want to include it as part of a commercial product - I recommend starting with KissFFTinstead.

请注意,虽然 FFTW 是一种非常好且速度非常快的 FFT,但它对于初学者来说可能有点不知所措 - 如果您想将其作为商业产品的一部分,它也非常昂贵 - 我建议从KissFFT开始。