def _draw_axes(kind, pass_=1): global _plt viewport = _plt.kwargs['viewport'] vp = _plt.kwargs['vp'] x_tick, x_org, x_major_count = _plt.kwargs['xaxis'] y_tick, y_org, y_major_count = _plt.kwargs['yaxis'] gr.setlinecolorind(1) gr.setlinewidth(1) diag = ((viewport[1]-viewport[0])**2 + (viewport[3]-viewport[2])**2)**0.5 charheight = max(0.018 * diag, 0.012) gr.setcharheight(charheight) ticksize = 0.0075 * diag if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): z_tick, z_org, z_major_count = _plt.kwargs['zaxis'] if pass_ == 1: gr.grid3d(x_tick, 0, z_tick, x_org[0], y_org[1], z_org[0], 2, 0, 2) gr.grid3d(0, y_tick, 0, x_org[0], y_org[1], z_org[0], 0, 2, 0) else: gr.axes3d(x_tick, 0, z_tick, x_org[0], y_org[0], z_org[0], x_major_count, 0, z_major_count, -ticksize) gr.axes3d(0, y_tick, 0, x_org[1], y_org[0], z_org[0], 0, y_major_count, 0, ticksize) else: if kind == 'heatmap': ticksize = -ticksize else: gr.grid(x_tick, y_tick, 0, 0, x_major_count, y_major_count) gr.axes(x_tick, y_tick, x_org[0], y_org[0], x_major_count, y_major_count, ticksize) gr.axes(x_tick, y_tick, x_org[1], y_org[1], -x_major_count, -y_major_count, -ticksize) if 'title' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.textext(0.5*(viewport[0] + viewport[1]), vp[3], _plt.kwargs['title']) gr.restorestate() if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): x_label = _plt.kwargs.get('xlabel', '') y_label = _plt.kwargs.get('ylabel', '') z_label = _plt.kwargs.get('zlabel', '') gr.titles3d(x_label, y_label, z_label) else: if 'xlabel' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_BOTTOM) gr.textext(0.5 * (viewport[0] + viewport[1]), vp[2] + 0.5 * charheight, _plt.kwargs['xlabel']) gr.restorestate() if 'ylabel' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.setcharup(-1, 0) gr.textext(vp[0] + 0.5 * charheight, 0.5 * (viewport[2] + viewport[3]), _plt.kwargs['ylabel']) gr.restorestate()
def _draw_axes(kind, pass_=1): global _plt viewport = _plt.kwargs['viewport'] vp = _plt.kwargs['vp'] x_tick, x_org, x_major_count = _plt.kwargs['xaxis'] y_tick, y_org, y_major_count = _plt.kwargs['yaxis'] gr.setlinecolorind(1) gr.setlinewidth(1) diag = ((viewport[1]-viewport[0])**2 + (viewport[3]-viewport[2])**2)**0.5 charheight = max(0.018 * diag, 0.012) gr.setcharheight(charheight) ticksize = 0.0075 * diag if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): z_tick, z_org, z_major_count = _plt.kwargs['zaxis'] if pass_ == 1: gr.grid3d(x_tick, 0, z_tick, x_org[0], y_org[0], z_org[0], 2, 0, 2) gr.grid3d(0, y_tick, 0, x_org[1], y_org[0], z_org[0], 0, 2, 0) else: gr.axes3d(x_tick, 0, z_tick, x_org[0], y_org[0], z_org[0], x_major_count, 0, z_major_count, -ticksize) gr.axes3d(0, y_tick, 0, x_org[1], y_org[0], z_org[0], 0, y_major_count, 0, ticksize) else: if kind == 'heatmap': ticksize = -ticksize else: gr.grid(x_tick, y_tick, 0, 0, x_major_count, y_major_count) gr.axes(x_tick, y_tick, x_org[0], y_org[0], x_major_count, y_major_count, ticksize) gr.axes(x_tick, y_tick, x_org[1], y_org[1], -x_major_count, -y_major_count, -ticksize) if 'title' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.textext(0.5*(viewport[0] + viewport[1]), vp[3], _plt.kwargs['title']) gr.restorestate() if kind in ('wireframe', 'surface', 'plot3', 'scatter3', 'trisurf'): x_label = _plt.kwargs.get('xlabel', '') y_label = _plt.kwargs.get('ylabel', '') z_label = _plt.kwargs.get('zlabel', '') gr.titles3d(x_label, y_label, z_label) else: if 'xlabel' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_BOTTOM) gr.textext(0.5 * (viewport[0] + viewport[1]), vp[2] + 0.5 * charheight, _plt.kwargs['xlabel']) gr.restorestate() if 'ylabel' in _plt.kwargs: gr.savestate() gr.settextalign(gr.TEXT_HALIGN_CENTER, gr.TEXT_VALIGN_TOP) gr.setcharup(-1, 0) gr.textext(vp[0] + 0.5 * charheight, 0.5 * (viewport[2] + viewport[3]), _plt.kwargs['ylabel']) gr.restorestate()
gr.setwindow(50, 25000, 0, 100) gr.setscale(1) start = time.time() while time.time() - start < 10: try: power = get_spectrum() peakind = signal.find_peaks_cwt(power, numpy.array([5])) except (IOError): continue gr.clearws() gr.setlinewidth(1) gr.setlinecolorind(1) gr.grid(1, 5, 50, 0, 1, 2) gr.axes(1, 5, 50, 0, 1, 2, -0.008) gr.setcharheight(0.020) gr.text(0.15, 0.965, '100Hz') gr.text(0.47, 0.965, '1kHz') gr.text(0.79, 0.965, '10kHz') gr.setlinecolorind(4) gr.polyline(f[1:], power[1:]) for p in peakind: if power[p] > 10: gr.setlinewidth(2) gr.setlinecolorind(2) xe, ye = parabolic(f[p], power, p) print(xe, ye) gr.polyline([xe] * 2, [0, ye]) gr.updatews()
import gr SAMPLES = 2048 wf = wave.open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Monty_Python.wav'), 'rb') pa = pyaudio.PyAudio() stream = pa.open(format=pa.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) gr.setwindow(0, SAMPLES, -30000, 30000) gr.setviewport(0.05, 0.95, 0.05, 0.95) gr.setlinecolorind(218) gr.setfillintstyle(1) gr.setfillcolorind(208) data = wf.readframes(SAMPLES) while data != '' and len(data) == SAMPLES * wf.getsampwidth(): stream.write(data) amplitudes = numpy.fromstring(data, dtype=numpy.short) power = abs(numpy.fft.fft(amplitudes / 512.0))[:SAMPLES/2:2] - 30000 gr.clearws() gr.fillrect(0, SAMPLES, -30000, 30000) gr.grid(40, 1200, 0, 0, 5, 5) gr.polyline(range(SAMPLES)[0::4], amplitudes[0::4]) gr.polyline(range(SAMPLES)[0::4], power) gr.updatews() data = wf.readframes(SAMPLES)
wf = wave.open( os.path.join(os.path.dirname(os.path.realpath(__file__)), 'Monty_Python.wav'), 'rb') pa = pyaudio.PyAudio() stream = pa.open(format=pa.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True) gr.setwindow(0, SAMPLES, -30000, 30000) gr.setviewport(0.05, 0.95, 0.05, 0.95) gr.setlinecolorind(218) gr.setfillintstyle(1) gr.setfillcolorind(208) data = wf.readframes(SAMPLES) while data != '' and len(data) == SAMPLES * wf.getsampwidth(): stream.write(data) amplitudes = numpy.fromstring(data, dtype=numpy.short) power = abs(scipy.fftpack.fft(amplitudes / 512.0))[:SAMPLES / 2:2] - 30000 gr.clearws() gr.fillrect(0, SAMPLES, -30000, 30000) gr.grid(40, 1200, 0, 0, 5, 5) gr.polyline(range(SAMPLES)[0::4], amplitudes[0::4]) gr.polyline(range(SAMPLES)[0::4], power) gr.updatews() data = wf.readframes(SAMPLES)
gr.setwindow(50, 25000, 0, 100) gr.setscale(1) start = time.time() while time.time() - start < 10: try: power = get_spectrum() peakind = signal.find_peaks_cwt(power, np.array([5])) except (IOError): continue gr.clearws() gr.setlinewidth(1) gr.setlinecolorind(1) gr.grid(1, 5, 50, 0, 1, 2) gr.axes(1, 5, 50, 0, 1, 2, -0.008) gr.setcharheight(0.020) gr.text(0.15, 0.965, '100Hz') gr.text(0.47, 0.965, '1kHz') gr.text(0.79, 0.965, '10kHz') gr.setlinecolorind(4) gr.polyline(f[1:], power[1:]) for p in peakind: if power[p] > 10: gr.setlinewidth(2) gr.setlinecolorind(2) xe, ye = parabolic(f[p], power, p) print(xe, ye) gr.polyline([xe] * 2, [0, ye]) gr.updatews()
from __future__ import print_function from __future__ import unicode_literals import sys import os import numpy as np import gr sys.path.insert(0, os.path.abspath('../src')) from statistics.rdf import Kernels x = np.linspace(-1.25, 1.25, 500) for name in dir(Kernels): if name.startswith('_') or name == 'bandwidth': continue kernel = getattr(Kernels, name) y = kernel(x) gr.beginprint(name.lower()+'.svg') gr.clearws() gr.setwsviewport(0, 0.1, 0, 0.06) gr.setviewport(0, 1, 0, 1) gr.setwindow(-1.25, 1.25, -0.25, 1.25) gr.grid(0.1, 0.1, 0, 0, 5, 5) gr.axes(0.1, 0.1, 0, 0, 5, 5, -0.01) gr.setlinewidth(2) gr.setlinecolorind(2) gr.polyline(x, y) gr.setlinecolorind(1) gr.setlinewidth(1) gr.updatews() gr.endprint()