Пример #1
0
    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
Пример #2
0
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()
Пример #3
0
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, 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')
Пример #4
0
from pylab import figure, subplot, imshow, show, colorbar, title

# files
datafile = 'example_data.h5'
calibfile = 'example_calib.xml'
micgeofile = path.join(path.split(acoular.__file__)[0], 'xml', 'array_56.xml')

#octave band of interest
cfreq = 4000

#===============================================================================
# first, we define the time samples using the MaskedTimeSamples class
# alternatively we could use the TimeSamples class that provides no masking
# of channels and samples
#===============================================================================
t1 = TimeSamples(name=datafile)

#===============================================================================
# calibration is usually needed and can be set directly at the TimeSamples
# object (preferred) or for frequency domain processing at the PowerSpectra
# 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)

#===============================================================================
# imports from acoular
import acoular
from acoular import L_p, TimeSamples, Calib, MicGeom, EigSpectra,\
RectGrid3D, BeamformerBase, BeamformerFunctional, BeamformerEig, BeamformerOrth, \
BeamformerCleansc, BeamformerCapon, BeamformerMusic, BeamformerCMF, PointSpreadFunction, BeamformerClean, BeamformerDamas

# other imports
from os import path
#from mayavi import mlab
from numpy import amax
#from cPickle import dump, load
from pickle import dump, load

# see example3
t = TimeSamples(name='example_data.h5')
cal = Calib(from_file='example_calib.xml')
m = MicGeom(from_file=path.join(\
    path.split(acoular.__file__)[0], 'xml', 'array_56.xml'))
g = RectGrid3D(x_min=-0.6, x_max=-0.0, y_min=-0.3, y_max=0.3, \
    z_min=0.48, z_max=0.88, increment=0.1)
f = EigSpectra(time_data=t,
               window='Hanning',
               overlap='50%',
               block_size=128,
               ind_low=5,
               ind_high=15)
csm = f.csm[:]
eva = f.eva[:]
eve = f.eve[:]
Пример #6
0
from pylab import figure, subplot, imshow, show, colorbar, title

# files
datafile = 'example_data.h5'
calibfile = 'example_calib.xml'
micgeofile = path.join( path.split(acoular.__file__)[0],'xml','array_56.xml')

#octave band of interest
cfreq = 4000

#===============================================================================
# first, we define the time samples using the MaskedTimeSamples class
# alternatively we could use the TimeSamples class that provides no masking
# of channels and samples
#===============================================================================
t1 = TimeSamples(name=datafile)

#===============================================================================
# calibration is usually needed and can be set directly at the TimeSamples 
# object (preferred) or for frequency domain processing at the PowerSpectra 
# 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)

#===============================================================================
Пример #7
0
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')