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
distance = 3. # 3 meters
#######################
# algorithms parameters
SNR = 0. # signal-to-noise ratio
c = 343. # speed of sound
fs = 16000 # sampling frequency
nfft = 256 # FFT size
freq_bins = np.arange(5, 60) # FFT bins to use for estimation
# 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([
start_time = time.time()
hop = frame_len // 2
while noisy_signal.shape[0] - n >= hop:
processed_audio[n:n + hop, ] = scnr.apply(noisy_signal[n:n + hop])
# update step
n += hop
proc_time = time.time() - start_time
print("{} minutes".format((proc_time/60)))
# save to output file
enhanced_signal_fp = os.path.join(os.path.dirname(__file__), 'output_samples',
'denoise_output_Subspace.wav')
wavfile.write(enhanced_signal_fp, fs,
pra.normalize(processed_audio).astype(np.float32))
"""
Plot spectrogram
"""
print("Noisy and denoised file written to: '%s'" %
os.path.join(os.path.dirname(__file__), 'output_samples'))
signal_norm = signal / np.abs(signal).max()
if plot_spec:
min_val = -80
max_val = -40
plt.figure()
plt.subplot(3, 1, 1)
plt.specgram(noisy_signal[:n-hop], NFFT=256, Fs=fs,
def get_rir(self, mic, visibility, Fs, t0=0., t_max=None):
'''
Compute the room impulse response between the source
and the microphone whose position is given as an
argument.
'''
# fractional delay length
fdl = constants.get('frac_delay_length')
fdl2 = (fdl-1) // 2
# compute the distance
dist = self.distance(mic)
time = dist / constants.get('c') + t0
alpha = self.damping / (4.*np.pi*dist)
# the number of samples needed
if t_max is None:
# we give a little bit of time to the sinc to decay anyway
N = np.ceil((1.05*time.max() - t0) * Fs)
else:
N = np.ceil((t_max - t0) * Fs)
N += fdl
def convergence_callback(Y, **kwargs):
global SDR, SIR, ref
from mir_eval.separation import bss_eval_sources
if Y.shape[2] == 1:
y = pra.transform.synthesis(
Y[:, :, 0], framesize, framesize // 2, win=win_s
)[:, None]
else:
y = pra.transform.synthesis(Y, framesize, framesize // 2, win=win_s)
if args.algo != "blinkiva":
new_ord = np.argsort(np.std(y, axis=0))[::-1]
y = y[:, new_ord]
m = np.minimum(y.shape[0] - framesize // 2, ref.shape[1])
sdr, sir, sar, perm = bss_eval_sources(
ref[:n_sources_target, :m, 0],
y[framesize // 2 : m + framesize // 2, :n_sources_target].T,
)
SDR.append(sdr)
SIR.append(sir)
def convergence_callback(Y, n_targets, SDR, SIR, ref, framesize, win_s, algo_name):
from mir_eval.separation import bss_eval_sources
if Y.shape[2] == 1:
y = pra.transform.synthesis(
Y[:, :, 0], framesize, framesize // 2, win=win_s
)[:, None]
else:
y = pra.transform.synthesis(Y, framesize, framesize // 2, win=win_s)
if algo_name not in parameters["overdet_algos"]:
new_ord = np.argsort(np.std(y, axis=0))[::-1]
y = y[:, new_ord]
m = np.minimum(y.shape[0] - framesize // 2, ref.shape[1])
synth[:n_targets, :m, 0] = y[framesize // 2 : m + framesize // 2, :n_targets].T
sdr, sir, sar, perm = bss_eval_sources(
ref[:n_targets+1, :m, 0], synth[:, :m, 0]
)
SDR.append(sdr[:n_targets].tolist())
SIR.append(sir[:n_targets].tolist())
step_size=ogive_mu,
update=ogive_update,
proj_back=True,
init_eig=(args.init == init_choices[1]),
callback=convergence_callback,
)
else:
raise ValueError("No such algorithm {}".format(args.algo))
toc = time.perf_counter()
print("Processing time: {} s".format(toc - tic))
# Run iSTFT
if Y.shape[2] == 1:
y = pra.transform.synthesis(Y[:, :, 0], framesize, framesize // 2, win=win_s)[
:, None
]
y = y.astype(np.float64)
else:
y = pra.transform.synthesis(Y, framesize, framesize // 2, win=win_s).astype(
np.float64
)
# If some of the output are uniformly zero, just add a bit of noise to compare
for k in range(y.shape[1]):
if np.sum(np.abs(y[:, k])) < 1e-10:
y[:, k] = np.random.randn(y.shape[0]) * 1e-10
# For conventional methods of BSS, reorder the signals by decreasing power
if args.algo != "blinkiva":
new_ord = np.argsort(np.std(y, axis=0))[::-1]
def convergence_callback(Y, n_targets, SDR, SIR, ref, framesize, win_s, algo_name):
from mir_eval.separation import bss_eval_sources
if Y.shape[2] == 1:
y = pra.transform.synthesis(
Y[:, :, 0], framesize, framesize // 2, win=win_s
)[:, None]
else:
y = pra.transform.synthesis(Y, framesize, framesize // 2, win=win_s)
if algo_name not in parameters["overdet_algos"]:
new_ord = np.argsort(np.std(y, axis=0))[::-1]
y = y[:, new_ord]
m = np.minimum(y.shape[0] - framesize // 2, ref.shape[1])
synth[:n_targets, :m, 0] = y[framesize // 2 : m + framesize // 2, :n_targets].T
sdr, sir, sar, perm = bss_eval_sources(
ref[:n_targets+1, :m, 0], synth[:, :m, 0]
)
help='Creates a small GUI for easy playback of the sound samples')
parser.add_argument('--save', action='store_true',
help='Saves the output of the separation to wav files')
args = parser.parse_args()
if args.gui:
# avoids a bug with tkinter and matplotlib
import matplotlib
matplotlib.use('TkAgg')
import pyroomacoustics as pra
## Prepare one-shot STFT
L = args.block
hop = L // 2
win_a = pra.hann(L)
win_s = pra.transform.compute_synthesis_window(win_a, hop)
## Create a room with sources and mics
# Room dimensions in meters
room_dim = [8, 9]
# source location
source = np.array([1, 4.5])
room = pra.ShoeBox(
room_dim,
fs=16000,
max_order=15,
absorption=0.35,
sigma2_awgn=1e-8)
# get signals
# fix the randomness for repeatability
np.random.seed(10)
# set the source powers, the first one is half
source_std = np.ones(n_sources_target)
source_std[0] /= np.sqrt(2.0)
SIR = 10 # dB
SNR = (
60
) # dB, this is the SNR with respect to a single target source and microphone self-noise
# STFT parameters
framesize = 4096
win_a = pra.hann(framesize)
win_s = pra.transform.compute_synthesis_window(win_a, framesize // 2)
# algorithm parameters
n_iter = args.n_iter
# param ogive
ogive_mu = 0.1
ogive_update = "switching"
ogive_iter = 2000
# Geometry of the room and location of sources and microphones
room_dim = np.array([10, 7.5, 3])
mic_locs = semi_circle_layout(
[4.1, 3.76, 1.2], np.pi, 0.04, n_mics, rot=np.pi / 2.0 * 0.99
)