def do_beamforming(self, mic_data): """ Beamforming using Acoular """ mg, rg, st = self.get_acoular_essentials() count = 0 #Divide audio samples as per frame rate (10fps) and do beamforming for s_time in tqdm( np.arange(self.start_time, self.end_time, self.interval)): audio_data = mic_data[:, int(s_time * self.sample_rate):int((s_time + self.interval) * self.sample_rate)] audio_data = np.transpose(audio_data) if audio_data.shape[0] == 0: continue #Acoular needs audio input through .h5 file target_file = self.outfile + '/temp.h5' if os.path.exists(target_file): os.remove(target_file) with h5py.File(target_file, 'w') as data_file: data_file.create_dataset('time_data', data=audio_data) data_file['time_data'].attrs.__setitem__( 'sample_freq', self.sample_rate) #.h5 file has issues with closing. Change 'ulimit' if not working ts = TimeSamples(name=target_file) ps = PowerSpectra(time_data=ts, block_size=128, window='Hanning', overlap='50%') bb = BeamformerEig(freq_data=ps, steer=st) pm = bb.synthetic(self.freq_query, self.octave_band) Lm = L_p(pm) if count == 0: bf_data = np.zeros( (Lm.shape[0], Lm.shape[1], len( np.arange(self.start_time, self.end_time, self.interval)))) bf_data[:, :, count] = Lm else: bf_data[:, :, count] = Lm count += 1 # remove temp.h5 file after its finished os.remove(target_file) return bf_data, rg
n=12) be3Full = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='true level', n=12) be4Full = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='true location', n=12) Lbe1Rem = L_p(be1Rem.synthetic(4000, 1)) Lbe2Rem = L_p(be2Rem.synthetic(4000, 1)) Lbe3Rem = L_p(be3Rem.synthetic(4000, 1)) Lbe4Rem = L_p(be4Rem.synthetic(4000, 1)) Lbe1Full = L_p(be1Full.synthetic(4000, 1)) Lbe2Full = L_p(be2Full.synthetic(4000, 1)) Lbe3Full = L_p(be3Full.synthetic(4000, 1)) Lbe4Full = L_p(be4Full.synthetic(4000, 1)) bm1Full = BeamformerMusic(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='classic', n=12)
bca3Full = BeamformerCapon(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='true level') bca4Full = BeamformerCapon(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='true location') Lbca1Full = L_p(bca1Full.synthetic(4000,1)) Lbca2Full = L_p(bca2Full.synthetic(4000,1)) Lbca3Full = L_p(bca3Full.synthetic(4000,1)) Lbca4Full = L_p(bca4Full.synthetic(4000,1)) be1Rem = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04, steer='classic', n=12) be2Rem = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04, steer='inverse', n=12) be3Rem = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04, steer='true level', n=12) be4Rem = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04, steer='true location', n=12) be1Full = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='classic', n=12) be2Full = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='inverse', n=12) be3Full = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='true level', n=12) be4Full = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='true location', n=12) Lbe1Rem = L_p(be1Rem.synthetic(4000,1)) Lbe2Rem = L_p(be2Rem.synthetic(4000,1)) Lbe3Rem = L_p(be3Rem.synthetic(4000,1)) Lbe4Rem = L_p(be4Rem.synthetic(4000,1)) Lbe1Full = L_p(be1Full.synthetic(4000,1)) Lbe2Full = L_p(be2Full.synthetic(4000,1)) Lbe3Full = L_p(be3Full.synthetic(4000,1)) Lbe4Full = L_p(be4Full.synthetic(4000,1)) bm1Full = BeamformerMusic(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='classic', n=12) bm2Full = BeamformerMusic(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='inverse', n=12) bm3Full = BeamformerMusic(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='true level', n=12) bm4Full = BeamformerMusic(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, steer='true location', n=12) Lbm1Full = L_p(bm1Full.synthetic(4000,1)) Lbm2Full = L_p(bm2Full.synthetic(4000,1)) Lbm3Full = L_p(bm3Full.synthetic(4000,1))
################ Elevation Azimuth Frequenzbänder Frames DL_Matrix = zeros((NPOINTS_ELE, NPOINTS_AZI, len(FREQBANDS), FRAMES)) # Wenn .npz, dann keine FRAMES, weil die in einzelne .npy-Tabellen?! # DL_Matrix = zeros((NPOINTS_ELE, NPOINTS_AZI, len(FREQBANDS))) for frame_index, frame in enumerate(range(STARTFRAME, ENDFRAME)): print('### FRAME: ', frame - STARTFRAME, ' (', frame, ') ###') for freq_index, freq in enumerate(FREQBANDS): print('FREQ =', freq) ts.start = frame * NUM ts.stop = (frame + 1) * NUM result = zeros((4, rg.shape[0], rg.shape[1])) for i in range(4): be.n = i result[i] = be.synthetic(freq, 3) maxind = argmax(result.max((1, 2))) # WARUM IMMER MAXINDEX = 3 ??? # print('Result Beamforming: Maxindex = ', maxind) Lm = L_p(result[maxind]).reshape(rg.shape).flatten() max_idx = argmax( Lm.flatten()) # position in grid with max source strength max_cartcoord = rg.gpos[:, max_idx] max_idx = argmax( Lm.flatten()) # position in grid with max source strength max_value = amax(Lm.flatten()) temp_azi = arctan2(sin(rg.phi[max_idx]), cos(rg.phi[max_idx]))
# Opt 1 maxval1 = zeros(len(FREQBANDS)) maxval2 = zeros(len(FREQBANDS)) # Opt 2 tot_maxval1 = 0 tot_maxval2 = 0 # Opt 3 tot_maxval = 0 # Befüllen von Src1_Matrix und Src2_Matrix for freq_index, freq in enumerate(FREQBANDS): be.n = -1 #Eigenwerte der Größe nach sortiert -> größter Eigenwert (default) Lm = L_p(be.synthetic(freq, 3)).reshape(rg.shape).flatten() Src1_Matrix[:,:,freq_index] = Lm.reshape(rg.shape).T max_idx1 = argmax(Lm.flatten()) # position in grid with max source strength max_value1 = amax(Lm.flatten()) be.n = -2 #Eigenwerte der Größe nach sortiert -> größter Eigenwert (default) Lm = L_p(be.synthetic(freq, 3)).reshape(rg.shape).flatten() Src2_Matrix[:,:,freq_index] = Lm.reshape(rg.shape).T max_idx2 = argmax(Lm.flatten()) # position in grid with max source strength max_value2 = amax(Lm.flatten()) # Opt 1 maxval1[freq_index] = max_value1