import pyaudio import numpy as np import matplotlib.pyplot as plt import time import sys from scipy.signal import hilbert from scipy.signal import medfilt from sklearn.cluster import KMeans CHUNK = 8001 FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 8000 x = range (CHUNK) y = np.zeros(CHUNK) binary = np.zeros(CHUNK) xx = range (CHUNK*2) yy = np.zeros(CHUNK*2) fig1 = plt.figure(1, figsize=(8, 19)) fig1.subplots_adjust(hspace=0.8, wspace=0.1) ax1 = fig1.add_subplot(9,1,1) ax1.set_xlim(CHUNK/2,CHUNK+CHUNK/2) lines1, = plt.plot(xx,yy, color="green") plt.title('input signal') ax2 = fig1.add_subplot(9,1,2) ax2.set_xlim(CHUNK/2,CHUNK+CHUNK/2) lines2, = plt.plot(xx,yy, color="orange") plt.title('envelope') ax3 = fig1.add_subplot(9,1,3) ax3.set_xlim(CHUNK/2,CHUNK+CHUNK/2) lines3, = plt.plot(xx,yy, color="firebrick") plt.title('median filtered') ax4 = fig1.add_subplot(9,1,4) lines4, = plt.plot(x,y, color="maroon") plt.title('binary signal') ax5 = fig1.add_subplot(9,1,5) lines5 = plt.hist(x, density=True, alpha= 0.75, color="white") plt.title('Dot/Dash histogram') ax6 = fig1.add_subplot(9,1,6) lines6 = plt.hist(x, density=True, alpha= 0.75, color="white") plt.title('Spaces histogram') up_2 = np.random.randn(200) down_2 = np.random.randn(200) ax8 = fig1.add_subplot(3,1,3) lines8 = plt.hist2d(up_2, down_2) plt.title('2D histogram') y_buf = np.zeros(CHUNK*2) time_base = 0 count = 0 trig_count = 0 t_up = 0 t_down = 0 binary_b4 = 0 up = [] down = [] p = pyaudio.PyAudio() def callback(in_data, frame_count, time_info, status): global y_buf, z_buf, window, yy global time_base, count, trig_count, t_up, t_down, binary_b4, up, down global ax6, ax7, lines6, lines7 y = np.frombuffer(in_data, dtype="int16") for i in range(CHUNK): y_buf[i ] = y_buf[i+CHUNK] y_buf[i+CHUNK] = y [i] analytic_signal = hilbert(y_buf) envelope = np.abs(analytic_signal) nmedian = 51 medsignal = medfilt(envelope, nmedian) y_max = max(1.1 * max(abs(y.min()), abs(y.max())), 100.0) y_buf_max = max(1.1 * max(abs(y_buf.min()), abs(y_buf.max())), 100.0) envelope_max = max(1.1 * abs(envelope.max()) , 100.0) medsignal_max = max(1.1 * abs(medsignal.max()), 100.0) th = 0.4*medsignal_max for i in range(CHUNK): if medsignal[i+CHUNK//2] > th: binary[i] = 1 else: binary[i] = 0 for i in range(CHUNK): if binary_b4 == 0 and binary[i] == 1: if trig_count == 0: trig_pos = i trig_count += 1 t_up = i+count*CHUNK t_down_duration = t_up - t_down if trig_count > 1: down.append(t_down_duration) if binary_b4 == 1 and binary[i] == 0 and trig_count > 0: t_down = i+count*CHUNK t_up_duration = t_down - t_up up.append(t_up_duration) binary_b4 = binary[i] print('count =', count, ', len(up)=', len(up), ', len(down) =', len(down)) if count%10 == 0: ax1.set_ylim(-y_buf_max, y_buf_max) ax2.set_ylim(-0.1*envelope_max, envelope_max) ax3.set_ylim(-0.1*medsignal_max, medsignal_max) ax4.set_ylim(-0.1, 1.1) ax5.set_xlim((0,2000)) ax6.set_xlim((0,2000)) lines1.set_data(xx,y_buf) lines2.set_data(xx,envelope) lines3.set_data(xx,medsignal) lines4.set_data(x,binary) ax5.hist(up, density=True, alpha=0.75, color="coral") ax6.hist(down, density=True, alpha=0.75, color="darkblue") ''' if len(up) > 200 and len(down) > 200: for i in range(200): up_2[i] = up[i] down_2[i] = down[i] ax8.hist2d(up_2, down_2) ''' if len(up) == len(down): ax8.hist2d(up,down) print('*') count += 1 return (None, pyaudio.paContinue) stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK, stream_callback=callback) stream.start_stream() while stream.is_active(): plt.pause (0.01) time.sleep(0.01) stream.stop_stream() stream.close() p.terminate()