-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignal.py
More file actions
131 lines (106 loc) · 4.58 KB
/
Copy pathSignal.py
File metadata and controls
131 lines (106 loc) · 4.58 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Import the required packages
import numpy as np
from scipy.signal import chirp
from scipy.signal import square
from scipy.signal import sawtooth
from scipy.signal import sweep_poly
# Building a class Signal for better use.
class Signal:
"""
Generate sinusoidal signals with specific parameters.
Example:
signal = Signal(amplitude=10, sampling_rate=2000.0)
sine = signal.sine()
cosine = signal.cosine()
"""
def __init__(self, amplitude=1, duration=1, sampling_rate=100.0):
"""
Initialize the Signal class.
Args:
amplitude (float): The amplitude of the signal
duration (float): The duration of the signal in second
sampling_rate (float): The sampling per second of the signal
Additional parameters,which are required to generate the signal, are
calculated and defined to be initialized here too:
time_step (float): 1.0/sampling_rate
time_axis (np.array): Generate the time axis from the duration and
the time_step of the signal. The time axis is
for better representation of the signal.
"""
self.amplitude = amplitude
self.duration = duration
self.sampling_rate = sampling_rate
self.time_step = 1.0/self.sampling_rate
self.time_axis = np.arange(0, self.duration, self.time_step)
# Generate sine wave
def sine(self, frequency=1.0, phase=0):
"""
Method of Signal
Args:
frequency (float): The frequency of the signal Hz
phase (float): The phase of the signal in radians
Returns:
np.array of sine wave using the pre-defined variables
"""
return self.amplitude*np.sin(2*np.pi*frequency*self.time_axis+phase)
# Generate cosine wave
def cosine(self, frequency=1.0, phase=0):
"""
Method of Signal
Args:
frequency (float): The frequency of the signal Hz
phase (float): The phase of the signal in radians
Returns:
np.array of cosine wave using the pre-defined variables
"""
return self.amplitude*np.cos(2*np.pi*frequency*self.time_axis+phase)
# Generate chirp signal
def chrip_signal(self, frequency_start=1, frequency_end=10, method='linear', vertex=True, phase=0):
"""
Method of Signal
Args:
frequency_start (float): The frequency to start the chirp [Hz]
frequency_end (float): The frequency to end the chirp [Hz]
method (str): Method to sweep the frequencies ['linear', 'quadratic', 'logarithmic', 'hyperbolic']
vertex (bool): Determine whether the vertex of the parabola that is the graph of the frequency is at t=0 or t=duration
phase (float): The phase of the signal in radians
Returns:
np.array of chirp wave using the pre-defined variables
"""
return self.amplitude*chirp(t=self.time_axis,
f0=frequency_start,
f1=frequency_end,
t1=self.duration,
method=method,
phi=phase*(180/np.pi),
vertex_zero=vertex)
# Generate square wave
def square_signal(self, frequency=1.0):
"""
Method of Signal
Args:
frequency (float): The frequency of the signal [Hz]
Returns:
np.array of square wave using the pre-defined variables
"""
return self.amplitude*square(2 * np.pi * frequency * self.time_axis)
# Generate sawtooth wave
def sawtooth_signal(self, frequency=1.0):
"""
Method of Signal
Args:
frequency (float): The frequency of the signal [Hz]
Returns:
np.array of sawtooth wave using the pre-defined variables
"""
return self.amplitude*sawtooth(2*np.pi*frequency*self.time_axis)
# Generate sweep poly signal
def poly_signal(self, poly=np.poly1d(list(np.linspace(0, 1, num=4))), phase=0):
"""
Method of Signal
Args:
poly (list): List of the polynomial coefficients.
Returns:
np.array of sweep poly signal using the pre-defined variables
"""
return self.amplitude*sweep_poly(t=self.time_axis, poly=poly, phi=phase*(180/np.pi))