Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def pendulum(t, theta, omega, acceleration):
gr.clearws()
gr.setviewport(0, 1, 0, 1)
x = [0.5, 0.5 + sin(theta) * 0.4]
y = [0.8, 0.8 - cos(theta) * 0.4]
gr.fillarea(4, # draw pivot point
[0.46, 0.54, 0.54, 0.46], [0.79, 0.79, 0.81, 0.81])
gr.setlinecolorind(1)
gr.setlinewidth(2)
gr.polyline(2, x, y) # draw rod
gr.setmarkersize(5)
gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE)
gr.setmarkercolorind(86)
gr.polymarker(1, [x[1]], [y[1]]) # draw bob
gr.setlinecolorind(4)
V = 0.05 * omega # show angular velocity
gr.drawarrow(x[1], y[1], x[1] + V*cos(theta), y[1] + V*sin(theta))
gr.setlinecolorind(2)
A = 0.05 * acceleration # show angular acceleration
gr.drawarrow(x[1], y[1], x[1] + A*sin(theta), y[1] + A*cos(theta))
gr.settextfontprec(2, gr.TEXT_PRECISION_STRING)
gr.setcharheight(0.032)
gr.settextcolorind(1)
gr.textext(0.05, 0.95, 'Damped Pendulum')
gr.setcharheight(0.040)
gr.mathtex(0.4, 0.22, '\\omega=\\dot{\\theta}')
gr.mathtex(0.4, 0.1, '\\dot{\\omega}=-\\gamma\\omega-\\frac{g}{l}sin(\\theta)')
gr.setcharheight(0.028)
gr.textext(0.05, 0.22, 't:%7.2f' % t)
gr.textext(0.05, 0.16, '\\theta:%7.2f' % (theta / pi * 180))
gr.setviewport(0.1, 0.95, 0.1, 0.95)
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()
def drawGR(self):
# preserve old values
ltype = gr.inqlinetype()
mtype = gr.inqmarkertype()
lcolor = gr.inqlinecolorind()
mcolor = gr.inqmarkercolorind()
lwidth = gr.inqlinewidth()
if self.linetype is not None:
gr.setlinecolorind(self.linecolor)
gr.setmarkercolorind(self.markercolor)
gr.setlinetype(self.linetype)
gr.setlinewidth(self.linewidth)
if self.markertype is not None:
gr.setmarkertype(self.markertype)
else:
gr.setmarkertype(gr.MARKERTYPE_DOT)
self._grerror(self._x, self._y, self._eneg, self._epos)
# restore old values
gr.setlinecolorind(lcolor)
gr.setmarkercolorind(mcolor)
gr.setlinetype(ltype)
gr.setmarkertype(mtype)
gr.setlinewidth(lwidth)
import os, wave, pyaudio
import numpy
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(SAMPLES/4, range(SAMPLES)[0::4], amplitudes[0::4])
gr.polyline(SAMPLES/8, range(SAMPLES)[0::8], power)
gr.updatews()
def pendulum(t, theta, omega, acceleration):
gr.clearws()
gr.setviewport(0, 1, 0, 1)
x = [0.5, 0.5 + np.sin(theta) * 0.4]
y = [0.8, 0.8 - np.cos(theta) * 0.4]
# draw pivot point
gr.fillarea([0.46, 0.54, 0.54, 0.46], [0.79, 0.79, 0.81, 0.81]),
gr.setlinecolorind(1)
gr.setlinewidth(2)
gr.polyline(x, y) # draw rod
gr.setmarkersize(5)
gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE)
gr.setmarkercolorind(86)
gr.polymarker([x[1]], [y[1]]) # draw bob
gr.setlinecolorind(4)
V = 0.05 * omega # show angular velocity
gr.drawarrow(x[1], y[1], x[1] + V * np.cos(theta), y[1] + V * np.sin(theta))
gr.setlinecolorind(2)
A = 0.05 * acceleration # show angular acceleration
gr.drawarrow(x[1], y[1], x[1] + A * np.sin(theta), y[1] + A * np.cos(theta))
gr.settextfontprec(2, gr.TEXT_PRECISION_STRING)
gr.setcharheight(0.032)
gr.settextcolorind(1)
def drawGR(self):
if self.linetype is not None and self.x:
# preserve old values
lcolor = gr.inqlinecolorind()
gr.setlinewidth(2)
gr.setlinecolorind(self.linecolor)
for xi, yi in zip(self.x, self.y):
gr.polyline([xi, xi], [0, yi])
# restore old values
gr.setlinecolorind(lcolor)
gr.setlinewidth(1)
gr.setlinewidth(self.linewidth)
gr.polyline(self.x, self.y)
if (self.markertype != gr.MARKERTYPE_DOT and
self.markertype is not None):
gr.setmarkertype(self.markertype)
gr.polymarker(self.x, self.y)
elif self.markertype is not None:
gr.setmarkercolorind(self.markercolor)
gr.setmarkertype(self.markertype)
gr.polymarker(self.x, self.y)
if self.errorBar1:
self.errorBar1.drawGR()
if self.errorBar2:
self.errorBar2.drawGR()
# restore old values
gr.setlinecolorind(lcolor)
gr.setmarkercolorind(mcolor)
gr.setlinetype(ltype)
gr.setmarkertype(mtype)
gr.setlinewidth(lwidth)
x = [0.5, 0.5 + sin(theta) * 0.4]
y = [0.8, 0.8 - cos(theta) * 0.4]
gr.fillarea(4, # draw pivot point
[0.46, 0.54, 0.54, 0.46], [0.79, 0.79, 0.81, 0.81])
gr.setlinecolorind(1)
gr.setlinewidth(2)
gr.polyline(2, x, y) # draw rod
gr.setmarkersize(5)
gr.setmarkertype(gr.MARKERTYPE_SOLID_CIRCLE)
gr.setmarkercolorind(86)
gr.polymarker(1, [x[1]], [y[1]]) # draw bob
gr.setlinecolorind(4)
V = 0.05 * omega # show angular velocity
gr.drawarrow(x[1], y[1], x[1] + V*cos(theta), y[1] + V*sin(theta))
gr.setlinecolorind(2)
A = 0.05 * acceleration # show angular acceleration
gr.drawarrow(x[1], y[1], x[1] + A*sin(theta), y[1] + A*cos(theta))
gr.settextfontprec(2, gr.TEXT_PRECISION_STRING)
gr.setcharheight(0.032)
gr.settextcolorind(1)
gr.textext(0.05, 0.95, 'Damped Pendulum')
gr.setcharheight(0.040)
gr.mathtex(0.4, 0.22, '\\omega=\\dot{\\theta}')
gr.mathtex(0.4, 0.1, '\\dot{\\omega}=-\\gamma\\omega-\\frac{g}{l}sin(\\theta)')
gr.setcharheight(0.028)
gr.textext(0.05, 0.22, 't:%7.2f' % t)
gr.textext(0.05, 0.16, '\\theta:%7.2f' % (theta / pi * 180))
gr.settextcolorind(4)
gr.textext(0.05, 0.10, '\\omega:%7.2f' % omega)
gr.settextcolorind(2)
elif location in (2, 3, 6):
px = viewport[0] + 0.11
else:
px = viewport[1] - 0.05 - w
if location in (5, 6, 7, 10):
py = 0.5 * (viewport[2] + viewport[3] + h) - 0.03
elif location in (3, 4, 8):
py = viewport[2] + h
else:
py = viewport[3] - 0.06
gr.setfillintstyle(gr.INTSTYLE_SOLID)
gr.setfillcolorind(0)
gr.fillrect(px - 0.08, px + w + 0.02, py + 0.03, py - 0.03 * num_lines)
gr.setlinetype(gr.LINETYPE_SOLID)
gr.setlinecolorind(1)
gr.setlinewidth(1)
gr.drawrect(px - 0.08, px + w + 0.02, py + 0.03, py - 0.03 * num_lines)
i = 0
gr.uselinespec(" ")
for (x, y, z, c, spec) in _plt.args:
gr.savestate()
mask = gr.uselinespec(spec)
if mask in (0, 1, 3, 4, 5):
gr.polyline([px - 0.07, px - 0.01], [py, py])
if mask & 2:
gr.polymarker([px - 0.06, px - 0.02], [py, py])
gr.restorestate()
gr.settextalign(gr.TEXT_HALIGN_LEFT, gr.TEXT_VALIGN_HALF)
if i < num_labels:
gr.textext(px, py, _plt.kwargs['labels'][i])
i += 1