def run(): from os import path from acoular import __file__ as bpath, MicGeom, WNoiseGenerator, PointSource,\ Mixer, WriteH5, TimeSamples, PowerSpectra, RectGrid, SteeringVector,\ BeamformerBase, L_p from pylab import figure, plot, axis, imshow, colorbar, show # set up the parameters sfreq = 51200 duration = 1 nsamples = duration * sfreq micgeofile = path.join(path.split(bpath)[0], 'xml', 'array_64.xml') h5savefile = 'three_sources.h5' # generate test data, in real life this would come from an array measurement mg = MicGeom(from_file=micgeofile) n1 = WNoiseGenerator(sample_freq=sfreq, numsamples=nsamples, seed=1) n2 = WNoiseGenerator(sample_freq=sfreq, numsamples=nsamples, seed=2, rms=0.7) n3 = WNoiseGenerator(sample_freq=sfreq, numsamples=nsamples, seed=3, rms=0.5) p1 = PointSource(signal=n1, mics=mg, loc=(-0.1, -0.1, 0.3)) p2 = PointSource(signal=n2, mics=mg, loc=(0.15, 0, 0.3)) p3 = PointSource(signal=n3, mics=mg, loc=(0, 0.1, 0.3)) pa = Mixer(source=p1, sources=[p2, p3]) wh5 = WriteH5(source=pa, name=h5savefile) wh5.save() # analyze the data and generate map ts = TimeSamples(name=h5savefile) ps = PowerSpectra(time_data=ts, block_size=128, window='Hanning') rg = RectGrid( x_min=-0.2, x_max=0.2, y_min=-0.2, y_max=0.2, z=0.3, \ increment=0.01 ) st = SteeringVector(grid=rg, mics=mg) bb = BeamformerBase(freq_data=ps, steer=st) pm = bb.synthetic(8000, 3) Lm = L_p(pm) # show map imshow( Lm.T, origin='lower', vmin=Lm.max()-10, extent=rg.extend(), \ interpolation='bicubic') colorbar() # plot microphone geometry figure(2) plot(mg.mpos[0], mg.mpos[1], 'o') axis('equal') show()
n1 = WNoiseGenerator( sample_freq=sfreq, numsamples=nsamples, seed=1 ) n2 = WNoiseGenerator( sample_freq=sfreq, numsamples=nsamples, seed=2, rms=0.7 ) n3 = WNoiseGenerator( sample_freq=sfreq, numsamples=nsamples, seed=3, rms=0.5 ) p1 = PointSource( signal=n1, mpos=mg, loc=(-0.1,-0.1,0.3) ) p2 = PointSource( signal=n2, mpos=mg, loc=(0.15,0,0.3) ) p3 = PointSource( signal=n3, mpos=mg, loc=(0,0.1,0.3) ) pa = Mixer( source=p1, sources=[p2,p3] ) wh5 = WriteH5( source=pa, name=h5savefile ) wh5.save() # analyze the data and generate map ts = TimeSamples( name=h5savefile ) ps = PowerSpectra( time_data=ts, block_size=128, window='Hanning' ) rg = RectGrid( x_min=-0.2, x_max=0.2, y_min=-0.2, y_max=0.2, z=0.3, \ increment=0.01 ) bb = BeamformerBase( freq_data=ps, grid=rg, mpos=mg ) pm = bb.synthetic( 8000, 3 ) Lm = L_p( pm ) # show map imshow( Lm.T, origin='lower', vmin=Lm.max()-10, extent=rg.extend(), \ interpolation='bicubic') colorbar() # plot microphone geometry figure(2) plot(mg.mpos[0],mg.mpos[1],'o') axis('equal') show()
# usually, another type of beamformer (e.g. CLEAN-SC) would be more appropriate # to be really fast, we restrict ourselves to only 10 frequencies # in the range 2000 - 6000 Hz (5*400 - 15*400) #=============================================================================== f = EigSpectra(time_data=t, window='Hanning', overlap='50%', block_size=128, \ ind_low=5, ind_high=15) b = BeamformerBase(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04) #=============================================================================== # reads the data, finds the maximum value (to properly scale the views) #=============================================================================== map = b.synthetic(4000,1) L1 = L_p(map) mx = L1.max() #=============================================================================== # print out the result integrated over an 3d-sector of the 3d map #=============================================================================== print(L_p(b.integrate((-0.3,-0.1,0.58,-0.1,0.1,0.78)))[f.ind_low:f.ind_high]) #=============================================================================== # displays the 3d view #=============================================================================== X,Y,Z = mgrid[g.x_min:g.x_max:1j*g.nxsteps,\ g.y_min:g.y_max:1j*g.nysteps,\ g.z_min:g.z_max:1j*g.nzsteps] data = mlab.pipeline.scalar_field(X,Y,Z,L1)
# in the range 2000 - 6000 Hz (5*400 - 15*400) #=============================================================================== f = PowerSpectra(time_data=t, window='Hanning', overlap='50%', block_size=128, \ ind_low=5, ind_high=15) env = Environment(c=346.04) st = SteeringVector(grid=g, mics=m, env=env) b = BeamformerBase(freq_data=f, steer=st, r_diag=True) #=============================================================================== # reads the data, finds the maximum value (to properly scale the views) #=============================================================================== map = b.synthetic(4000,1) L1 = L_p(map) mx = L1.max() #=============================================================================== # print out the result integrated over an 3d-sector of the 3d map #=============================================================================== print(L_p(b.integrate((-0.3,-0.1,0.58,-0.1,0.1,0.78)))[f.ind_low:f.ind_high]) #=============================================================================== # displays the 3d view #=============================================================================== X,Y,Z = mgrid[g.x_min:g.x_max:1j*g.nxsteps,\ g.y_min:g.y_max:1j*g.nysteps,\ g.z_min:g.z_max:1j*g.nzsteps] data = mlab.pipeline.scalar_field(X,Y,Z,L1)
def fbeamextraction(mg, rg, ts, be, firstframe, lastframe, csvdata, _name, fbeamplot, fbeamplot_2ndSrc, algoplot, algoplot_2ndSrc): ##################### DeepLearning-Matrix (Framewise) ######################### ################ Elevation Azimuth Frequenzbänder ################### DL_Matrix = zeros((NPOINTS_ELE, NPOINTS_AZI, len(FREQBANDS)), dtype="float32") ## ################################################################################# for frame in range(firstframe, lastframe): if frame in csvdata[:, 0]: fr_index = where(csvdata[:, 0] == frame)[0][0] if DETAILEDINFO_LVL >= 1: print(' FRAME: ', frame - firstframe, ' (', frame, ')') ts.start = frame * NUM ts.stop = (frame + 1) * NUM # Zur Berechnung des Mittelwerts der Richtungswinkel if not (TRAINING): azis = list(zeros(len(FREQBANDS))) azic = list(zeros(len(FREQBANDS))) eles = list(zeros(len(FREQBANDS))) elec = list(zeros(len(FREQBANDS))) azis_2nd = list(zeros(len(FREQBANDS))) azic_2nd = list(zeros(len(FREQBANDS))) eles_2nd = list(zeros(len(FREQBANDS))) elec_2nd = list(zeros(len(FREQBANDS))) #%% 1. Quelle ############################## # Zur Angle-Prediction ohne händischem Algo glob_maxval = 0 glob_maxidx = 0 for freq_index, freq in enumerate(FREQBANDS): be.n = -1 #Eigenwerte sind der Größe nach sortiert! -> größter Eigenwert (default) Lm = L_p(be.synthetic(freq, 3)).reshape(rg.shape).flatten() DL_Matrix[:, :, freq_index] = Lm.reshape(rg.shape).T if PLOTBEAMMAPS or DETAILEDINFO_LVL >= 3 or not (TRAINING): max_idx = argmax(Lm.flatten( )) # position in grid with max source strength max_value = amax(Lm.flatten()) if glob_maxval < max_value: # TODO: 'and freq != 500:' !?! glob_maxidx = max_idx glob_maxval = max_value # min_value = amin(Lm.flatten()) max_cartcoord = rg.gpos[:, max_idx] temp_azi = arctan2(sin(rg.phi[max_idx]), cos(rg.phi[max_idx])) temp_ele = pi / 2 - rg.theta[max_idx] max_polcoord = [rad2deg(temp_azi), rad2deg(temp_ele)] azis[freq_index] = sin(temp_azi) azic[freq_index] = cos(temp_azi) eles[freq_index] = sin(temp_ele) elec[freq_index] = cos(temp_ele) if PLOTBEAMMAPS: #### 3D-Plot ### fig = plt.figure() ax = fig.add_subplot(121, projection='3d') ax.set_xlabel('x-Achse') ax.set_ylabel('y-Achse') ax.set_zlabel('z-Achse') ax.set_xlim(-1, 1) ax.set_ylim(-1, 1) ax.set_zlim(-1, 1) cmhot = plt.get_cmap("hot_r") if frame == firstframe: ax.scatter(rg.gpos[0], rg.gpos[1], -rg.gpos[2], s=50, c=Lm, cmap=cmhot, marker='.') # Mikros ax.scatter(mg.mpos[0], mg.mpos[1], mg.mpos[2], 'o') ax.scatter(max_cartcoord[0], max_cartcoord[1], max_cartcoord[2], s=60, c='blue') ### 3D-Plot Ende ### ### 2D-Map ### ax2 = fig.add_subplot(122) ax2.set_xticks(arange(-180, 270, step=90)) ax2.set_yticks(arange(-90, 135, step=45)) cax2 = ax2.imshow(Lm.reshape(rg.shape).T, cmap=cmhot, vmin=Lm.max() - 6, vmax=Lm.max(), extent=[-180, 180, -90, 90]) ax2.plot(max_polcoord[0], max_polcoord[1], 'bo') fig.set_figheight(4) fig.set_figwidth(8) fig.colorbar(cax2) fig.tight_layout(pad=3.0) fig.suptitle(_name + ' ' + str(frame) + ' ' + str(freq)) plt.show() ### 2D-Map Ende ### if DETAILEDINFO_LVL >= 3: print(' {:4d} [{:7.2f}, {:7.2f}] {}'.format( freq, round(max_polcoord[0], 2), round(max_polcoord[1], 2), round(max_value, 2))) #print(' ',freq, max_cartcoord, max_value) if DETAILEDINFO_LVL >= 2 and TRAINING: print(" .csv-Values: Class Azi Ele") print(" ", '{:4d}'.format(csvdata[fr_index, 1]), '{:4d}'.format(csvdata[fr_index, 2]), '{:4d}'.format(csvdata[fr_index, 3])) if TRAINING: feature_dict = { 'inputmap': _float_list_feature(DL_Matrix), 'class': _int64_feature(csvdata[fr_index, 1]), 'azi': _int64_feature(csvdata[fr_index, 2]), 'ele': _int64_feature(csvdata[fr_index, 3]), } if not (TRAINING): # Prediction nur übers globale Maximum azi_pred = arctan2(sin(rg.phi[glob_maxidx]), cos(rg.phi[glob_maxidx])) ele_pred = pi / 2 - rg.theta[glob_maxidx] fbeamplot.append([frame, rad2deg(azi_pred), rad2deg(ele_pred)]) # Calculation per Algorithmus (basierend auf Ergebnissen des fbeamformings) azi_algo, ele_algo = angle_calc_algo(azis, azic, eles, elec) algoplot.append([frame, rad2deg(azi_algo), rad2deg(ele_algo)]) feature_dict = {'inputmap': _float_list_feature(DL_Matrix)} yield feature_dict #%% 2. Quelle ############################## # Nur, wenn nicht bereits am Ende des Arrays (wenn bei letztem aktiven Frame nur 1 Quelle) if not (fr_index + 1 == len(csvdata)): if not (JUST_1SOURCE_FRAMES or TRAINING) and (frame == csvdata[fr_index + 1, 0]): glob_maxval = 0 glob_maxidx = 0 if DETAILEDINFO_LVL >= 1: print(' FRAME: ', frame - firstframe, ' (', frame, '), 2nd Src') for freq_index, freq in enumerate(FREQBANDS): be.n = -2 # zweitgrößter Eigenwert -> zweitstärkste Quelle Lm = L_p(be.synthetic(freq, 3)).reshape(rg.shape).flatten() DL_Matrix[:, :, freq_index] = Lm.reshape(rg.shape).T max_idx = argmax(Lm.flatten( )) # position in grid with max source strength max_value = amax(Lm.flatten()) if glob_maxval < max_value: # TODO: 'and freq != 500:' !?! glob_maxidx = max_idx glob_maxval = max_value max_cartcoord = rg.gpos[:, max_idx] temp_azi = arctan2(sin(rg.phi[max_idx]), cos(rg.phi[max_idx])) temp_ele = pi / 2 - rg.theta[max_idx] max_polcoord = [rad2deg(temp_azi), rad2deg(temp_ele)] azis_2nd[freq_index] = sin(temp_azi) azic_2nd[freq_index] = cos(temp_azi) eles_2nd[freq_index] = sin(temp_ele) elec_2nd[freq_index] = cos(temp_ele) if PLOTBEAMMAPS: #### 3D-Plot ### fig = plt.figure() ax = fig.add_subplot(121, projection='3d') ax.set_xlabel('x-Achse') ax.set_ylabel('y-Achse') ax.set_zlabel('z-Achse') ax.set_xlim(-1, 1) ax.set_ylim(-1, 1) ax.set_zlim(-1, 1) cmhot = plt.get_cmap("hot_r") if frame == firstframe: ax.scatter(rg.gpos[0], rg.gpos[1], -rg.gpos[2], s=50, c=Lm, cmap=cmhot, marker='.') # Mikros ax.scatter(mg.mpos[0], mg.mpos[1], mg.mpos[2], 'o') ax.scatter(max_cartcoord[0], max_cartcoord[1], max_cartcoord[2], s=60, c='blue') ### 3D-Plot Ende ### ### 2D-Map ### ax2 = fig.add_subplot(122) ax2.set_xticks(arange(-180, 270, step=90)) ax2.set_yticks(arange(-90, 135, step=45)) cax2 = ax2.imshow(Lm.reshape(rg.shape).T, cmap=cmhot, vmin=Lm.max() - 6, vmax=Lm.max(), extent=[-180, 180, -90, 90]) ax2.plot(max_polcoord[0], max_polcoord[1], 'bo') fig.set_figheight(4) fig.set_figwidth(8) fig.colorbar(cax2) fig.tight_layout(pad=3.0) fig.suptitle(_name + ' (Frame:' + str(frame) + ', Freq:' + str(freq) + ') 2nd Source') plt.show() ### 2D-Map Ende ### if DETAILEDINFO_LVL >= 3: print(' {:4d} [{:7.2f}, {:7.2f}] {} (2nd Src)'. format(freq, round(max_polcoord[0], 2), round(max_polcoord[1], 2), round(max_value, 2))) #print(' ',freq, max_cartcoord, max_value) # Prediction nur übers globale Maximum azi_pred_2nd = arctan2(sin(rg.phi[glob_maxidx]), cos(rg.phi[glob_maxidx])) ele_pred_2nd = pi / 2 - rg.theta[glob_maxidx] fbeamplot_2ndSrc.append( [frame, rad2deg(azi_pred_2nd), rad2deg(ele_pred_2nd)]) # Calculation per Algorithmus (basierend auf Ergebnissen des fbeamformings) azi_algo_2nd, ele_algo_2nd = angle_calc_algo( azis_2nd, azic_2nd, eles_2nd, elec_2nd) algoplot_2ndSrc.append( [frame, rad2deg(azi_algo_2nd), rad2deg(ele_algo_2nd)]) feature_dict = {'inputmap': _float_list_feature(DL_Matrix)} yield feature_dict