Last active
May 16, 2020 16:50
-
-
Save fakuivan/b6a550d79c38b0bdb8bcb97a954fe9a8 to your computer and use it in GitHub Desktop.
Algunas resoluciones para los trabajos prácticos propuestos por la cátedra de la materia "Señales y Sistemas"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.8 | |
from sympy import Basic, Piecewise, Symbol, Eq, Mod, Heaviside, sympify, plot as splot | |
import numpy | |
from typing import Hashable, Tuple, Iterable, NamedTuple, Callable, Type | |
from matplotlib import pyplot as mplot | |
from sympy.physics.units.definitions import Hz | |
from sympy.physics.units.quantities import Quantity | |
from sympy.physics.units.prefixes import kilo | |
kilohertz = khz = kHz = Quantity("kilohertz", abbrev="kHz") | |
kHz.set_global_relative_scale_factor(kilo, Hz) | |
# Class used for discrete functions | |
# .zeroth is the index of the element for which n = 0, | |
# negative numbers imply that n = 0 is outside the list | |
class DiscreteSpace(NamedTuple): | |
values: Tuple[Basic, ...] | |
zeroth: Basic | |
@property | |
def n(self) -> Iterable[Basic]: | |
return (i - self.zeroth for i in range(len(self.values))) | |
def padded(self, right: int, filler: Basic = 0) -> "DiscreteSpace": | |
pad = (filler,) | |
zeroth = max(0, -right) + self.zeroth | |
return DiscreteSpace(pad * -right + self.values + pad * right, zeroth) | |
def centered(self, filler: Basic = 0) -> "DiscreteSpace": | |
return self.padded((self.zeroth*2 + 1) - len(self.values), filler) | |
def aligned_pair(self, | |
other: "DiscreteSpace", | |
filler: Basic = 0) -> Tuple["DiscreteSpace", "DiscreteSpace"]: | |
lower_diff = other.zeroth - self.zeroth | |
upper_diff = (len(other.values) - other.zeroth) - (len(self.values) - self.zeroth) | |
new_zeroth = max(other.zeroth, self.zeroth) | |
self_padded = (filler,) * lower_diff + self.values + (filler,) * upper_diff | |
other_padded = (filler,) * -lower_diff + other.values + (filler,) * -upper_diff | |
return (DiscreteSpace(self_padded, new_zeroth), | |
DiscreteSpace(other_padded, new_zeroth)) | |
def as_piecewise(self, var: Symbol, filler: Basic = 0) -> Piecewise: | |
arguments = [(value, Eq(var, n)) for value, n in zip(self.values, self.n)] | |
arguments += [(filler, Eq(Mod(var, 1), 0))] | |
return Piecewise(*arguments) | |
@classmethod | |
def from_sampling(cls, zeroth: Basic, nsamples: int, f: Callable[[Basic], Basic]): | |
samples = (f(i - zeroth) for i in range(nsamples)) | |
return cls(tuple(samples), zeroth) | |
# We'll use this to plot discrete spaces as scatter plots | |
def plot_discs(discs: DiscreteSpace, show=True, **kwargs): | |
mplot.scatter(list(discs.n), discs.values, **kwargs) | |
mplot.show() if show else None | |
def splot_colors(*exprs: Tuple[Basic, str], plotf: Type[splot], **options): | |
to_plot, colors = zip(*exprs) | |
show = options.pop("show", True) | |
plot = plotf(*to_plot, show=False, **options) | |
for i, color in enumerate(colors): | |
plot[i].line_color = color | |
plot.show() if show else None | |
return plot | |
# Converts an expression with units for the variable var into | |
# a dimentionless lambda | |
def ulambdify(expr: Basic, var: Symbol, unit_in, unit_out): | |
return lambda x: (expr.subs(var, x*unit_in) / unit_out) | |
# Splits a function of ``var`` into even and odd parts | |
def eo_split(f: Basic, var: Symbol): | |
print(f, var) | |
return ((f + f.subs(var, -var)) / 2, | |
(f - f.subs(var, -var)) / 2) | |
def boxcar(t_on: Basic, t_off: Basic, t: Symbol, unit = 1): | |
return Heaviside((t - t_on) / unit) - Heaviside((t - t_off) / unit) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jupyter | |
sympy | |
matplotlib | |
numpy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment