简体   繁体   English

Python:声音文件的频率分析

[英]Python: Frequency Analysis of Sound Files

I am generating some sound files that play tones at various frequencies with a certain number of harmonics. 我正在生成一些声音文件,这些声音文件以不同的频率播放音调,并具有一定数量的谐波。
Ultimately, these sounds will be played on a device with a small speaker. 最终,这些声音将在带有小扬声器的设备上播放。

I have the frequency response curve of the speaker and want to do the following in Python: 我有扬声器的频率响应曲线,并希望在Python中执行以下操作:

  1. Plot the frequency spectrum of sound file. 绘制声音文件的频谱。 I need a take the FFT of the file and plot it with gnuplot 我需要一个文件的FFT并用gnuplot绘制它
  2. Apply a nonlinear transfer function based on the frequency response curve in the data sheet. 根据数据表中的频率响应曲线应用非线性传递函数。
  3. Plot the result after the function is applied. 应用函数后绘制结果。

Does anyone know : 有人知道吗 :

  • What the simplest way to do this would be? 最简单的方法是什么?
  • or of an Application (GNU/Linux based) that could do this for me? 或者是一个可以为我做这个的应用程序 (基于GNU / Linux)?

I know you didn't mention Pylab/Matplotlib, but it works. 我知道你没有提到Pylab / Matplotlib,但它确实有效。 Here is an example (assumes single-channel signal): 这是一个例子(假设是单通道信号):

x, fs, nbits = audiolab.wavread('schubert.wav')
audiolab.play(x, fs)
N = 4*fs    # four seconds of audio
X = scipy.fft(x[:N])
Xdb = 20*scipy.log10(scipy.absolute(X))
f = scipy.linspace(0, fs, N, endpoint=False)
pylab.plot(f, Xdb)
pylab.xlim(0, 5000)   # view up to 5 kHz

Y = X*H
y = scipy.real(scipy.ifft(Y))

you can use numpy and matPlotLib. 你可以使用numpy和matPlotLib。 Something like the code below: 类似下面的代码:

spectrum = numpy.fft.fft(signal)
frequencies = numpy.fft.fftfreq(len(spectrum))
pylab.plot(frequencies,spectrum)
pylab.show()

That will show a graph of the fft spectrum. 这将显示fft光谱的图表。

scipy has an FFT and hooks nicely into gnuplot. scipy有一个FFT并很好地挂钩到gnuplot。 You should be able to use the signal module to do the math. 您应该能够使用信号模块进行数学运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM