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()
def get_beamformer_traj_result(Beamformer, num=32): """ returns the result for a given Beamformer class Parameters ---------- Beamformer : cls trajectory beamformer. num : int, optional number of samples to return. The default is 32. Returns ------- array first block returned by the trajectory beamformers result() function. """ ## with moving grid ts = MaskedTimeSamples(name=FNAME) gMoving = RectGrid(x_min=-.1, x_max=.1, y_min=0, y_max=0, z=0, increment=.1) stMoving = SteeringVector(grid=gMoving, mics=MGEOM) bt = Beamformer(source=ts, trajectory=TRAJ, steer=stMoving) if hasattr(bt, 'n_iter'): bt.n_iter = 2 return next(bt.result(num)).astype(np.float32)
def get_acoular_essentials(self): #Set the mic array geometry mg = MicGeom(from_file=self.array_arrngmnt) #Set rectangular plane and grid parameters for Acoular self.set_grid() rg = RectGrid(x_min=self.x_min_grid, x_max=self.x_max_grid, y_min=self.y_min_grid, y_max=self.y_max_grid, z=self.distance, \ increment=self.grid_increment) st = SteeringVector(grid=rg, mics=mg) return mg, rg, st
def get_acoular_grid(conn, grid_id, ap): ''' function returns acoular grid from given grid id and aperture value ''' grid_type, x1_min, x1_max, x2_min, x2_max, x3_min, dx1 = read_columns( conn=conn, table_name='grid', column_names=[ 'type', 'x1_min', 'x1_max', 'x2_min', 'x2_max', 'x3_min', 'dx1' ], condition_column_name=['id'], condition_value=[grid_id]) if grid_type == 'rect': acoular_grid = RectGrid(x_min=float(x1_min) * ap, x_max=float(x1_max) * ap, y_min=float(x2_min) * ap, y_max=float(x2_max) * ap, z=float(x3_min) * ap, increment=float(dx1) * ap) return acoular_grid
# 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') show()
micgeofile = path.join(path.split(acoular.__file__)[0], 'xml', 'array_56.xml') #calc all values from example 1 cfreq = 4000 t1 = MaskedTimeSamples(name=datafile) t1.start = 0 # first sample, default t1.stop = 16000 # last valid sample = 15999 invalid = [1, 7] # list of invalid channels (unwanted microphones etc.) t1.invalid_channels = invalid t1.calib = Calib(from_file=calibfile) m = MicGeom(from_file=micgeofile) 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)
#octave band of interest cfreq = 4000 t1 = MaskedTimeSamples(name=datafile) t1.start = 0 # first sample, default t1.stop = 16000 # last valid sample = 15999 invalid = [1,7] # list of invalid channels (unwanted microphones etc.) t1.invalid_channels = invalid #t1.calib = Calib(from_file=calibfile) m = MicGeom(from_file=micgeofile) 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) 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)))
#t = p1 # use only fix source # uncomment to save the signal to a wave file #ww = WriteWAV(source = t) #ww.channels = [0,14] #ww.save() #=============================================================================== # fixed focus frequency domain beamforming #=============================================================================== f = PowerSpectra(time_data=t, window='Hanning', overlap='50%', block_size=128, \ ind_low=1,ind_high=30) # CSM calculation g = RectGrid(x_min=-3.0, x_max=+3.0, y_min=-3.0, y_max=+3.0, z=Z, increment=0.3) b = BeamformerBase(freq_data=f, grid=g, mpos=m, r_diag=True, c=c0) map1 = b.synthetic(freq, 3) #=============================================================================== # fixed focus time domain beamforming #=============================================================================== fi = FiltFiltOctave(source=t, band=freq, fraction='Third octave') bt = BeamformerTimeSq(source=fi, grid=g, mpos=m, r_diag=True, c=c0) avgt = TimeAverage(source=bt, naverage=int(sfreq * tmax / 16)) # 16 single images cacht = TimeCache(source=avgt) # cache to prevent recalculation map2 = zeros(g.shape) # accumulator for average
micGeoFile = path.join(path.split(acoular.__file__)[0], 'xml', 'array_56.xml') freqInt = 4000 t1 = MaskedTimeSamples(name = dataFile) t1.start = 0 t1.stop = 16000 invalid = [1, 7] t1.invalid_channels = invalid t1.calib = Calib(from_file = calibFile) m = MicGeom(from_file = micGeoFile) 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) f = EigSpectra(time_data = t1, window = 'Hanning', overlap = '50%', block_size = 128, ind_low = 7, ind_high = 15) 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 = range(38, 54)) bs = BeamformerCleansc(freq_data = f, grid = g, mpos = m, r_diag = True, c = 346.04) bf = BeamformerCMF(freq_data = f, grid = g, mpos = m, c = 346.04, method = 'LassoLarsBIC')
"obj.plane = array((1., 0., 0.))"), # "OpenJet.origin item assignment": (OpenJet(), "obj.origin[0] = 1."), "OpenJet.origin array assignment": (OpenJet(), "obj.origin = array((1., 0., 0.))"), # "RotatingFlow.origin item assignment": (RotatingFlow(), "obj.origin[0] = 1."), "RotatingFlow.origin array assignment": (RotatingFlow(), "obj.origin = array((1., 0., 0.))"), #fbeamform.py # "SteeringVector.ref item assignment": (SteeringVector(), "obj.ref[0] = 1."), "SteeringVector.ref array assignment": (SteeringVector(), "obj.ref = array((1., 1., 1.))"), # "BeamformerOrth.eva_list item assignment": (BeamformerOrth(eva_list=array((0, 1))), "obj.eva_list[0] = 2"), "BeamformerOrth.eva_list array assignment": (BeamformerOrth(eva_list=array((0, 1))), "obj.eva_list = array((2))"), #grids.py "MergeGrid.grids item assignment": (MergeGrid(grids=[RectGrid()]), "obj.grids[0] = RectGrid()"), "MergeGrid.grids list assignment": (MergeGrid(), "obj.grids = [RectGrid()]"), # signals.py # "FiltWNoiseGenerator.ar item assignment": (FiltWNoiseGenerator(ar=[1.,2.,3.]), "obj.ar[0] = 0."), "FiltWNoiseGenerator.ar array assignment": (FiltWNoiseGenerator(ar=[1., 2., 3.]), "obj.ar = array((0., 0., 0.))"), # "FiltWNoiseGenerator.ma item assignment": (FiltWNoiseGenerator(ma=[1.,2.,3.]), "obj.ma[0] = 0."), "FiltWNoiseGenerator.mar array assignment": (FiltWNoiseGenerator(ma=[1., 2., 3.]), "obj.ma = array((0., 0., 0.))"), #sources.py "MaskedTimeSamples.invalid_channels item assignment": (MaskedTimeSamples(invalid_channels=[1]), "obj.invalid_channels[0] = 0"), "MaskedTimeSamples.invalid_channels list assignment": (MaskedTimeSamples(), "obj.invalid_channels = [0]"),
p1 = PointSource(signal=n1, mics=m, loc=(0,R,Z)) t = Mixer(source = p0, sources = [p1,]) # mix both signals #t = p1 # use only fix source # uncomment to save the signal to a wave file #ww = WriteWAV(source = t) #ww.channels = [0,14] #ww.save() #=============================================================================== # fixed focus frequency domain beamforming #=============================================================================== f = PowerSpectra(time_data=t, window='Hanning', overlap='50%', block_size=128, \ ind_low=1,ind_high=30) # CSM calculation g = RectGrid(x_min=-3.0, x_max=+3.0, y_min=-3.0, y_max=+3.0, z=Z, increment=0.3) st = SteeringVector(grid=g, mics=m) b = BeamformerBase(freq_data=f, steer=st, r_diag=True) map1 = b.synthetic(freq,3) #=============================================================================== # fixed focus time domain beamforming #=============================================================================== fi = FiltFiltOctave(source=t, band=freq, fraction='Third octave') bt = BeamformerTimeSq(source=fi, steer=st, r_diag=True) avgt = TimeAverage(source=bt, naverage=int(sfreq*tmax/16)) # 16 single images cacht = TimeCache(source=avgt) # cache to prevent recalculation map2 = zeros(g.shape) # accumulator for average # plot single frames figure(1,(8,7))
if sys.version_info > (3, ): long = int nsamples = long(sfreq * tmax) n1 = WNoiseGenerator(sample_freq=sfreq, numsamples=nsamples) s1 = SineGenerator(sample_freq=sfreq, numsamples=nsamples, freq=freq) p1 = PNoiseGenerator(seed=1, numsamples=nsamples) #=============================================================================== # define environment and grid #=============================================================================== ufe = UniformFlowEnvironment(ma=0.5, fdv=(0, 1, 0)) g = RectGrid(x_min=-3.0, x_max=+3.0, y_min=-3.0, y_max=+3.0, z=Z, increment=0.2) #=============================================================================== # define some sources #=============================================================================== mp = MovingPointSource(signal=s1, env=ufe, mpos=m, trajectory=tr1) ps = PointSource(signal=s1, mpos=m, loc=(0, R, Z)) pd = PointSourceDipole(signal=s1, mpos=m, direction=(0.5, 0, 0), loc=(0, -2, Z)) un = UncorrelatedNoiseSource(signal=n1, mpos=mg) mix = SourceMixer(sources=[ps, pd])
object_name.configure_traits() m = MicGeom(from_file='UCA8.xml') import matplotlib matplotlib.use('TkAgg') import matplotlib.pyplot as plt from os import path import acoular from acoular import L_p, Calib, MicGeom, TimeSamples, \ RectGrid, BeamformerBase, EigSpectra, BeamformerOrth, BeamformerCleansc, \ MaskedTimeSamples, FiltFiltOctave, BeamformerTimeSq, TimeAverage, \ TimeCache, BeamformerTime, TimePower, BeamformerCMF, \ BeamformerCapon, BeamformerMusic, BeamformerDamas, BeamformerClean, \ BeamformerFunctional object_name.configure_traits() m = MicGeom(from_file='UCA8.xml') m = MicGeom(from_file='UCA8.xml') g = RectGrid(x_min=-0.8, x_max=-0.2, y_min=-0.1, y_max=0.3, z=0.8, increment=0.01) t1 = TimeSamples(name='cry_n0000001.wav') f1 = EigSpectra(time_data=t1, block_size=256, window="Hanning", overlap='75%') e1 = BeamformerBase(freq_data=f1, grid=g, mpos=m, r_diag=False) fr = 4000 L1 = L_p(e1.synthetic(fr, 0)) object_name.configure_traits() m = MicGeom(from_file='UCA8.xml')
t = Mixer(source=p0, sources=[p1]) # mix both signals # t = p1 # use only fix source # uncomment to save the signal to a wave file # ww = WriteWAV(source = t) # ww.channels = [0,14] # ww.save() # =============================================================================== # fixed focus frequency domain beamforming # =============================================================================== f = PowerSpectra( time_data=t, window="Hanning", overlap="50%", block_size=128, ind_low=1, ind_high=30 ) # CSM calculation g = RectGrid(x_min=-3.0, x_max=+3.0, y_min=-3.0, y_max=+3.0, z=Z, increment=0.3) b = BeamformerBase(freq_data=f, grid=g, mpos=m, r_diag=True, c=c0) map1 = b.synthetic(freq, 3) # =============================================================================== # fixed focus time domain beamforming # =============================================================================== fi = FiltFiltOctave(source=t, band=freq, fraction="Third octave") bt = BeamformerTimeSq(source=fi, grid=g, mpos=m, r_diag=True, c=c0) avgt = TimeAverage(source=bt, naverage=int(sfreq * tmax / 16)) # 16 single images cacht = TimeCache(source=avgt) # cache to prevent recalculation map2 = zeros(g.shape) # accumulator for average # plot single frames figure(1) i = 0
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') bl = BeamformerClean(beamformer=bb, n_iter=100) bf = BeamformerFunctional(freq_data=f, grid=g, mpos=m, r_diag=False, c=346.04, \ gamma=4)
t1.calib = Calib(from_file=calibfile) #=============================================================================== # the microphone geometry must have the same number of valid channels as the # TimeSamples object has #=============================================================================== m = MicGeom(from_file=micgeofile) m.invalid_channels = invalid #=============================================================================== # the grid for the beamforming map; a RectGrid3D class is also available # (the example grid is very coarse) #=============================================================================== 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
# object (for backwards compatibility) #=============================================================================== t1.calib = Calib(from_file=calibfile) #=============================================================================== # the microphone geometry must have the same number of valid channels as the # TimeSamples object has #=============================================================================== m = MicGeom(from_file=micgeofile) m.invalid_channels = invalid #=============================================================================== # the grid for the beamforming map; a RectGrid3D class is also available # (the example grid is very coarse) #=============================================================================== 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 = EigSpectra(time_data=t1, window='Hanning', overlap='50%', block_size=128, #FFT-parameters ind_low=7, ind_high=15, precision='complex128') #to save computational effort, only # frequencies with index 1-30 are used csm = f.csm[:] eva = f.eva[:] f32 = EigSpectra(time_data=t1, window='Hanning', overlap='50%', block_size=128, #FFT-parameters