Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# compute the noise variance
sigma2 = 10**(-SNR / 10) / (4. * np.pi * distance)**2
# Create an anechoic room
room_dim = np.r_[10.,10.]
aroom = pra.ShoeBox(room_dim, fs=fs, max_order=0, sigma2_awgn=sigma2)
# add the source
source_location = room_dim / 2 + distance * np.r_[np.cos(azimuth), np.sin(azimuth)]
source_signal = np.random.randn((nfft // 2 + 1) * nfft)
aroom.add_source(source_location, signal=source_signal)
# We use a circular array with radius 15 cm # and 12 microphones
R = pra.circular_2D_array(room_dim / 2, 12, 0., 0.15)
aroom.add_microphone_array(pra.MicrophoneArray(R, fs=aroom.fs))
# run the simulation
aroom.simulate()
################################
# Compute the STFT frames needed
X = np.array([
pra.stft(signal, nfft, nfft // 2, transform=np.fft.rfft).T
for signal in aroom.mic_array.signals ])
##############################################
# Now we can test all the algorithms available
algo_names = sorted(pra.doa.algorithms.keys())
for algo_name in algo_names:
# Construct the new DOA object
src_loc = 0.66 * np.array(room_dim)
# we just do this to avoid some probability zero
# placements with special artefacts
mic_loc += 0.0005 * np.random.randn(len(room_dim))
src_loc += 0.0005 * np.random.randn(len(room_dim))
# Create the room and place equipment in it
room = pra.ShoeBox(
room_dim,
fs=16000,
rt60=rt60,
)
room.add_source(src_loc)
room.add_microphone_array(
pra.MicrophoneArray(
np.c_[ mic_loc, ],
room.fs,
)
)
# Simulate and analyze
room.compute_rir()
rt60_analysis(room, 0, 0, rt60_tgt=rt60)
n_sources,
f"{samples_dir}/metadata.json",
gender_balanced=True,
seed=3,
)[0]
signals = wav_read_center(wav_files, seed=123)
# Create the room itself
room = pra.ShoeBox(room_dim, fs=fs, absorption=absorption, max_order=max_order)
# Place a source of white noise playing for 5 s
for sig, loc in zip(signals, source_locs.T):
room.add_source(loc, signal=sig)
# Place the microphone array
room.add_microphone_array(pra.MicrophoneArray(mic_locs, fs=room.fs))
# compute RIRs
room.compute_rir()
# define a callback that will do the signal mix to
# get a the correct SNR and SIR
callback_mix_kwargs = {
"snr": SNR,
"sir": SIR,
"n_src": n_sources,
"n_tgt": n_sources_target,
"src_std": source_std,
"ref_mic": 0,
}
def callback_mix(
)
room = pra.Room(
shoebox.walls,
fs=fs,
t0=t0,
max_order=max_order_sim,
sigma2_awgn=sigma2_n
)
source_loc = [2, 3.5, 2]
mic_loc = np.array([[2, 1.5, 2]]).T
shoebox.addSource(source_loc)
room.addSource(source_loc)
room.addMicrophoneArray(pra.MicrophoneArray(mic_loc, fs))
shoebox.addMicrophoneArray(pra.MicrophoneArray(mic_loc, fs))
then = time.time()
shoebox.image_source_model(use_libroom=True)
shoebox.compute_RIR()
shoebox_exec_time = time.time() - then
#then = time.time()
#room.image_source_model(use_libroom=True)
#room.compute_RIR()
room_exec_time = time.time() - then
print("Time spent (room):", room_exec_time)
print("Time spent (shoebox):", shoebox_exec_time)
else:
raise ValueError("The size parameter can only take values ['small', 'medium', 'large']")
pol = size_coef * np.array([[0,0], [0,4], [3,2], [3,0]]).T
room = pra.Room.from_corners(pol, fs=32000, max_order=2, absorption=absor, ray_tracing=True)
# Create the 3D room by extruding the 2D by a specific height
room.extrude(size_coef * 2.5, absorption=absor)
# Adding the source
room.add_source(size_coef * np.array([1.8, 0.4, 1.6]), signal=audio_anechoic)
# Adding the microphone
R = size_coef * np.array([[0.5],[1.2],[0.5]])
room.add_microphone_array(pra.MicrophoneArray(R, room.fs))
# Compute the RIR using the hybrid method
s = time.perf_counter()
room.image_source_model()
room.ray_tracing()
room.compute_rir()
print("Computation time:", time.perf_counter() - s)
# Plot and apply the RIR on the audio file
room.plot_rir()
plt.show()
return room.rir[0][0], room
mic_locs = np.vstack(
(
pra.circular_2D_array([4.1, 3.76], n_mics, np.pi / 2, 0.02),
1.2 * np.ones((1, n_mics)),
)
)
all_locs = np.concatenate((mic_locs, blinky_locs), axis=1)
# Create the room itself
room = pra.ShoeBox(room_dim[:2])
for loc in source_locs.T:
room.add_source(loc[:2])
# Place the microphone array
room.add_microphone_array(pra.MicrophoneArray(all_locs[:2, :], fs=room.fs))
room.plot(img_order=0)
plt.xlim([-0.1, room_dim[0] + 0.1])
plt.ylim([-0.1, room_dim[1] + 0.1])
plt.savefig(filename)
# mic location
mic_height = self.mic_height_distrib.sample()
assert mic_height > self.mic_min_dist_wall
assert mic_height < room_dim[2] - self.mic_min_dist_wall
mic_loc = sample_mic_location(room_dim,
self.mic_min_dist_wall)
mic_loc.append(mic_height)
# create Room object
R = np.array(mic_loc, ndmin=2).T
room = ShoeBox(p=room_dim,
fs=self.sample_rate,
materials=materials,
max_order=self.ism_order,
mics=pra.MicrophoneArray(R,
self.sample_rate),
air_absorption=self.air_absorption,
ray_tracing=self.ray_tracing,
)
# sample target location
target_orientation = \
self.target_orientation_distrib.sample()
target_dist = self.target_mic_dist_distrib.sample()
target_loc = mic_loc + \
spher2cart(r=target_dist,
azimuth=target_orientation[0],
colatitude=target_orientation[1]
)
# make sure inside room and meets constraint
y_source, z_source, x_mic, y_mic, z_mic]
if room_setting not in room_settings:
temp += 1
room_settings.append(room_setting)
max_order = 100
# set max_order to a low value for a quick (but less accurate) RIR
room = pra.ShoeBox(room_dim, fs=fs, max_order=max_order, absorption=0.2)
# add source and set the signal to WAV file content
room.add_source(source, signal=signal)
# add two-microphone array
room.add_microphone_array(pra.MicrophoneArray(microphone, room.fs))
# compute image sources
room.image_source_model(use_libroom=True)
room.compute_rir()
rir = room.rir[0][0]
# save the room reverberations
wavfile.write(name_sub + "_rir_" + str(temp) + '.wav', 16000, rir)
with open('room_setting.data', 'wb') as f:
pickle.dump(room_settings, f)
noise *= alpha
# play noise at a random location within the room
noise_source = [self.random(0, depth),
self.random(0, width),
self.random(0, height)]
room.add_source(noise_source,
signal=noise.squeeze(),
delay=0.)
# place the microphone at a random location within the room
microphone = [self.random(0, depth),
self.random(0, width),
self.random(0, height)]
room.add_microphone_array(
pra.MicrophoneArray(np.c_[microphone, microphone], sample_rate))
# create the Room Impulse Response (RIR)
room.compute_rir()
# simulate sound propagation
room.simulate()
return room.mic_array.signals[0,:n_samples, np.newaxis]