-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignalpackage.py
More file actions
117 lines (91 loc) · 3.35 KB
/
signalpackage.py
File metadata and controls
117 lines (91 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import numpy as np
import matplotlib.pyplot as plt
class SignalImpulse:
def __init__(self, values=0, dt=0.001, T = 10, t0=0):
self.T = T
self.values = values
self.max_T = self._define_max_T(T)
self.dt = dt
self.t = np.linspace(-self.max_T,self.max_T,int(1/dt), endpoint=False)
self.t0 = t0
def _define_max_T(self, T):
if T <= 10:
return 10
else:
return 100
def show_values(self):
myfig = plt.figure(figsize=(10,4))
plt.plot(self.t, self.values)
plt.title(self.to_string())
return myfig
def show_convolution(self, in2: 'SignalImpulse'):
convolved = np.convolve(self.values, in2.values, mode="full")
# Normalize convolution
amp_max = np.max(self.values) * np.max(in2.values)
convolved = amp_max*convolved / np.max(np.abs(convolved))
# New time vector will be amplified
t_conv = np.linspace(-2*self.max_T, 2*self.max_T, len(convolved), endpoint=False)
myfig = plt.figure(figsize=(10, 4))
plt.plot(t_conv, convolved)
plt.title("Convolution")
plt.xlabel('Tiempo [s]')
plt.ylabel('Amplitud')
plt.xlim(-20, 20) # Establecer los límites del eje x de -20 a 20
plt.xticks(range(-20,21, 2))
return myfig
def to_string(self):
return "Custom Signal"
class unitImpulse(SignalImpulse):
def __init__(self, A, t0):
super().__init__(t0 = t0)
self.values = np.ones_like(self.t)
self.values = np.where(np.isclose(self.t, t0, atol=0.006), A, 0)
def to_string(self) -> str:
return "Delta Pulse"
class SquareImpulse(SignalImpulse):
def __init__(self, A, t0, T):
"""
Unit square impulse with custom amplitude.
Parameters
----------
A : int/float
Constant amplitude of the square.
t0 : int/float
Where the pulse will be centered.
T : int/float
Total width of the pulse.
The square will have value of A when abs(t) <= abs(t-T/2).
"""
# Generar el pulso rectangular
super().__init__(T = T, t0 = t0)
self.values = np.ones_like(self.t)
bound1 = t0 - (T/2)
bound2 = t0 + (T/2)
self.values = np.where((self.t >= bound1) & (self.t <= bound2), A, 0)
def to_string(self) -> str:
return "Square Pulse"
class TriangleImpulse(SignalImpulse):
def __init__(self, A, t0, T):
super().__init__(T = T, t0 = t0)
"""
Unit triangle impulse with custom amplitude.
Parameters
----------
A : int/float
Constant amplitude of the square.
t0 : int/float
Where the pulse will be centered.
T : int/float
Width of the pulse.
The triangle will have value of the equation describing the slope when abs(t) <= abs(t-T)
"""
self.A = A
self.values = self._generate_values()
def _generate_values(self):
values = np.zeros_like(self.t)
for i,t in enumerate(self.t):
if t>= self.t0 - self.T and t<= self.t0 + self.T:
values[i] = self.A * (1 - abs((t - self.t0) / (self.T)))
return values
def to_string(self) -> str:
return "Triangular Pulse"