简体   繁体   English

PyAudio,如何在录制时分辨频率和幅度?

[英]PyAudio, how to tell frequency and amplitude while recording?

I've used the PyAudio default recording example, and added numpy and scipy. 我已经使用了PyAudio默认录制示例,并添加了numpy和scipy。 I can only use scipy.io.wavefile.read('FILE.wav') , after recording the file, however, and it also gives me this random tuple, eg: (44100, array([[ 0, 0], [-2, 0], [ 0, -2], ..., [-2, -2], [ 1, 3], [ 2, -1]], dtype=int16)) . 但是,我只能在录制文件后使用scipy.io.wavefile.read('FILE.wav') ,它也会给我这个随机元组,例如: (44100, array([[ 0, 0], [-2, 0], [ 0, -2], ..., [-2, -2], [ 1, 3], [ 2, -1]], dtype=int16)) What does this array give me and do you know how else you can get the frequency/amplitude of each frame of a wav file, preferably while recording? 这个数组给了我什么,你知道如何获得wav文件每帧的频率/幅度,最好是在录制时吗?

The array is not random data, it's the wave data of your stereo sound, and 44100 is the sampling rate. 数组不是随机数据,而是立体声的波数据,44100是采样率。 use the following code to plot the wave of left channel: 使用以下代码绘制左声道的波形:

import scipy.io.wavfile as wavfile
import numpy as np
import pylab as pl
rate, data = wavfile.read('FILE.wav')
t = np.arange(len(data[:,0]))*1.0/rate
pl.plot(t, data[:,0])
pl.show()

To get the frequency and amplitude of you wave, do FFT. 要获得波形的频率和幅度,请执行FFT。 Following code plot the power of every frequency bin: 下面的代码绘制每个频率仓的功率:

p = 20*np.log10(np.abs(np.fft.rfft(data[:2048, 0])))
f = np.linspace(0, rate/2.0, len(p))
pl.plot(f, p)
pl.xlabel("Frequency(Hz)")
pl.ylabel("Power(dB)")
pl.show()

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

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