-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·178 lines (157 loc) · 7.17 KB
/
example.py
File metadata and controls
executable file
·178 lines (157 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
"""
Example 1 for acoular library
demonstrates different features of acoular,
uses measured data in file example_data.h5
calibration in file example_calib.xml
microphone geometry in array_56.xml (part of acoular)
Copyright (c) 2006-2015 The Acoular developers.
All rights reserved.
"""
# imports from acoular
import acoular
from acoular import L_p, Calib, MicGeom, EigSpectra, \
RectGrid, BeamformerBase, BeamformerEig, BeamformerOrth, BeamformerCleansc, \
MaskedTimeSamples, FiltFiltOctave, BeamformerTimeSq, TimeAverage, \
TimeCache, BeamformerTime, TimePower, BeamformerCMF, \
BeamformerCapon, BeamformerMusic, BeamformerDamas, BeamformerClean, \
BeamformerFunctional
# other imports
from numpy import zeros
from os import path
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')
# DEBUG
import h5py as h5
import numpy as np
def print_name(name):
print(name)
with h5.File(datafile, 'r') as hf:
hf.visit(print_name)
for name in hf.keys():
print(name)
print(hf[name])
tmp = np.array(hf[name])
print(tmp)
print(tmp.shape)
#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 = 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
#===============================================================================
# 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)
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.02)
#===============================================================================
# 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) #to save computational effort, only
# frequencies with index 1-30 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=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=True, c=346.04, \
gamma=4)
#===============================================================================
# plot result maps for different beamformers in frequency domain
#===============================================================================
figure(1)
i1 = 1 #no of subplot
for b in (bb, bc, be, bm, bl, bo, bs, bd, bcmf, bf):
subplot(3,4,i1)
i1 += 1
map = b.synthetic(cfreq,1)
mx = L_p(map.max())
imshow(L_p(map.T), vmax=mx, vmin=mx-15,
interpolation='nearest', extent=g.extend())
colorbar()
title(b.__class__.__name__)
#===============================================================================
# delay and sum beamformer in time domain
# processing chain: beamforming, filtering, power, average
#===============================================================================
bt = BeamformerTime(source=t1, grid=g, mpos=m, c=346.04)
ft = FiltFiltOctave(source=bt, band=cfreq)
pt = TimePower(source=ft)
avgt = TimeAverage(source=pt, naverage = 1024)
cacht = TimeCache( source = avgt) # cache to prevent recalculation
#===============================================================================
# delay and sum beamformer in time domain with autocorrelation removal
# processing chain: zero-phase filtering, beamforming+power, average
#===============================================================================
fi = FiltFiltOctave(source=t1, band=cfreq)
bts = BeamformerTimeSq(source = fi,grid=g, mpos=m, r_diag=True,c=346.04)
avgts = TimeAverage(source=bts, naverage = 1024)
cachts = TimeCache( source = avgts) # cache to prevent recalculation
#===============================================================================
# plot result maps for different beamformers in time domain
#===============================================================================
i2 = 2 # no of figure
for b in (cacht, cachts):
# first, plot time-dependent result (block-wise)
figure(i2)
i2 += 1
res = zeros(g.size) # init accumulator for average
i3 = 1 # no of subplot
for r in b.result(1): #one single block
subplot(4,4,i3)
i3 += 1
res += r[0] # average accum.
map = r[0].reshape(g.shape)
mx = L_p(map.max())
imshow(L_p(map.T), vmax=mx, vmin=mx-15,
interpolation='nearest', extent=g.extend())
title('%i' % ((i3-1)*1024))
res /= i3-1 # average
# second, plot overall result (average over all blocks)
figure(1)
subplot(3,4,i1)
i1 += 1
map = res.reshape(g.shape)
mx = L_p(map.max())
imshow(L_p(map.T), vmax=mx, vmin=mx-15,
interpolation='nearest', extent=g.extend())
colorbar()
title(('BeamformerTime','BeamformerTimeSq')[i2-3])
show()
'''