1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| import pyaudio import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D
nmicro=2
pos = np.array([0,-0.2])
print(pos)
CHUNK = 4800 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 48000 p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK)
X_STEP=100 x = np.linspace(-0.8, 0.8, X_STEP)
A_STEP=100 p = np.zeros(x.shape[0]) while True: data = stream.read(CHUNK,exception_on_overflow=False)
data = np.frombuffer(data, dtype=np.short)
data = data.reshape(CHUNK, 2)[:, :2].T
data_n = np.fft.fft(data)/data.shape[1]
data_n = data_n[:, :data.shape[1]//2]
data_n[:, 1:] *= 2
r = np.zeros((A_STEP, nmicro,nmicro), dtype=complex)
for fi in range(1, A_STEP+1): rr = np.dot(data_n[:, fi*10-10:fi*10+10], data_n[:, fi*10-10:fi*10+10].T.conjugate())/nmicro r[fi-1, ...] = np.linalg.inv(rr)
for i in range(x.shape[0]): dm = np.sqrt(x[i]**2 + 1) delta_dn = pos*x[i]/dm for fi in range(1, A_STEP+1): a = np.exp(-1j*2*np.pi*fi*100*delta_dn/340) p[i] = p[i] + 1 / np.abs(np.dot(np.dot(a.conjugate(), r[fi-1]), a))
p /= np.max(p) p_max = np.argsort(p)[-5:] print(np.average(p_max)) plt.clf() plt.plot(x, np.abs(p)) plt.xlabel('x') plt.ylabel('p') plt.pause(0.01)
|