def createroom(mic_p, mic_d, sour_p, sour_d, callback_mix, roomdim, absorption, max_order, n_mics, angle): np.random.seed(10) # STFT parameters framesize = 4096 win_a = pra.hann(framesize) win_s = pra.transform.compute_synthesis_window(win_a, framesize // 2) # algorithm parameters # param ogive ogive_mu = 0.1 ogive_update = "switching" ogive_iter = 2000 SIR = 10 # dB SNR = ( 60 ) # dB, this is the SNR with respect to a single target source and microphone self-noise ########separation params############# algo = algo_choices[0] no_cb = True save = True n_iter = 60 dist = "gauss" #guass or laplace ########paramas set################## fs = 44100 n_sources = 2 n_mics = n_mics n_sources_target = 2 assert n_sources_target <= n_mics, "More sources than microphones is not supported" # set the source powers, the first one is half source_std = np.ones(n_sources_target) # room size room_dim = roomdim # micro position rot = angle offset = np.pi - rot / 2 mic_locs = semi_circle_layout(mic_p, rot, mic_d, n_mics, rot=offset) ###micro2 # target position target_locs = np.transpose([[7, 10, 6], [9, 16, 6]]) #interference position interferer_locs = random_layout([14, 0, 6], n_sources - n_sources_target, offset=[5, 20, 3], seed=1) source_locs = target_locs # audio loaded wav_files = [amBird, saBird] signals = wav_read_center(wav_files, seed=123) #create room room = pra.ShoeBox(room_dim, fs=44100, absorption=absorption, max_order=max_order, air_absorption=True, humidity=50) # add source for sig, loc in zip(signals, source_locs.T): room.add_source(loc, signal=sig) # add micro room.add_microphone_array(pra.MicrophoneArray(mic_locs, fs=room.fs)) callback_mix_kwargs = { "snr": SNR, "sir": SIR, "n_src": n_sources, "n_tgt": n_sources_target, "src_std": source_std, "ref_mic": 0, } # Run the simulation separate_recordings = room.simulate( callback_mix=callback_mix, callback_mix_kwargs=callback_mix_kwargs, return_premix=True, ) mics_signals = room.mic_array.signals print("Simulation done.") # rt60 = room.measure_rt60() # print(rt60) # Monitor Convergence ref = np.moveaxis(separate_recordings, 1, 2) if ref.shape[0] < n_mics: ref = np.concatenate( (ref, np.random.randn(n_mics - ref.shape[0], ref.shape[1], ref.shape[2])), axis=0, ) SDR, SIR, cost_func = [], [], [] convergence_callback = None # START BSS # shape: (n_frames, n_freq, n_mics) X_all = pra.transform.analysis(mics_signals.T, framesize, framesize // 2, win=win_a).astype(np.complex128) X_mics = X_all[:, :, :n_mics] tic = time.perf_counter() # Run BSS if algo == "auxiva": # Run AuxIVA Y = overiva( X_mics, n_iter=n_iter, proj_back=True, model=dist, callback=convergence_callback, ) elif algo == "auxiva_pca": # Run AuxIVA Y = auxiva_pca( X_mics, n_src=n_sources_target, n_iter=n_iter, proj_back=True, model=dist, callback=convergence_callback, ) elif algo == "overiva": # Run AuxIVA Y = overiva( X_mics, n_src=n_sources_target, n_iter=n_iter, proj_back=True, model=dist, init_eig=(init == init_choices[1]), callback=convergence_callback, ) elif algo == "ilrma": # Run AuxIVA Y = pra.bss.ilrma( X_mics, n_iter=n_iter, n_components=2, proj_back=True, callback=convergence_callback, ) elif algo == "ogive": # Run OGIVE Y = ogive( X_mics, n_iter=ogive_iter, step_size=ogive_mu, update=ogive_update, proj_back=True, model=dist, init_eig=(init == init_choices[1]), callback=convergence_callback, ) elif algo == "ogive_matlab": # Run OGIVE Y = ogive_matlab_wrapper( X_mics, n_iter=ogive_iter, step_size=ogive_mu, update=ogive_update, proj_back=True, init_eig=(init == init_choices[1]), callback=convergence_callback, ) else: raise ValueError("No such algorithm {}".format(algo)) toc = time.perf_counter() # 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 algo != "blinkiva": new_ord = np.argsort(np.std(y, axis=0))[::-1] y = y[:, new_ord] # Compare SIR 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, ) # reorder the vector of reconstructed signals y_hat = y[:, perm] print("SDR:", sdr) print("SIR:", sir) ####save mix and separation ####### if save: from scipy.io import wavfile wavfile.write( "birdmix.wav", room.fs, (pra.normalize(mics_signals, bits=16).astype(np.int16).T)[:, 0], ) for i, sig in enumerate(y_hat.T): wavfile.write( "birdsep{}.wav".format(i + 1), room.fs, pra.normalize(sig, bits=16).astype(np.int16).T, )
def createroom(amBird, saBird, noises, mic_p, mic_d, sour_p, sour_d, callback_mix, roomdim, absorption, max_order, n_mics, angle): ###########IVA params set################ np.random.seed(10) # STFT parameters framesize = 4096 win_a = pra.hann(framesize) win_s = pra.transform.compute_synthesis_window( win_a, framesize // 2) # algorithm parameters # param ogive ogive_mu = 0.1 ogive_update = "switching" ogive_iter = 2000 # dB, this is the SNR with respect to a single target source and microphone self-noise ########separation params################## algo = algo_choices[0] no_cb = True save = True n_iter = 60 dist = "gauss" # guass or laplace ########paramas set################## fs = 44100 snr=60 sinr=10 n_sources = 2+3 n_mics = n_mics n_sources_target = 2 assert n_sources_target <= n_mics, "More sources than microphones is not supported" # set the source powers, the first one is half source_std = np.ones(n_sources_target) # position # room size room_dim = roomdim # micro position rot = angle offset = np.pi-rot/2 mic_locs = semi_circle_layout( mic_p, rot, mic_d, n_mics, rot=offset) # micro2 # target position target_locs = np.transpose([[7, 10, 6], [9, 16, 6]]) #interfere position interferer_locs = random_layout([16, 2, 6], 3, offset=[5, 18, 3], seed=1) source_locs = np.concatenate((target_locs, interferer_locs), axis=1) # source_locs = target_locs # audio input wav_files = [amBird, saBird, noises[0],noises[1],noises[2]] signals = wav_read_center(wav_files, seed=123) # create room room = pra.ShoeBox(room_dim, fs=44100, absorption=absorption, max_order=max_order, air_absorption=True, humidity=50) # add source for sig, loc in zip(signals, source_locs.T): room.add_source(loc, signal=sig) # add micro room.add_microphone_array( pra.MicrophoneArray(mic_locs, fs=room.fs)) # # draw # x = mic_locs[:2][0] # y = mic_locs[:2][1] # import matplotlib.pyplot as plt # plt.scatter(x,y) # plt.axis('equal') # plt.xlim([0,20]) # plt.ylim([0,20]) # x1 = source_locs[:2][0] # y1 = source_locs[:2][1] # plt.scatter(x1,y1) # plt.xlim([0,20]) # plt.ylim([0,20]) # plt.axis('equal') # x1 = interferer_locs[:2][0] # y1 = interferer_locs[:2][1] # plt.scatter(x1,y1) # plt.xlim([0,20]) # plt.ylim([0,20]) # plt.axis('equal') # plt.show() # callback_mix_kwargs = { # "snr": SNR, # "sir": SIR, # "n_src": n_sources, # "n_tgt": n_sources_target, # "src_std": source_std, # "ref_mic": 0, # } # power set premix = room.simulate(return_premix=True) n_samples = premix.shape[2] # Normalize the signals so that they all have unit variance at the reference microphone ref_mic=0 p_mic_ref = np.std(premix[:, ref_mic, :], axis=1) premix /= p_mic_ref[:, None, None] sources_var = np.ones(n_sources_target) # # scale to pre-defined variance premix[:n_sources_target, :, :] *= np.sqrt(sources_var[:, None, None]) # compute noise variance sigma_n = np.sqrt(10 ** (-snr / 10) * np.sum(sources_var)) # now compute the power of interference signal needed to achieve desired SINR sigma_i = np.sqrt( np.maximum(0, 10 ** (-sinr / 10) * np.sum(sources_var) - sigma_n ** 2) / (n_sources-n_sources_target) ) premix[n_sources_target:, :, :] *= sigma_i background = ( np.sum(premix[n_sources_target:, :, :], axis=0) ) # Mix down the recorded signals mix = np.sum(premix, axis=0) mics_signals = room.mic_array.signals print("Simulation done.") # Monitor Convergence ref = np.zeros( (n_sources_target+1, premix.shape[2], premix.shape[1]), dtype=premix.dtype) ref[:n_sources_target, :, :] = premix[:n_sources_target, :, :].swapaxes(1, 2) ref[n_sources_target, :, :] = background.T convergence_callback = None # START BSS X_all = pra.transform.analysis( mix.T, framesize, framesize // 2, win=win_a ).astype(np.complex128) X_mics = X_all[:, :, :n_mics] # Run BSS if algo == "auxiva": # Run AuxIVA Y = overiva( X_mics, n_iter=n_iter, proj_back=True, model=dist, callback=convergence_callback, ) elif algo == "auxiva_pca": # Run AuxIVA Y = auxiva_pca( X_mics, n_src=n_sources_target, n_iter=n_iter, proj_back=True, model=dist, callback=convergence_callback, ) elif algo == "overiva": # Run AuxIVA Y = overiva( X_mics, n_src=n_sources_target, n_iter=n_iter, proj_back=True, model=dist, init_eig=(init == init_choices[1]), callback=convergence_callback, ) elif algo == "ilrma": # Run AuxIVA Y = pra.bss.ilrma( X_mics, n_iter=n_iter, n_components=2, proj_back=True, callback=convergence_callback, ) elif algo == "ogive": # Run OGIVE Y = ogive( X_mics, n_iter=ogive_iter, step_size=ogive_mu, update=ogive_update, proj_back=True, model=dist, init_eig=(init == init_choices[1]), callback=convergence_callback, ) elif algo == "ogive_matlab": # Run OGIVE Y = ogive_matlab_wrapper( X_mics, n_iter=ogive_iter, step_size=ogive_mu, update=ogive_update, proj_back=True, init_eig=(init == init_choices[1]), callback=convergence_callback, ) else: raise ValueError("No such algorithm {}".format(algo)) # 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 algo != "blinkiva": new_ord = np.argsort(np.std(y, axis=0))[::-1] y = y[:, new_ord] # Compare SIR 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, ) # reorder the vector of reconstructed signals y_hat = y[:, perm] SDRList.append(sdr) SIRList.append(sir) print("SDR:", sdr) print("SIR:", sir) ####save mix and separation ####### if save: from scipy.io import wavfile wavfile.write( "birdmix.wav", room.fs, (pra.normalize(mics_signals, bits=16).astype( np.int16).T)[:, 0], ) for i, sig in enumerate(y_hat.T): wavfile.write( "birdsep{}.wav".format(i + 1), room.fs, pra.normalize(sig, bits=16).astype(np.int16).T, )
X_mics, n_iter=ogive_iter, step_size=ogive_mu, update=ogive_update, proj_back=True, model=args.dist, init_eig=(args.init == init_choices[1]), callback=convergence_callback, ) elif args.algo == "ogive_matlab": # Run OGIVE Y = ogive_matlab_wrapper( X_mics, n_iter=ogive_iter, 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,
def line_createroom(Bird1,Bird2,Bird3,callback_mix): roomdim = np.array([20, 20, 10]) max_order = 17 absorption = 0.9 mic_p = [13, 10, 3.5] #mic_center_point mic_d = 0.015 #mic_dinstance sour_p = [7,10,6] #source_postion sour_d = 5 #source_distance n_mics = 4 #mic_number n_sources = 3 mic_rot = np.pi/2 bird_rot = np.pi/2 ### params setting ### np.random.seed(10) # STFT parameters framesize = 4096 win_a = pra.hann(framesize) win_s = pra.transform.compute_synthesis_window( win_a, framesize // 2) ogive_mu = 0.1 ogive_update = "switching" ogive_iter = 2000 SIR = 10 # dB SNR = (60) algo = algo_choices[0] no_cb = True save = True n_iter = 60 dist = "gauss" # laplace fs = 44100 n_sources_target = 3 assert n_sources_target <= n_mics, "More sources than microphones is not supported" source_std = np.ones(n_sources_target) # room size room_dim = roomdim # micro position mic_locs = semi_line_layout(mic_p,mic_rot,mic_d,n_mics) # target position source_locs = semi_line_layout(sour_p,bird_rot,sour_d,n_sources) source_locs[0][0],source_locs[0][2] = source_locs[0][0]+0,source_locs[0][2]+0##push value # target_locs = np.transpose([[7, 10, 6], [9, 16, 6]]) # audio loaded wav_files = [Bird1,Bird2,Bird3] signals = wav_read_center(wav_files, seed=123) #create room room = pra.ShoeBox(room_dim, fs=44100, absorption=absorption, max_order=max_order, air_absorption=True, humidity=50) # add source for sig, loc in zip(signals, source_locs.T): room.add_source(loc, signal=sig) # add micro room.add_microphone_array( pra.MicrophoneArray(mic_locs, fs=room.fs)) callback_mix_kwargs = { "snr": SNR, "sir": SIR, "n_src": n_sources, "n_tgt": n_sources_target, "src_std": source_std, "ref_mic": 0, } # # draw # x = mic_locs[:2][0] # y = mic_locs[:2][1] # import matplotlib.pyplot as plt # plt.scatter(x,y) # plt.axis('equal') # plt.xlim([0,20]) # plt.ylim([0,20]) # x1 = source_locs[:2][0] # y1 = source_locs[:2][1] # plt.scatter(x1,y1) # plt.xlim([0,20]) # plt.ylim([0,20]) # plt.axis('equal') # plt.show() # Run the simulation separate_recordings = room.simulate( callback_mix=callback_mix, callback_mix_kwargs=callback_mix_kwargs, return_premix=True, ) mics_signals = room.mic_array.signals print("line Simulation done.") # Monitor Convergence ref = np.moveaxis(separate_recordings, 1, 2) if ref.shape[0] < n_mics: ref = np.concatenate( (ref, np.random.randn(n_mics - ref.shape[0], ref.shape[1], ref.shape[2])), axis=0, ) convergence_callback = None X_all = pra.transform.analysis( mics_signals.T, framesize, framesize // 2, win=win_a ).astype(np.complex128) X_mics = X_all[:, :, :n_mics] tic = time.perf_counter() if algo == "auxiva": # Run AuxIVA Y = overiva( X_mics, n_iter=n_iter, proj_back=True, model=dist, callback=convergence_callback, ) elif algo == "auxiva_pca": # Run AuxIVA Y = auxiva_pca( X_mics, n_src=n_sources_target, n_iter=n_iter, proj_back=True, model=dist, callback=convergence_callback, ) elif algo == "overiva": # Run AuxIVA Y = overiva( X_mics, n_src=n_sources_target, n_iter=n_iter, proj_back=True, model=dist, init_eig=(init == init_choices[1]), callback=convergence_callback, ) elif algo == "ilrma": # Run AuxIVA Y = pra.bss.ilrma( X_mics, n_iter=n_iter, n_components=2, proj_back=True, callback=convergence_callback, ) elif algo == "ogive": # Run OGIVE Y = ogive( X_mics, n_iter=ogive_iter, step_size=ogive_mu, update=ogive_update, proj_back=True, model=dist, init_eig=(init == init_choices[1]), callback=convergence_callback, ) elif algo == "ogive_matlab": # Run OGIVE Y = ogive_matlab_wrapper( X_mics, n_iter=ogive_iter, step_size=ogive_mu, update=ogive_update, proj_back=True, init_eig=(init == init_choices[1]), callback=convergence_callback, ) else: raise ValueError("No such algorithm {}".format(algo)) # 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 algo != "blinkiva": new_ord = np.argsort(np.std(y, axis=0))[::-1] y = y[:, new_ord] # Compare SIR 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, ) # reorder the vector of reconstructed signals y_hat = y[:, perm] #return mixdata = pra.normalize(mics_signals, bits=16).astype(np.int16).T separationdata = [] for sig in y_hat.T: separationdata.append(pra.normalize(sig, bits=16).astype(np.int16).T) print("sdr",sdr) return sdr,sir,mixdata,separationdata #wavefile(mixdata) wavefile(separationdata[0]) wavefile(separationdata[1])