基本上,我希望我的频谱呈条形,其频率从0-200、200-400、400-800、800-2000、2000-4000等变化 我会实时收集fft数据并以标准方式实时将其绘制出来,但我希望它能成为条形图,因此我们将为您提供任何帮助!
这里是当前外观的gif图像 :https://i.imgur.com/eOGzPFd.gifv
它并没有真正显示出低音的变化,因此我想将频率分组为一些代表较高频率范围的条形图(例如,一个条形代表2000hz-4000hz),而某些代表较低频率范围(例如,在一格中为0hz-200hz)
import pyaudio
import numpy as np
import time
import matplotlib.animation as animation
import matplotlib.pyplot as plt
from matplotlib import style
pa = pyaudio.PyAudio()
audio_data = 0
def callback(in_data, frame_count, time_info, flag):
global audio_data
audio_data = np.fromstring(in_data, dtype=np.float32)
return None,pyaudio.paContinue
stream = pa.open(format=pyaudio.paFloat32,
channels=1,
rate=44100,
output=False,
input=True,
stream_callback=callback)
stream.start_stream()
plt.ion() # Stop matplotlib windows from blocking
# Setup figure, axis and initiate plot
fig, ax = plt.subplots()
ln, = ax.plot([], [], 'b,:')
x = 0
while stream.is_active():
fft_data = np.fft.fft(audio_data)
fft_freq = np.fft.fftfreq(len(fft_data),d=1/44100)
ln.set_xdata(np.abs(fft_freq))
ln.set_ydata(np.abs(fft_data))
fig.canvas.draw()
fig.canvas.flush_events()
if x == 0:
ax.relim()
ax.autoscale_view()
ax.set_xlim([-500,10000])
ax.set_ylim([-1,10])
x=1
stream.close()
pa.terminate()