forked from mohamedmosilhy/Sampling-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignal.py
64 lines (49 loc) · 1.97 KB
/
Signal.py
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
from Components import Components
import numpy as np
class Signal:
def __init__(self, name):
self.name = name
self.components = []
self.data = []
self.time = np.linspace(0, 3, 1000)
self.interpolated_data = None
self.snr = 1
self.old_noise = None
self.sample_rate = None
self.maxFreq = None
# either 0 or 1 (0 --> actual || 1 --> normalized )
self.sampling_mode = None
def add_component(self, component):
self.components.append(component)
def change_sampling_mode(self, mode_num):
self.sampling_mode = mode_num
def change_noise(self, new_noise):
self.old_noise = new_noise
def change_snr(self, new_snr):
self.snr = new_snr
def change_sample_rate(self, new_sample_rate):
self.sample_rate = new_sample_rate
def delete_component_after_preparing(self, component):
if component in self.components:
self.components.remove(component)
# re-generating the data
self.generate_points()
def delete_component_during_preparing(self, component):
if component in self.components:
self.components.remove(component)
def generate_signal(self):
self.generate_points()
self.sample_rate = self.maxFreq
self.sampling_mode = 0 # by default
def generate_points(self):
if self.components != []:
# Initialize self.samples as an empty array with the same shape
self.data = np.zeros(self.time.shape)
self.old_noise = np.zeros(self.time.shape)
# Generate the signal samples for each component and add them
for component in self.components:
self.data += component.amplitude * \
np.cos(2 * np.pi * component.frequency *
self.time + component.phase)
self.maxFreq = max(
[component.frequency for component in self.components])