-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlight_aided_beamforming.py
131 lines (101 loc) · 4 KB
/
light_aided_beamforming.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
'''
This is a simple example of beamforming where the VAD function is provided by a
sound-to-light sensor
'''
import numpy as np
from scipy.io import wavfile
from scipy import signal as sig
from scipy import linalg as la
import pyroomacoustics as pra
import matplotlib.pyplot as plt
from max_sinr_beamforming import compute_variances, compute_gain
from light_array import LightArray2
### Parameters ###
fs_sound = 16000
fs_light = 30
nfft = 512 # approximately 30 overlapping frames / second
vad_thresh = -30 # dB
r, target_audio = wavfile.read('../audio/DR1_FAKS0_SX133_SX313_SX223.wav')
assert r == fs_sound
target_audio = np.concatenate([np.zeros(fs_sound), target_audio.astype(np.float32)])
target_audio /= target_audio.max()
sigma_s = np.std(target_audio)
sound_time = np.arange(target_audio.shape[0]) / fs_sound
mics_loc = np.c_[
[1, 1.9825], # mic 1
[1, 2.0175], # mic 2
] # location of microphone array
mics_loc = pra.circular_2D_array([2., 2.], 6, 0, 0.0625) # kindof compactsix
src_loc = np.r_[5, 2] # source location
noise_loc = np.r_[2.5, 4.5]
SIR = 25 # decibels
SINR = SIR - 1 # decibels
sigma_i, sigma_n = compute_variances(SIR, SINR, src_loc, noise_loc, mics_loc.mean(axis=1), sigma_s=sigma_s)
interference_audio = np.random.randn(target_audio.shape[0] + fs_sound) * sigma_i
room = pra.ShoeBox([6,5], fs=16000, max_order=12, absorption=0.4, sigma2_awgn=sigma_n**2)
room.add_source(src_loc, signal=target_audio)
room.add_source(noise_loc, signal=interference_audio)
# conventional microphone array
M = mics_loc.shape[1]
mics = pra.Beamformer(mics_loc, fs=fs_sound, N=nfft, hop=nfft // 2, zpb=nfft)
room.add_microphone_array(mics)
room.simulate()
# sound-to-light sensor
# we assume there is no propagation delay between speaker and sensor
leds = LightArray2(src_loc, fs=fs_light)
leds.record(target_audio + np.random.randn(*target_audio.shape) * sigma_n, fs=fs_sound)
leds_sig = leds.signals - leds.signals.min()
leds_sig /= leds_sig.max()
leds_time = np.arange(leds.signals.shape[0]) / fs_light
# perform VAD on the light signal
vad = leds.signals > vad_thresh
# Now compute the STFT of the microphone input
X = np.moveaxis([ pra.stft(a, nfft, nfft//2, np.fft.rfft, win=pra.hann(nfft)) for a in room.mic_array.signals ], 0, -1)
X_time = np.arange(1, X.shape[0]+1) * (nfft / 2) / fs_sound
# we need to match the VAD to sampling rate of X
vad_x = np.zeros(X_time.shape[0], dtype=bool)
v = 0
for i,t in enumerate(X_time):
if v < leds_time.shape[0] - 1 and abs(t - leds_time[v]) > abs(t - leds_time[v+1]):
v += 1
vad_x[i] = vad[v]
vad_x_comp = np.logical_not(vad_x)
# covariance matrix
Rs = np.einsum('i...j,i...k->...jk', X[vad_x,:,:], np.conj(X[vad_x,:,:])) / np.sum(vad_x)
Rn = np.einsum('i...j,i...k->...jk', X[vad_x_comp,:,:], np.conj(X[vad_x_comp,:,:])) / np.sum(vad_x_comp)
# compute the MaxSINR beamformer
w = [la.eigh(rs, b=rn, eigvals=(M-1,M-1))[1] for rs,rn in zip(Rs[1:], Rn[1:])]
w = np.squeeze(np.array(w))
w /= la.norm(w, axis=1)[:,None]
w = np.concatenate([np.ones((1,M))/np.sqrt(M), w], axis=0)
# Compute the gain
ref = X[vad_x,:,0]
#z = compute_gain(w, X[vad_x,:,:], ref, clip_up=1.0, clip_down=0.1)
z = compute_gain(w, X[vad_x,:,:], ref, clip_up=2.0)
#z = compute_gain(w, X[vad_x,:,:], ref)
#z = compute_gain(w, X[vad_x,:,:], ref)
sig_in = pra.normalize(mics.signals[0])
mics.weights = w.T
room.plot(img_order=1, freq=[800,1000,1200, 1400, 1600, 1800, 2000])
plt.title('No matching')
plt.figure()
mics.plot_beam_response()
plt.title('No matching')
sig_out_flat = mics.process()
sig_out_flat = pra.normalize(sig_out_flat)
mics.weights = (z[:,None] * w).T
sig_out_ref0 = mics.process()
sig_out_ref0 = pra.normalize(sig_out_ref0)
room.plot(img_order=1, freq=[800,1000,1200, 1400, 1600, 1800, 2000])
plt.title('Channel 1 matching')
plt.figure()
mics.plot_beam_response()
plt.title('Channel 1 matching')
plt.figure()
plt.plot(sound_time, target_audio)
plt.plot(X_time, vad_x)
plt.plot(leds_time, leds_sig, '--')
plt.legend(['sound','VAD', 'light'])
plt.figure()
mics.plot()
plt.show()