我试图想象一个正弦发生器,但得到错误
ValueError:x和y必须具有相同的第一维
我认为尺寸是相同的,但显然它们不是。 鼻窦发生器是标准的
import pyaudio
import numpy as np
p = pyaudio.PyAudio()
volume = 0.08 # range [0.0, 1.0]
fs = 192000 # sampling rate, Hz, must be integer
duration = 3.0 # in seconds, may be float
f = 257.0 # sine frequency, Hz, may be float
f2 = 258.0
f3 = 300.0
f4 = 301.0
oma = 2000.0
a = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)
b = (np.sin(2*np.pi*np.arange(fs*duration)*f2/fs)).astype(np.float32)
c = (np.sin(2*np.pi*np.arange(fs*duration)*f3/fs)).astype(np.float32)
d = (np.sin(2*np.pi*np.arange(fs*duration)*f4/fs)).astype(np.float32)
om = (np.sin(2*np.pi*np.arange(fs*duration)*oma/fs)).astype(np.float32)
# generate samples, note conversion to float32 array
samples = om*(a+b+c+d)
# for paFloat32 sample values must be in range [-1.0, 1.0]
stream = p.open(format=pyaudio.paFloat32,
channels=2,
rate=fs,
output=True)
# play. May repeat with different volume values (if done interactively)
stream.write(volume*samples)
stream.stop_stream()
stream.close()
p.terminate()
我添加了matplotlib但无法开始工作。我试图在0 - 2 * np.pi的区间内进行可视化,包括单个波和聚合函数。
在线有一个解决方案,但它仅限于两个频率。 Wave interference beat frequency
plt的最新版本是
import matplotlib.pyplot as plt
x = np.mgrid[0:2*np.pi]
plt.rc('lines', linewidth=4)
plt.plot(samples,x)
plt.show()