def audio_csv_extraction(filepath, trackname, st, firstframe): if DETAILEDINFO_LVL >= 3: print('[Azimuth, Elevation], max_value') ts = WavSamples(name=filepath,start=firstframe*NUM,stop=(firstframe+1)*NUM) ps = PowerSpectra(time_data=ts, block_size=512, window='Hanning') be = BeamformerEig(freq_data=ps, steer=st, r_diag=True, n=3) # be = BeamformerBase(freq_data=ps, steer=st, r_diag=True) # bb = BeamformerBase(freq_data=ps, steer=st) # bo = BeamformerOrth(beamformer=be, eva_list=[3]) # Extraktion von Framenummer (Spalte 0), Geräuschklasse (Spalte 1), Azi (Spalte 3) und Ele (Spalte 4) aus .csv-Datei csvpath = CSV_DIR+trackname+".csv" rawdata = loadtxt(open(csvpath, "rb"), dtype="int32", delimiter=",", usecols=(0,1,3,4)) if TRAINING or JUST_1SOURCE_FRAMES: labeldata = rawdata = rm_2sources_frames(rawdata) if THRESHOLD_FILTER: soundpressure = zeros((600,1)) ## Sound Pressure Level ## wavsamples = WavSamples(name = filepath).data[:,3] for frameindex, _ in enumerate(soundpressure[:,0]): soundpressure[frameindex,0] = sum(wavsamples[frameindex*NUM:(frameindex+1)*NUM]**2)/NUM spl = L_p(soundpressure[:,0]) ########################## threshold = threshold_calculator(spl) ## Liste der per Threshold gefilterten Frames ## active_frames = [] for index, frame in enumerate(spl): if frame > threshold: active_frames.append(index) ################################################ # active_frames weiterhin eine Liste, aus der man Elemente # auch zwischendrin löschen kann # thrdata = array(active_frames).reshape((len(active_frames),1)) thrdata = [] for ind, frame in enumerate(rawdata[:,0]): if frame in active_frames: thrdata.append(rawdata[ind,:]) labeldata = array(thrdata) if not(TRAINING or THRESHOLD_FILTER): labeldata = rawdata return ts, be, labeldata
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
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()
sfreq= 12800 n1 = WNoiseGenerator(sample_freq = sfreq, numsamples = 10*sfreq, seed = 1) m = MicGeom() m.mpos_tot = array([[0,0,0]]) t = PointSource(signal = n1, mpos = m, loc = (1, 0, 1)) f = PowerSpectra(time_data = t, window = 'Hanning', overlap = '50%', block_size = 4096) ################################################################### ### Plotting ### from pylab import figure,plot,show,xlim,ylim,xscale,xticks,xlabel,ylabel,grid,real from acoular import L_p band = 3 # octave: 1 ; 1/3-octave: 3 (f_borders, p, f_center) = barspectrum(real(f.csm[:,0,0]), f.fftfreq(), band) label_freqs = [str(int(_)) for _ in f_center] figure(figsize=(20, 6))
g = RectGrid(x_min=-0.6, x_max=-0.0, y_min=-0.3, y_max=0.3, z=0.68, increment=0.05) #=============================================================================== # for frequency domain methods, this provides the cross spectral matrix and its # eigenvalues and eigenvectors, if only the matrix is needed then class # PowerSpectra can be used instead #=============================================================================== f = PowerSpectra( time_data=t1, window='Hanning', overlap='50%', block_size=128, #FFT-parameters ind_low=8, ind_high=16) #to save computational effort, only # frequencies with indices 8..15 are used #=============================================================================== # different beamformers in frequency domain #=============================================================================== bb = BeamformerBase(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04) bc = BeamformerCapon(freq_data=f, grid=g, mpos=m, c=346.04, cached=False) be = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04, n=54) bm = BeamformerMusic(freq_data=f, grid=g, mpos=m, c=346.04, n=6) bd = BeamformerDamas(beamformer=bb, n_iter=100) bo = BeamformerOrth(beamformer=be, eva_list=list(range(38, 54))) bs = BeamformerCleansc(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04)
wh5.save() #definition the different source signal r = 52.5 nsamples = long(sfreq * 0.3) n1 = WNoiseGenerator(sample_freq=sfreq, numsamples=nsamples) s1 = SineGenerator(sample_freq=sfreq, numsamples=nsamples, freq=freq) s2 = SineGenerator(sample_freq=sfreq, numsamples=nsamples, freq=freq, \ phase=pi) #define a circular array of 8 microphones m = MicGeom('array_64_8mic.xml') m.mpos_tot = array([(r*sin(2*pi*i+pi/8), r*cos(2*pi*i+pi/8), 0) \ for i in linspace(0.0, 1.0, 8, False)]).T t = MaskedTimeSamples(name=datafile) f = PowerSpectra(time_data=t, window='Hanning', overlap='50%', block_size=128, \ ind_low=1,ind_high=30) g = RectGrid(x_min=0 - .2, x_max=0.3, y_min=-0.13, y_max=0.3, z=0, increment=0.05) bb = BeamformerBase(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04) bc = BeamformerCapon(freq_data=f, grid=g, mpos=m, c=346.04, cached=False) be = BeamformerEig(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04, n=54) bm = BeamformerMusic(freq_data=f, grid=g, mpos=m, c=346.04, n=6) bd = BeamformerDamas(beamformer=bb, n_iter=100) bo = BeamformerOrth(beamformer=be, eva_list=list(range(38, 54))) bs = BeamformerCleansc(freq_data=f, grid=g, mpos=m, r_diag=True, c=346.04) bcmf = BeamformerCMF(freq_data=f, grid=g, mpos=m, c=346.04, \ method='LassoLarsBIC')
# 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, 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')
m.invalid_channels = invalid g = RectGrid(x_min=-0.6, x_max=-0.0, y_min=-0.3, y_max=0.3, z=0.68, increment=0.05) env = Environment(c=346.04) st = SteeringVector(grid=g, mics=m, env=env) f = PowerSpectra( time_data=t1, window='Hanning', overlap='50%', block_size=128, #FFT-parameters cached=False) #cached = False bb = BeamformerBase(freq_data=f, steer=st, r_diag=True, cached=False) bc = BeamformerCapon(freq_data=f, steer=st, cached=False) be = BeamformerEig(freq_data=f, steer=st, r_diag=True, n=54, cached=False) bm = BeamformerMusic(freq_data=f, steer=st, n=6, cached=False) bd = BeamformerDamas(beamformer=bb, n_iter=100, cached=False) bdp = BeamformerDamasPlus(beamformer=bb, n_iter=100, cached=False) bo = BeamformerOrth(beamformer=be, eva_list=list(range(38, 54)), cached=False) bs = BeamformerCleansc(freq_data=f, steer=st, r_diag=True, cached=False) bcmf = BeamformerCMF(freq_data=f, steer=st, method='LassoLarsBIC', cached=False)
# constants sfreq = 12800 # sample frequency band = 3 # octave: 1 ; 1/3-octave: 3 (for plotting) # set up microphone at (0,0,0) m = MicGeom() m.mpos_tot = array([[0, 0, 0]]) # create noise source n1 = WNoiseGenerator(sample_freq=sfreq, numsamples=10 * sfreq, seed=1) t = PointSource(signal=n1, mics=m, loc=(1, 0, 1)) # create power spectrum f = PowerSpectra(time_data=t, window='Hanning', overlap='50%', block_size=4096) # get spectrum data spectrum_data = real(f.csm[:, 0, 0]) # get power spectrum from cross-spectral matrix freqs = f.fftfreq() # FFT frequencies # use barspectrum from acoular.tools to create third octave plot data (f_borders, p, f_center) = barspectrum(spectrum_data, freqs, band, bar=True) (f_borders_, p_, f_center_) = barspectrum(spectrum_data, freqs, band, bar=False) # create figure with barspectra figure(figsize=(20, 6))
x_max=0.2, y_min=-0.2, y_max=0.2, z_min=0.1, z_max=0.36, increment=0.02) #=============================================================================== # The following provides the cross spectral matrix and defines the CLEAN-SC beamformer. # To be really fast, we restrict ourselves to only 10 frequencies # in the range 2000 - 6000 Hz (5*400 - 15*400) #=============================================================================== f = PowerSpectra(time_data=pa, window='Hanning', overlap='50%', block_size=128, ind_low=5, ind_high=16) st = SteeringVector(grid=g, mics=m, steer_type='true location') b = BeamformerCleansc(freq_data=f, steer=st) #=============================================================================== # Calculate the result for 4 kHz octave band #=============================================================================== map = b.synthetic(4000, 1) #=============================================================================== # Display views of setup and result. # For each view, the values along the repsective axis are summed. # Note that, while Acoular uses a left-oriented coordinate system,
if __name__ == '__main__': ################################################################### ### Defining noise source ### from acoular import WNoiseGenerator, PointSource, PowerSpectra, MicGeom sfreq = 12800 n1 = WNoiseGenerator(sample_freq=sfreq, numsamples=10 * sfreq, seed=1) m = MicGeom() m.mpos_tot = array([[0, 0, 0]]) t = PointSource(signal=n1, mpos=m, loc=(1, 0, 1)) f = PowerSpectra(time_data=t, window='Hanning', overlap='50%', block_size=4096) ################################################################### ### Plotting ### from pylab import figure, plot, show, xlim, ylim, xscale, xticks, xlabel, ylabel, grid, real from acoular import L_p band = 3 # octave: 1 ; 1/3-octave: 3 (f_borders, p, f_center) = barspectrum(real(f.csm[:, 0, 0]), f.fftfreq(), band) label_freqs = [str(int(_)) for _ in f_center] figure(figsize=(20, 6)) plot(f_borders, L_p(p))