我正忙着处理信号处理,我只是将440 Hz的测试频率读入计算机的麦克风,然后尝试用numpy的fft从原始音频数据中恢复它。看起来我的关系是2倍 - 我持续得到220赫兹而不是440赫兹。
代码
RATE = 44100
CHUNK = 1024
N = 2048
RECORD_SECONDS = 1
p = pyaudio.PyAudio()
player = p.open(format=pyaudio.paInt16, channels=2, rate=RATE, output=True, output_device_index=1, frames_per_buffer=CHUNK)
stream = p.open(format=pyaudio.paInt16, channels=2, rate=RATE, input=True, input_device_index=0, frames_per_buffer=CHUNK)
frames = []
max_frames = []
for i in range(int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
data = np.fromstring(data,dtype=np.int16)
spec_y = np.fft.fft(data[:])
spec_y = [np.sqrt(j.real**2 + j.imag**2) for j in spec_y]
spec_x = np.fft.fftfreq(N, d = 1.0 / RATE)
frames.append((spec_x, spec_y))
spec_index = np.where(spec_y==np.max(spec_y))[0]
max_frames.append(np.abs(spec_x[spec_index]))
player.write(data, CHUNK)
# frames = ''.join(frames)
stream.stop_stream()
stream.close()
p.terminate()
#Spectrum
plt.clf()
plt.subplot(312)
plt.plot(frames[-1][0], frames[-1][1], marker= 'o', linestyle='-')
plt.axis([-RATE/2, RATE / 2, 0, np.max(frames[-1][1])])
plt.xlabel("frequency [Hz]")
plt.ylabel("amplitude spectrum")
plt.show()