-
Notifications
You must be signed in to change notification settings - Fork 0
/
mic_testing.py
232 lines (143 loc) · 4.99 KB
/
mic_testing.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import numpy as np
from pythonAudioMeasurements.polarData import polarData
from pythonAudioMeasurements.audioSample import audioSample
from pythonAudioMeasurements.microphone import Microphone
from pythonAudioMeasurements.microphoneArray import MicrophoneArray
import matplotlib.pyplot as plt
from scipy.signal import convolve
import tensorflow as tf
filename = "/home/terrasa/UROP/polar-measurement/data/19_Jan15_fixedpkls/spv1840.pkl"
def test_mic_apply():
filename = "/home/terrasa/UROP/polar-measurement/data/19_Jan15/spv1840.pkl"
pd = polarData.fromPkl(filename)
pd.plotAngle(90)
# position in mm
mic = Microphone(pd, [-500,2000])
fs = 44.1e3
length = 100000
n = np.arange(length)
f_targ = (fs/length) * 4900 # (2pi/ T)*k
# f_targ_2 = 2300
sin_wave_1 = np.sin(n*(2*np.pi)*(f_targ/fs))
# sin_wave_2 = np.sin(n*(2*np.pi)*(f_targ_2/fs))
sin_wave = audioSample(sin_wave_1, type="t", Fs=fs)
# sin_wave.hanning()
pad = 10000
# sin_wave.hanning()
# sin_wave.zeroPadStart(pad)
# sin_wave.zeroPadEnd(pad)
sin_wave.plot(both=True)
sin_shifted = mic.apply_microphone(sin_wave, 90)
# plot the resulting waveform
sin_shifted.toTime()
sin_shifted.plot(both=True)
def test_xy():
filename = "/home/terrasa/UROP/polar-measurement/data/19_Jan15/spv1840.pkl"
pd = polarData.fromPkl(filename)
pd.plotAngle(90)
# position in mm
mic = Microphone(pd, [-500,2000])
fs = 44.1e3
length = 100000
n = np.arange(length)
f_targ = (fs/length) * 4000 # (2pi/ T)*k
# f_targ_2 = 2300
sin_wave_1 = np.sin(n*(2*np.pi)*(f_targ/fs))
# sin_wave_2 = np.sin(n*(2*np.pi)*(f_targ_2/fs))
sin_wave = audioSample(sin_wave_1, type="t", Fs=fs)
pad = 10000
# sin_wave.hanning()
sin_wave.zeroPadStart(pad)
sin_wave.zeroPadEnd(pad)
sin_wave.plot(both=True)
for theta in range(0,20,15):
sin_shifted = mic.apply_xy(sin_wave, theta)
sin_shifted.plot(both=True)
def simulate_polar_1mic():
filename = "/home/terrasa/UROP/polar-measurement/data/19_Jan15/spv1840.pkl"
pd = polarData.fromPkl(filename)
pd.plotAngle(0, both=True)
# position in mm
mic = Microphone(pd, [-500,2000])
fs = 44.1e3
length = 100000
n = np.arange(10000)
f_options = np.int32(np.logspace(2,4, 6))*2*(fs/length)
plt.figure(1)
for f_test in f_options:
sin_wave = np.sin(n*(2*np.pi)*(f_test/fs))
sin_wave = audioSample(sin_wave, type="t", Fs=fs)
# sin_wave.hanning()
thetas = np.array(list(range(0, 361, 1)))
mags = []
for theta in thetas:
print(f_test, theta)
result = mic.apply(sin_wave, theta)
# sin_wave.plot(both=True)
# result.plot(both=True)
# get magnitude of the closest frequency of the result
result.toDb()
mags.append(result.getFreq([f_test])[0].real)
plt.polar(thetas*np.pi/180, mags)
plt.title("RESULT")
plt.legend(np.int32(f_options), loc = "upper left")
pd.plotFreqs(f_options, fig=2)
def simulate_polar_array():
filename = "/home/terrasa/UROP/polar-measurement/data/19_Jan15/spv1840.pkl"
pd = polarData.fromPkl(filename)
fs = 44.1e3
length = 100000
n = np.arange(10000)
f_options = np.int32(np.logspace(2,4, 4))*2*(fs/length)
# pd.plotAngle(90)
f_1 = f_options[0]
f_2 = f_options[2]
c = 343e3
d_1 = c/(2*f_1)
d_2 = c/(2*f_2)
# position in mm
mic_1 = Microphone(pd, [0,0])
mic_2 = Microphone(pd, [d_1, 0])
mic_3 = Microphone(pd, [0, d_2])
mic_array = MicrophoneArray([mic_1, mic_2])
# mic_array = MicrophoneArray([mic_1, mic_2, mic_3])
plt.figure(2)
for f_test in f_options:
sin_wave = np.sin(n*(2*np.pi)*(f_test/fs))
sin_wave = 2/length*audioSample(sin_wave, type="t", Fs=fs)
# sin_wave.hanning()
thetas = np.array(list(range(0, 361, 2)))
mags = []
for theta in thetas:
print(f_test, theta)
result = mic_array.apply(sin_wave, theta)
# sin_wave.plot(both=True)
# result.plot(both=True)
# get magnitude of the closest frequency of the result
result.toDb()
mags.append(result.getFreq([f_test])[0].real)
plt.polar(thetas*np.pi/180, mags)
plt.legend(f_options, loc = "upper left")
mic_array.visualize()
def test_tf_prep():
pd = polarData.fromPkl(filename)
mic = Microphone(pd, (50,100))
mic.polar[30].plot(both=True)
mic.self_apply_xy()
mic.polar[30].plot(both=True)
angles, freqs, data = mic.tf_prep()
print(angles)
print(freqs)
print(data)
angles = tf.constant(angles)
freqs = tf.constant(freqs)
data = tf.constant(data)
print(angles)
print(freqs)
print(data)
if __name__ == "__main__":
# test_mic_apply()
# test_xy()
# simulate_polar_array()
# simulate_polar_1mic()
test_tf_prep()