This repository has been archived by the owner on Dec 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_sound.py
184 lines (170 loc) · 7.88 KB
/
my_sound.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import numpy as np
# #=========================================================
# # Input Arguments
# #=========================================================
# with open("parameters.yml", 'r') as stream:
# D = yaml.safe_load(stream)
#
# for key in D:
# globals() [str(key)] = D[key]
# print('{}: {}'.format(str(key), D[key]))
# # transforms key-names from dictionary into global variables, then assigns those variables their respective key-values
# ========================================================
# CLASS: Sound Wave (composed of multiple pressure waves)
# ========================================================
class SoundWave:
def __init__(self, amp_range, freq_range, num_waves):
assert type(num_waves) == int
assert num_waves >= int(1)
assert np.size(amp_range) == 2
assert np.size(freq_range) == 2
# -----------------------------------------
self.n_waves = num_waves
self.amp_range = amp_range
self.freq_range = freq_range
self.waves = self.generate_waves()
#print("__init__(SoundWave)")
#print(f"amp_range = {amp_range}")
# ----------------------------------------------------------------
def generate_waves(self):
assert self.amp_range[1] >= self.amp_range[0]
assert self.freq_range[1] >= self.freq_range[0]
# -----------------------------------------
amp_samples = self.poisson_normalize(self.amp_range[0], self.amp_range[1], self.n_waves)
amp_samples = np.sort(amp_samples) # increasing order, smallest to largest
freq_samples = self.lognormal_interval(self.freq_range[0], self.freq_range[1], self.n_waves)
#freq_samples = np.sort(freq_samples)
#freq_samples = freq_samples[::-1] # decreasing order, largest to smallest
#print(f"sum of amps before = {np.sum(amp_samples)}")
# -----------------------------------------
waves_list = list()
for j in range(self.n_waves):
waves_list.append(self.PressureWave(amp_samples[j], freq_samples[j]))
return waves_list
# ----------------------------------------------------------------
def pressure(self, t):
assert t >= 0.0
assert np.size(self.waves) > 0
sum_pos = 0.0
sum_neg = 0.0
# ---------------------------------
#if np.size(self.waves) == 0:
# self.generate_waves()
# --------------------------------
for j in range(np.size(self.waves)):
temp = self.waves[j].get_pressure(t)
#sum_abs = sum_abs + abs(temp)
if temp >= 0.0:
sum_pos += temp # sums all positive pressure to avoid catastrophic cancellation
else:
sum_neg += temp # sums all negative pressures to avoid catastrophic cancellation
#print("pressure(t):")
#print(f"mean(|pressure|) = {sum_abs/self.n_waves}")
# --------------------------------
return sum_neg + sum_pos
# --------------------------------------------------------------------
def pressure_dot(self, t):
assert t >= 0.0
assert np.size(self.waves) > 0
sum_pos = 0.0
sum_neg = 0.0
# --------------------------------
#if np.size(self.waves) == 0:
# self.generate_waves(self)
# --------------------------------
for j in range(np.size(self.waves)):
temp = self.waves[j].get_pressure_dot(t)
if temp >= 0:
sum_pos += temp # sums all positive pressure to avoid catastrophic cancellation
else:
sum_neg += temp # sums all negative pressures to avoid catastrophic cancellation
# --------------------------------
return sum_pos + sum_neg
# --------------------------------------------------------------------
@staticmethod
def lognormal_normalize(A, B, N):
# RETURN: random sample following a log_normal distribution that sums to 'x' in the interval {A < x < B}
# A: minimum sum of samples
# B: maximum sum of samples
# ------------------------------------------
assert B >= A
mu, sigma = 0.0, 1.0
r = np.random.lognormal(mu, sigma, N)
r = r / np.sum(r)
return r * np.random.uniform(A,B) # normalized and then scaled to desired random sum
# --------------------------------------------------------------------
@staticmethod
def lognormal_interval(A, B, N):
# RETURN: random sample following a log_normal distribution mapped to the interval {A < x < B}
# A: minimum of any sample
# B: maximum of any sample
# note: 4 is the "apparent" max of log_normal(0,1)
# ------------------------------------------
assert B >= A
mu, sigma = 0.0, 1.0
r = np.random.lognormal(mu, sigma, N)
max_r = np.max([4,np.max(r)])
return np.ones(N) * A + r * (B - A) / max_r
# ------------------------------------------
# derivation of interval scaling:
# (1) { 0 < r < M } --> { 0 < r/M < 1 } the random distribution
# (2) { A < x < B } --> { 0 < (x-A)/(B-A) < 1 } the desired distribution
# (1) & (2) x-A = r/M (B-A)
@staticmethod
def poisson_normalize(A, B, N):
# RETURN: random sample following a poisson distribution that sums to 'x' in the interval {A < x < B}
# A: minimum sum of samples
# B: maximum sum of samples
# ------------------------------------------
assert B >= A
mu = 3.0
r = np.random.poisson(mu, N)
r = r / np.sum(r)
return r * np.random.uniform(A, B) # normalized and then scaled to desired random sum
# --------------------------------------------------------------------
@staticmethod
def poisson_interval(A, B, N):
# RETURN: random sample following a poisson distribution mapped to the interval {A < x < B}
# A: minimum of any sample
# B: maximum of any sample
# note: 9 is the "apparent" max of poisson(3)
# ------------------------------------------
assert B >= A
mu = 3.0
r = np.random.poisson(mu, N)
max_r = np.max([9, np.max(r)])
return np.ones(N) * A + r * (B - A) / max_r
# ------------------------------------------
# derivation of interval scaling:
# (1) { 0 < r < M } --> { 0 < r/M < 1 } the random distribution
# (2) { A < x < B } --> { 0 < (x-A)/(B-A) < 1 } the desired distribution
# (1) & (2) x-A = r/M (B-A)
# ========================================================
# SUB-CLASS: Pressure Wave
# ========================================================
class PressureWave:
__slots__ = ('amplitude','frequency','time_init')
# ----------------------------------------------------------
def __init__(self, amp, freq, t0=None):
self.amplitude = amp
self.frequency = freq
if t0 == None:
self.time_init = (-1) * np.random.uniform(0, 1) * (1 / freq)
else:
self.time_init = t0
# ----------------------------------------------------------
def get_pressure(self, t):
duration = t - self.time_init
assert duration >= 0
return self.amplitude * np.sin(2 * np.pi * self.frequency * duration)
#return pressure
# ----------------------------------------------------------
def get_pressure_dot(self, t):
duration = t - self.time_init
assert duration >= 0
return self.amplitude * 2 * np.pi * self.frequency * np.cos(2 * np.pi * self.frequency * duration)
#return pressure_dot
# ---------------------------------------------------------
# ========================================================
# UTILITY FUNCTIONS: archived for later use
# ========================================================