def save_main_beam(self, rs_x, rs_y, sp_x, sp_y, is_circular, mic_num, c_x, c_y, i_m_d, angle, room_frequency, freques, room_temp=None, room_humidity=None, is_airAbsorption=None): # Create a rs_x by rs_y metres shoe box room room = pra.ShoeBox([rs_x, rs_y], fs=room_frequency, temperature=room_temp, humidity=room_humidity, air_absorption=is_airAbsorption) # Add a source somewhere in the room room.add_source([sp_x, sp_y]) # Create a linear array beamformer with 4 microphones # with angle 0 degrees and inter mic distance 10 cm if (is_circular): R = pra.circular_2D_array([c_x, c_y], mic_num, angle, i_m_d) else: R = pra.linear_2D_array([c_x, c_y], mic_num, angle, i_m_d) room.add_microphone_array(pra.Beamformer(R, room.fs)) # Now compute the delay and sum weights for the beamformer room.mic_array.rake_delay_and_sum_weights(room.sources[0][:1]) # plot the room and resulting beamformer room.plot(freq=freques, img_order=0) # plt.show() plt.savefig('./fig.png')
def beamformed_das(comb, people_num, sr=16000): f1 = comb[0] f2 = comb[1] # def beamformed_das(f1, f2, people_num, sr=16000): f1_data = f1['data'] f2_data = f2['data'] signal_len = len(f1['data']) distance = 1.5 # azimuth = np.array([math.atan2(1.5, 0.5), math.atan2(1.5, -0.5)]) azimuth = np.array([ 90., 270., ]) * np.pi / 180 # centre = [2, 1.5] room_dim = np.r_[4, 6] room = pra.ShoeBox(room_dim, fs=sr) echo = pra.linear_2D_array(center=(room_dim / 2), M=5, phi=0, d=0.5) echo = np.concatenate((echo, np.array((room_dim / 2), ndmin=2).T), axis=1) mics = pra.Beamformer(echo, room.fs) room.add_microphone_array(mics) # room.add_source(np.array([1.5, 4.5]), delay=0., signal=f1_data) # room.add_source(np.array([2.5, 4.5]), delay=0., signal=f2_data[:len(f1_data)]) signals = [f1_data, f2_data] for i, ang in enumerate(azimuth): source_location = room_dim / 2 + distance * np.r_[np.cos(ang), np.sin(ang)] source_signal = signals[i] room.add_source(source_location, signal=source_signal[:signal_len], delay=0) mics.rake_delay_and_sum_weights(room.sources[0][:1]) # room.plot(freq=[300, 400, 500, 1000, 2000, 4000], img_order=0) # plt.show() # ax.legend(['300', '400', '500', '1000', '2000', '4000']) # fig.set_size_inches(20, 8) room.compute_rir() room.simulate() filename = 'beamformeded_%05d-%05d' % (f1['filename'], f2['filename']) + '.wav' with open(TXT_PATH + 'build_beamformeded.txt', 'a') as f: f.write(filename) f.write('\n') for i in range(5): wavfile.write(MICS_PATH + 'mic%d/' % (i + 1) + filename, sr, room.mic_array.signals[i, :])
def _move_agent(self, new_agent_loc, initial_placing=False): """ This function moves the agent to a new location (given by new_agent_loc). It effectively removes the agent (mic array) from the room and then adds it back in the new location. If initial_placing == True, the agent is placed in the room for the first time. Args: new_agent_loc (List[int] or np.array or None): [x,y] coordinates of the agent's new location. initial_placing (bool): True if initially placing the agent in the room at the beginning of the episode """ # Placing agent in room for the first time (likely at the beginning of a new episode, after a reset) if initial_placing: if new_agent_loc is None: loc = self._sample_points(1, sources=False, agent=True) print("Placing agent at {}".format(loc)) self.agent_loc = loc self.cur_angle = 0 # Reset the orientation of agent back to zero at start of an ep else: self.agent_loc = new_agent_loc.copy() print("Placing agent at {}".format(self.agent_loc)) self.cur_angle = 0 else: # Set the new agent location (where to move) self.agent_loc = new_agent_loc # Setup microphone in agent location, delete the array at previous time step self.room.mic_array = None if self.num_channels == 2: # Create the array at current time step (2 mics, angle IN RADIANS, 0.2m apart) mic = MicrophoneArray( linear_2D_array(self.agent_loc, 2, self.cur_angle, constants.DIST_BTWN_EARS), self.room.fs) self.room.add_microphone_array(mic) else: mic = MicrophoneArray(self.agent_loc.reshape(-1, 1), self.room.fs) self.room.add_microphone_array(mic)
""" This example shows how to create delay and sum beamformers """ from __future__ import print_function, division import numpy as np import matplotlib.pyplot as plt import pyroomacoustics as pra # Create a 4 by 6 metres shoe box room room = pra.ShoeBox([4, 6]) # Add a source somewhere in the room room.add_source([2.5, 4.5]) # Create a linear array beamformer with 4 microphones # with angle 0 degrees and inter mic distance 10 cm R = pra.linear_2D_array([2, 1.5], 4, 0, 0.04) room.add_microphone_array(pra.Beamformer(R, room.fs)) # Now compute the delay and sum weights for the beamformer room.mic_array.rake_delay_and_sum_weights(room.sources[0][:1]) # plot the room and resulting beamformer room.plot(freq=[1000, 2000, 4000, 8000], img_order=0) plt.show()
number_mics = 6 # creation of the mic_array in a special way such that we can use beamforming # shape of the array shape = 'Circular' #position of the center mic mic = np.array([2, 1.5]) # radius of the array d = 5 # the angle from horizontal phi = 0. # creation of the array if shape is 'Circular': R = pra.circular_2D_array(mic, number_mics, phi, radius=d) else: R = pra.linear_2D_array(mic, number_mics, phi, d) R = np.concatenate((R, np.array(mic, ndmin=2).T), axis=1) # FFT length N = 1024 # desired basis word(s) (can also be a list) desired_word = 'yes' #choose your label file labels_file = "conv_labels.txt" # choose your graph file graph_file = "my_frozen_graph.pb" # destination directory to write your new samples dest_dir = 'output_final_beamforming' if not os.path.exists(dest_dir): os.makedirs(dest_dir) '''
orientation=DirectionVector(azimuth=45, colatitude=90, degrees=True), pattern_enum=DirectivityPattern.CARDIOID, ) # create room room = pra.ShoeBox( p=[7, 7, 3], materials=pra.Material(0.4), fs=16000, max_order=40, ) # add source room.add_source([1, 1, 1.7], directivity=source_dir) # add linear microphone array M = 2 R = pra.linear_2D_array(center=[5, 5], M=M, phi=0, d=0.7) R = np.concatenate((R, np.ones((1, M)))) room.add_microphone_array(R, directivity=[dir_1, dir_2]) # plot room fig, ax = room.plot() ax.set_xlim([-1, 8]) ax.set_ylim([-1, 8]) ax.set_zlim([-1, 4]) # plot room.plot_rir() plt.show()
def srp_phat(s, fs, nFFT=None, center=None, d=None, azimuth_estm=None, mode=None): ''' Applies Steered Power Response with phase transform algorithm Uses pyroomacoustics module Input params ------------ s: numpy array -stacked microphone array signals (NOTE: number of microphones is extracted from the size of input signal, since the input signal will be of size MxN where M is number of microphones and N is the length of the audio signal.) fs: int -Sampling frequency nfft: int -FFT size. Default 1024 center: numpy array -Defines the center of the room. Default [0,0] d: int -Distance between microphones. Default 10cm. azimuth_estm: numpy array -Candidate azimuth estimates, representing location estimates of speakers. Default expects microphone to be in the middle of a table and speakers located around it. Assumes two speakers - [60,120] mode: str -Defines the microphone setup layout. Default mode = linear. mode = linear mode = circular ''' if nFFT is None: nFFT = 1024 if center is None: center = [0, 0] if d is None: d = 0.1 if azimuth_estm is None: azimuth_estm = [60, 120] freq_bins = np.arange( 30, 330) #list of individual frequency bins used to run DoA M = s.shape[0] #number of microphones phi = 0 #assume angle between microphones is 0 (same y-axis) radius = d * M / (2 * np.pi) #define radius for circular microphone layout c = 343.0 #speed of sound #Define Microphone array layout if mode is 'circular': L = pra.circular_2D_array(center, M, phi, radius) if mode is None or 'linear': L = pra.linear_2D_array(center, M, phi, d) nSrc = len(azimuth_estm) #number of speakers #STFT s_FFT = np.array([ pra.stft(s, nFFT, nFFT // 2, transform=np.fft.rfft).T for s in s ]) #STFT for s1 and s2 #SRP doa = pra.doa.srp.SRP(L, fs, nFFT, c, max_four=4, num_src=nSrc) #perform SRP approximation #Apply SRP-PHAT doa.locate_sources(s_FFT, freq_bins=freq_bins) #PLOTTING doa.polar_plt_dirac() plt.title('SRP-PHAT') print('SRP-PHAT') print('Speakers at: ', np.sort(doa.azimuth_recon) / np.pi * 180, 'degrees') plt.show()
else: signal = corpus[j - N].data fs = corpus[j - N].fs # Add source to 3D room (set max_order to a low value for a quick, but less accurate, RIR) source_position = sources_pos[j] room = pra.Room.from_corners(corners, fs=fs, max_order=8, absorption=0.16 / T60) room.extrude(2.4) room.add_source(source_position, signal=signal) # Add microphones to 3D room mic_center = np.array([1.7, 7.0, 0.96]) R = pra.linear_2D_array(mic_center[:2], M=8, phi=0, d=0.3) # mic_center = np.array([1.8, 4.5, 1.2]) # R = pra.circular_2D_array(mic_center[:2], M=8, phi0=0, radius=0.1) R = np.concatenate((R, np.ones((1, 8)) * mic_center[2]), axis=0) mics = pra.MicrophoneArray(R, room.fs) room.add_microphone_array(mics) # Compute image sources room.image_source_model() # Simulate the propagation room.compute_rir() room.simulate(snr=SNR) print("MICROPHONES SIGNAL SHAPE FOR SOURCE", j + 1, "IN POSITION", source_position)
d = 0.08 # distance between microphones phi = 0.0 # angle from horizontal max_order_design = 1 # maximum image generation used in design shape = "Linear" # array shape Lg_t = 0.100 # Filter size in seconds Lg = np.ceil(Lg_t * Fs) # Filter size in samples delay = 0.050 # Beamformer delay in seconds # Define the FFT length N = 1024 # Create a microphone array if shape is "Circular": R = pra.circular_2D_array(mic1, M, phi, d * M / (2 * np.pi)) else: R = pra.linear_2D_array(mic1, M, phi, d) # path to samples path = os.path.dirname(__file__) # The first signal (of interest) is singing rate1, signal1 = wavfile.read(path + "/input_samples/singing_" + str(Fs) + ".wav") signal1 = np.array(signal1, dtype=float) signal1 = pra.normalize(signal1) signal1 = pra.highpass(signal1, Fs) delay1 = 0.0 # The second signal (interferer) is some german speech rate2, signal2 = wavfile.read(path + "/input_samples/german_speech_" + str(Fs) + ".wav") signal2 = np.array(signal2, dtype=float) signal2 = pra.normalize(signal2)
def beamformed_doa_plot(comb): f1_data = f1['data'] f2_data = f2['data'] # azimuth = np.array([math.atan2(1.5, 0.5), math.atan2(1.5, -0.5)]) azimuth = np.array([ 90., 270., ]) * np.pi / 180 distance = 1.5 c = 343. # speed of sound fs = 16000 # sampling frequency nfft = 256 # FFT size freq_range = [300, 400] sr = 16000 snr_db = 5. # signal-to-noise ratio # sigma2 = 10**(-snr_db / 10) / (4. * np.pi * distance)**2 # Add sources of 1 second duration rng = np.random.RandomState(23) duration_samples = int(sr) room_dim = np.r_[4., 6.] room = pra.ShoeBox(room_dim, fs=sr) echo = pra.linear_2D_array(center=(room_dim / 2), M=5, phi=0, d=0.5) room.add_microphone_array(pra.MicrophoneArray(echo, room.fs)) # R = pra.linear_2D_array([2, 1.5], 4, 0, 0.04) # source_location = room_dim / 2 + distance * np.r_[np.cos(ang), np.sin(ang)] # source_signal = rng.randn(duration_samples) # room.add_source(source_location, signal=source_signal) # room.add_source(np.array([1.5, 4.5]), delay=0., signal=f1_data) # room.add_source(np.array([2.5, 4.5]), delay=0., signal=f2_data[:len(f1_data)]) for ang in azimuth: source_location = room_dim / 2 + distance * np.r_[np.cos(ang), np.sin(ang)] source_signal = rng.randn(duration_samples) room.add_source(source_location, signal=source_signal) room.simulate() X = np.array([ pra.stft(signal, nfft, nfft // 2, transform=np.fft.rfft).T for signal in room.mic_array.signals ]) # DOA_algorithm = 'MUSIC' # spatial_resp = dict() doa = pra.doa.algorithms['MUSIC'](echo, fs, nfft, c=c, num_src=2, max_four=4) # this call here perform localization on the frames in X doa.locate_sources(X, freq_range=freq_range) spatial_resp = doa.grid.values # normalize min_val = spatial_resp.min() max_val = spatial_resp.max() spatial_resp = (spatial_resp - min_val) / (max_val - min_val) # plotting param base = 1. height = 10. true_col = [0, 0, 0] # loop through algos phi_plt = doa.grid.azimuth # plot fig = plt.figure() ax = fig.add_subplot(111, projection='polar') c_phi_plt = np.r_[phi_plt, phi_plt[0]] c_dirty_img = np.r_[spatial_resp, spatial_resp[0]] ax.plot( c_phi_plt, base + height * c_dirty_img, linewidth=3, alpha=0.55, linestyle='-', # label="spatial spectrum" ) # plt.title('MUSIC') # plot true loc # for angle in azimuth: # ax.plot([angle, angle], [base, base + height], linewidth=3, linestyle='--', # color=true_col, alpha=0.6) # K = len(azimuth) # ax.scatter(azimuth, base + height*np.ones(K), c=np.tile(true_col, # (K, 1)), s=500, alpha=0.75, marker='*', # linewidths=0, # # label='true locations' # ) plt.legend() handles, labels = ax.get_legend_handles_labels() ax.legend(handles, labels, framealpha=0.5, scatterpoints=1, loc='center right', fontsize=16, ncol=1, bbox_to_anchor=(1.6, 0.5), handletextpad=.2, columnspacing=1.7, labelspacing=0.1) ax.set_xticks(np.linspace(0, 2 * np.pi, num=12, endpoint=False)) ax.xaxis.set_label_coords(0.5, -0.11) ax.set_yticks(np.linspace(0, 1, 2)) ax.xaxis.grid(b=True, color=[0.3, 0.3, 0.3], linestyle=':') ax.yaxis.grid(b=True, color=[0.3, 0.3, 0.3], linestyle='--') ax.set_ylim([0, 1.05 * (base + height)]) plt.show()