import numpy as np import matplotlib.pyplot as plt from funcoes import u, conv_disc t = np.arange(-2, 12) x = (u(t) - u(t - 4)) * t + (u(t - 4) - u(t - 8)) * (8 - t) h = np.zeros_like(t) h[t == 0] = 1 h[t == 1] = -1 y = conv_disc(x, h, t) plt.figure() plt.subplot(1, 2, 1) plt.stem(t, x) plt.xticks(t) plt.grid() plt.legend(['x[n]']) plt.xlabel('n') plt.axis([t.min(), t.max(), -.5, 4.5]) plt.subplot(1, 2, 2) plt.stem(t, h) plt.xticks(t) plt.grid() plt.legend(['h[n]']) plt.xlabel('n') plt.axis([t.min(), t.max(), -1.5, 1.5]) plt.figure() plt.stem(t, y) plt.xticks(t)
ax_x = sp[0].axes[0] ax_y = sp[0].axes[1] plotit(title='x(t)', ax=ax_x, x=t, y=x_quizz(t), axis=axis, xticks=xticks, wait=False) plotit(title='y(t)', ax=ax_y, x=t, y=x_quizz(t+2), axis=axis, xticks=xticks, wait=False) plt.gcf().set_size_inches(7.41, 2.45) sp = plt.subplots(1, 2) ax_x = sp[0].axes[0] ax_y = sp[0].axes[1] plotit(title='x(t)', ax=ax_x, x=t, y=x_quizz(t), axis=axis, xticks=xticks, wait=False) plotit(title='y(t)', ax=ax_y, x=t, y=x_quizz(-t+2), axis=axis, xticks=xticks, wait=False) plt.gcf().set_size_inches(7.41, 2.45) sp = plt.subplots(1, 2) ax_x = sp[0].axes[0] ax_y = sp[0].axes[1] plotit(title='x(t)', ax=ax_x, x=t, y=x_quizz(t), axis=axis, xticks=xticks, wait=False) plotit(title='y(t)', ax=ax_y, x=t, y=x_quizz(-t-2), axis=axis, xticks=xticks, wait=False, ) plt.gcf().set_size_inches(7.41, 2.45) sp = plt.subplots(1, 1) ax_x = sp[0].axes[0] x = np.zeros_like(t) plotit(title='x(t)', ax=ax_x, x=t, y=x, axis=axis, xticks=xticks, wait=False, dirac=[[1, 2],[2, 1]]) plt.gcf().set_size_inches(7.41, 2.45) sp = plt.subplots(1, 1) ax_x = sp[0].axes[0] x = u(t) + u(t-2) - 2*u(t-3) plotit(title='x(t)', ax=ax_x, x=t, y=x, axis=axis, xticks=xticks, wait=False) plt.gcf().set_size_inches(7.41, 2.45)
def x_quizz(t): return (2/3)*(u(t)-u(t-3)) * t
def x_def(t): return (1 - 1e-3/np.pi * t) * (u(t)-u(t-1e3*np.pi))
def h_def(t): return .5 * (u(t) - u(t - 2))
def meuh(t): return u(t) - u(t - 2)
import matplotlib.pyplot as plt def pixaxis(ax): ticks_loc = ax.get_xticks().tolist() ax.set_xticks(ax.get_xticks().tolist()) ax.set_xticklabels(["{:.0f}".format(x / np.pi) + 'π' for x in ticks_loc]) # Questão 1a sp = plt.subplots(1, 2) ax_x = sp[0].axes[0] ax_y = sp[0].axes[1] t = np.arange(-3, 16, .01) xtick = np.arange(-3, 16) axis = [-1, 15, -.5, 10.2] x = u(t) h = 2*u(t) - u(t-3) - u(t-6) y = conv(x, h, t) plotit(x=t, y=x, ax=ax_x, axis=axis, xticks=xtick) plotit(x=t, y=y, ax=ax_y, axis=axis, wait=False, save=False, xticks=xtick) sp = plt.subplots(1, 1) ax = sp[0].axes[0] plotit(x=t, y=y, ax=ax, axis=[-1, 15, -1, 10], xlabel='t [s]', ylabel='y(t)', xticks=xtick, linewidth=3) # Questão 1b x = impulse(t)+impulse(t-1)+impulse(t-2) y = conv(x, h, t) sp = plt.subplots(1, 2) ax_x = sp[0].axes[0] ax_y = sp[0].axes[1]
from graficos import myplot as plotit from funcoes import u, window from scipy.signal import step, lti import numpy as np import matplotlib.pyplot as plt sp = plt.subplots(2, 1) ax_x = sp[0].axes[0] ax_y = sp[0].axes[1] axis = [-1, 2.7, -.2, 1.2] t = np.arange(-1, 2.7, .01) x = u(t) y = .5 * x plotit(ax=ax_x, x=t, y=x, axis=axis, wait=False) plotit(ax=ax_y, x=t, y=y, axis=axis, wait=False) sp = plt.subplots(2, 1) ax_x = sp[0].axes[0] ax_y = sp[0].axes[1] axis = [-1, 2.7, -.2, 1.2] t = np.arange(-1, 2.7, .01) x = u(t) sys = lti([20], [1, 5, 40]) t_step, y = step(sys) plotit(ax=ax_x, x=t, y=x, axis=axis, wait=False) plotit(ax=ax_y, x=t_step, y=y, axis=axis, wait=False, hold=True) plotit(ax=ax_y, x=[t[0], 0], y=[0, 0], axis=axis, wait=False, hold=True)
import matplotlib.pyplot as plt def pixaxis(ax): ticks_loc = ax.get_xticks().tolist() ax.set_xticks(ax.get_xticks().tolist()) ax.set_xticklabels(["{:.0f}".format(x / np.pi) + 'π' for x in ticks_loc]) # Questão 1 sp = plt.subplots(1) ax_x = sp[0].axes[0] t = np.arange(-3, 6, .01) xtick = np.arange(-3, 7) axis = [-1, 6, -.2, 2.2] h = (-t + 2) * (u(t)-u(t-2)) plotit(x=t, y=h, ax=ax_x, axis=axis, xticks=xtick, yticks=[0, 1, 2], xlabel='t [s]', ylabel='h(t)', linewidth=3) # Questão 1a sp = plt.subplots(1) ax_x = sp[0].axes[0] x = u(t) y = conv(x, h, t) plotit(x=t, y=y, ax=ax_x, axis=axis, xticks=xtick, xlabel='t [s]', ylabel='y(t)', linewidth=3) # Questão 1b sp = plt.subplots(1) ax_x = sp[0].axes[0] x = u(t) - u(t-3) y = conv(x, h, t) plotit(x=t, y=y, ax=ax_x, axis=axis, xticks=xtick, xlabel='t [s]', ylabel='y(t)', linewidth=3)
def get_delay_secs(self): return self.delay def get_product(self): return self.x * self.h_func(-t + self.delay) def get_y_trunc(self): idx_now = np.argmin(np.abs(t - self.delay)) t_trunc = self.t[:idx_now] y_trunc = self.y[:idx_now] return np.array([t_trunc, y_trunc]) t = np.arange(-10, 10, .001) x = (-t + 1) * (u(t) - u(t - 1)) c = Convolver(x, h_def, t, .1) tmin = -5 tmax = 5 xticks = np.arange(-10, 10, 1) def on_key_press(event): # print(format(event.key)) if event.key == 'right': if c.direction == 'stop': c.direction = 'right' else: c.direction = 'stop' elif event.key == 'left': if c.direction == 'stop':
def h_def(t): return u(t) - u(t - 4)
def realpole(w, w0): magnitude = u(w) - u(w-w0) + \ (w0/w) * (u(w-w0))
def pixaxis(ax): ticks_loc = ax.get_xticks().tolist() ax.set_xticks(ax.get_xticks().tolist()) ax.set_xticklabels(["{:.0f}".format(x / np.pi) + 'π' for x in ticks_loc]) def square(t, t0, t_init, min, max, duty): t_per = np.mod(t - t_init, t0) / t0 x = np.zeros_like(t) x[np.where(np.abs(t_per) <= duty)] = max x[np.where(np.abs(t_per) > duty)] = min return x # espectro do sinal exemplo sp = plt.subplots(1, 1) ax_abs = sp[0].axes[0] w = np.arange(-4 * np.pi, 4.001 * np.pi, 1e-3) h = u(w + 2 * np.pi) - u(w - 2 * np.pi) xtick = np.arange(w[0], w[-1], np.pi) plotit(ax=ax_abs, x=w, y=h, axis=[w[0], w[-1], -.1, 1.1], axes=False, save=False, title='$H(\omega)$', xlabel='$\omega$', xticks=xtick, wait=True) pixaxis(ax_abs)
ax.set_xticklabels(["{:.0f}".format(x / np.pi) + 'π' for x in ticks_loc]) def square(t, t0, t_init, min, max, duty): t_per = np.mod(t - t_init, t0) / t0 x = np.zeros_like(t) x[np.where(np.abs(t_per) <= duty)] = max x[np.where(np.abs(t_per) > duty)] = min return x # Questao SLIT sp = plt.subplots(1, 1) ax_abs = sp[0].axes[0] t = np.arange(-1, 8, 1e-3) h = np.exp(-1 * t) * u(t) - np.exp(-2 * t) * u(t) plotit(ax=ax_abs, x=t, y=h, axes=True, axis=[t[0], t[-1], -0.1, h.max() * 1.1], save=False, title='h(t)', xlabel='t', wait=False) sp = plt.subplots(1, 1) ax_abs = sp[0].axes[0] t = np.arange(-1, 7, 1e-3) h = .5 * np.exp(.1 * t) * u(t) - np.exp(-1 * t) * u(t) plotit(ax=ax_abs,
def get_product(self): return self.x * self.h_func(-t + self.delay) def get_y_trunc(self): idx_now = np.argmin(np.abs(t - self.delay)) t_trunc = self.t[:idx_now] y_trunc = self.y[:idx_now] return np.array([t_trunc, y_trunc]) t = np.arange(-10, 10, .01) #x = gaussian(t, 2, 1.5) #x = np.zeros_like(t) #for k in range(20): # x += u(t-k) x = u(t) - u(t - 2) + 2 * (u(t - 4) - u(t - 7)) def ret(t): return u(t) - u(t - 1) def meuh(t): return u(t) - u(t - 2) c = Convolver(x, meuh, t, .5) #c = Convolver(x, u, t, .5) tmin = -5 tmax = 5 xticks = np.arange(-10, 10, 1)
def x_def(t): return (1 - 1/(100*np.pi) * t) * (u(t)-u(t-100*np.pi))
def ret(t): return u(t) - u(t - 1)
sp = plt.subplots(1, 1) ax_x = sp[0].axes[0] t = np.arange(-.1, .1, 1e-5) axis = [-.02, .08, -.1, 1.1] # Onda quadrada x = square(t, .005, -.0025 / 2, 0., 1., .5) plotit(ax=ax_x, x=t, y=x, axis=axis, axes=True, title='x(t)', save=False) h = x_RC(t, 1, 10e-3) y = conv(x, h, t) ax_x = plt.subplots(1, 1)[0].axes[0] plotit(ax=ax_x, x=t, y=y, axis=axis, axes=True, title='y(t)', save=False) # Pulso x = u(t + .0025 / 2) - u(t - .0025 / 2) ax_x = plt.subplots(1, 1)[0].axes[0] plotit(ax=ax_x, x=t, y=x, axis=axis, axes=True, title='x(t)', save=False) h = x_RC(t, 1, 10e-3) y = conv(x, h, t) ax_x = plt.subplots(1, 1)[0].axes[0] plotit(ax=ax_x, x=t, y=y, axis=axis, axes=True, title='y(t)', save=False) # Sequencia com período crescente sp = plt.subplots(3, 1) ax_x = sp[0].axes[0] ax_abs = sp[0].axes[1] ax_ang = sp[0].axes[2] w0_0 = 2 * np.pi / .005 n = np.arange(-50, 50) for i in np.arange(0, 10, 1):
def h_def(t): R = 2 L = 1 return np.exp(-t * R / L) * u(t) / L
def x_RC(t, R, C): return np.exp(-t / (R * C)) * u(t) / (R * C)
def h_def(t): return 2*u(t) - u(t-3) - u(t-6)
positive_samples = int(np.floor(t[-1] / width)) for i in range(1, positive_samples): x_hat = x_hat + r_(t - i*width, width) * x[t0 + i*width_s] return x_hat def x_RLi(t, R, L): return np.exp(-t*R/L)*u(t) / L sp = plt.subplots(1, 2) ax_x = sp[0].axes[0] ax_y = sp[0].axes[1] t = np.arange(-3, 10, .01) xtick = np.arange(-3, 10) axis = [-1, 5, -.5, 2] h = impulse(t) / 2 x = u(t) y = conv(x, h, t) plotit(x=t, y=x, ax=ax_x, axis=axis, xticks=xtick) plotit(x=t, y=y, ax=ax_y, axis=axis, wait=True, save=True, xticks=xtick) x = impulse(t) y = conv(x, h, t) plotit(x=t, y=x, ax=ax_x, axis=axis, dirac=[[0], [1]], xticks=xtick) plotit(x=t, y=y, ax=ax_y, axis=axis, save=True, dirac=[[0], [.5]], xticks=xtick) x = impulse(t) + 2 * impulse(t - 1.5) - .5*impulse(t - 3.) y = conv(x, h, t) plotit(x=t, y=x, ax=ax_x, axis=axis, dirac=[[0, 1.5, 3.0], [1, 2, -.5]], xticks=xtick) plotit(x=t, y=y, ax=ax_y, axis=axis, save=True, dirac=[[0, 1.5, 3], [.5, .75, -.25]], xticks=xtick) h = x_RLi(t, R=2, L=1) x = u(t) y = conv(x, h, t)
def get_delay_secs(self): return self.delay def get_product(self): return self.x * self.h_func(-t + self.delay) def get_y_trunc(self): idx_now = np.argmin(np.abs(t - self.delay)) t_trunc = self.t[:idx_now] y_trunc = self.y[:idx_now] return np.array([t_trunc, y_trunc]) t = np.arange(-10, 10, .001) x = t * (u(t) - u(t-1)) c = Convolver(x, h_def, t, .2) tmin = -5 tmax = 5 xticks = np.array([0, 1, 2]) def on_key_press(event): # print(format(event.key)) if event.key == 'right': if c.direction == 'stop': c.direction = 'right' else: c.direction = 'stop' elif event.key == 'left': if c.direction == 'stop':
def x_RLi(t, R, L): return np.exp(-t*R/L)*u(t) / L
from graficos import myplot as plotit from funcoes import u, window, integrate, conv, movavgc, movavgpast, derivate, impulse import numpy as np import matplotlib.pyplot as plt def piyaxis(ax): ticks_loc = ax.get_yticks().tolist() ax.set_yticks(ax.get_yticks().tolist()) ax.set_yticklabels(["{:.3f}".format(y / np.pi) + 'π' for y in ticks_loc]) # Questao 2b sp = plt.subplots(1, 1) ax_x = sp[0].axes[0] w = np.arange(-30, 30, .1) xtick = np.arange(w[0], w[-1], 5) ytick = np.linspace(0, np.pi/20, 3) resp = (np.pi/20) * (u(w+20)-u(w+15)+u(w-15)-u(w-20)) axis = [w[0], w[-1], -.1*np.max(resp), 1.1*np.max(resp)] plotit(x=w, y=resp, ax=ax_x, axis=axis, xticks=xtick, yticks=ytick, xlabel='$\omega$', title='$Y(\omega)$') piyaxis(ax_x)
from graficos import myplot as plotit from funcoes import u, window, integrate, conv, movavgc, movavgpast, derivate, exp_u, sinc import numpy as np import matplotlib.pyplot as plt t = np.arange(-1, 5, .01) h = u(t) - u(t - 1) x = np.cos(3 * np.pi * t) * u(t) ya = conv(x, h, t) x = np.cos(2 * np.pi * t) * u(t) yb = conv(x, h, t) plt.subplot(3, 1, 1) plt.plot(t, h) plt.subplot(3, 1, 2) plt.plot(t, x) plt.subplot(3, 1, 3) plt.plot(t, ya) plt.figure() plt.subplot(2, 1, 1) plt.plot(t, ya) plt.grid() plt.xlabel('t [s]') plt.ylabel('Questão (1a)') plt.legend(['y(t)']) plt.subplot(2, 1, 2) plt.plot(t, yb) plt.grid() plt.xlabel('t [s]') plt.ylabel('Questão (1b)')
def xfunc(t): x = (np.exp(-2 * t) - np.exp(-15 * t)) * u(t) x = 2 * x / x.max() return x
def x_t(t): return u(t) - u(t - 1)
axis=[t[0], t[-1], -.2, 1.2], wait=False, linewidth=2) sp = plt.subplots(1, 1) ax_x = sp[0].axes[0] t = np.arange(-2.2, 1.8, .001) x = np.exp(t) plotit(ax=ax_x, x=t, y=x, axis=[t[0], t[-1], -.5, 6], wait=False, linewidth=2) # Causais e não causais sp = plt.subplots(1, 1) ax_x = sp[0].axes[0] axis = [-1, 2.7, -.2, .8] t = np.arange(-1, 5, .01) x = u(t) sys = lti([20], [1, 5, 40]) t_step, y = step(sys, T=t * (t >= 0)) plotit(ax=ax_x, x=t_step, y=y, axis=axis, wait=False, hold=True) plotit(ax=ax_x, x=[t[0], 0], y=[0, 0], axis=axis, wait=False, hold=True) plt.gcf().set_size_inches(6.4, 3.05) sp = plt.subplots(1, 1) ax_x = sp[0].axes[0] plotit(ax=ax_x, x=t[:-50], y=y[50:], axis=axis, wait=False, hold=True) plt.gcf().set_size_inches(6.4, 3.05) # Quizz: analógico / digital plt.figure() plt.subplot(1, 2, 1) t = np.arange(-2, 5, .01)
t = np.arange(-.1, .1, 1e-5) axis = [-.02, .02, -.02, 1.02] # Onda quadrada x = square(t, .01, 0., .4, 1., .5) plotit(ax=ax_x, x=t, y=x, axis=axis, axes=True, xlabel='t [s]', title='x(t)', save=False) sp = plt.subplots(1, 1) ax_x = sp[0].axes[0] w = np.arange(-150 * np.pi, 150 * np.pi, .5) axis = [-150 * np.pi, 150 * np.pi, -.2, 3.2] # Onda quadrada x = 3 * (u(w + 100 * np.pi) - u(w + 80 * np.pi) + u(w - 80 * np.pi) - u(w - 100 * np.pi)) plotit(ax=ax_x, x=w, y=x, axis=axis, axes=True, xlabel='ω [rad/s]', title='|Y(ω)|', save=False, xticks=np.array([-100, -80, 0, 80, 100]) * np.pi) pixaxis(ax_x)