How to extract frequency information from an input audio stream (using PortAudio)?

北战南征 提交于 2019-12-20 10:58:12

问题


I want to record sound (voice) using PortAudio (PyAudio) and output the corresponding sound wave on the screen. Hopeless as I am, I am unable to extract the frequency information from the audio stream so that I can draw it in Hz/time form.


Here's an example code snippet that records and plays recorded audio for five seconds, in case it helps any:

p = pyaudio.PyAudio()

chunk = 1024
seconds = 5

stream = p.open(format=pyaudio.paInt16,
                channels=1,
                rate=44100,
                input=True,
                output=True)

for i in range(0, 44100 / chunk * seconds):
    data = stream.read(chunk)
    stream.write(data, chunk)

I wish to extract the needed information from the above variable "data". (Or use some other high-level approach with PortAudio or another library with Python bindings.)


I'd be very grateful for any help! Even vaguely related tidbits of audio-analyzing wisdom are appreciated. :)


回答1:


What you want is probably the Fourier transform of the audio data. There is several packages that can calculate that for you. scipy and numpy is two of them. It is often named "Fast Fourier Transform" (FFT), but that is just the name of the algorithm.

Here is an example of it's usage: https://svn.enthought.com/enthought/browser/Chaco/trunk/examples/advanced/spectrum.py




回答2:


The Fourier Transform will not help you a lot if you want the analysis to be conducted in both the frequency and time domain. You might want to have a look at "Wavelet Transforms". There is a package called pywavelets... http://www.pybytes.com/pywavelets/#discrete-wavelet-transform-dwt



来源:https://stackoverflow.com/questions/259451/how-to-extract-frequency-information-from-an-input-audio-stream-using-portaudio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!