-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
181 lines (131 loc) · 5.25 KB
/
Copy pathmain.py
File metadata and controls
181 lines (131 loc) · 5.25 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
179
180
181
import acoular
import matplotlib.pylab as plt
from scipy.io import wavfile
import tables
import numpy as np
from os import path
# Read data from stereo WAV file
fs, data = wavfile.read('./24y_10m_21d_20h_28m_13s/audio_0_trim_5000_small.wav')
print("fs", fs)
print("data.shape[0]", data.shape[0])
# Ensure data is in the correct shape (two channels)
if data.ndim == 1:
# If it's mono, replicate the channel
data = np.stack([data, data], axis=-1)
# Output
folder = './output_folder/'
name = 'audio_0_trim_5000.h5'
# Save to Acoular HDF5 format
acoularh5 = tables.open_file(folder + name, mode="w", title=name)
acoularh5.create_earray('/', 'time_data', atom=tables.Float32Atom(), title='',
filters=None, expectedrows=data.shape[0],
chunkshape=None,
byteorder=None, createparents=False, obj=data.astype(np.float32))
acoularh5.set_node_attr('/time_data', 'sample_freq', fs)
acoularh5.close()
print(acoularh5)
# # Read data from WAV
# fs, data = wavfile.read('./24y_10m_21d_20h_28m_13s/audio_0.wav')
# # Ensure data is in the correct shape (handle mono/stereo cases)
# if data.ndim == 1:
# data = data[:, np.newaxis] # Convert mono to shape (samples, 1)
# # Convert data to float32 for Acoular
# data = data.astype(np.float32)
# # Output
# folder = './output_folder/'
# name = 'audio_0_NEW.h5'
# # Save to Acoular HDF5 format
# acoularh5 = tables.open_file(folder + name, mode="w", title=name)
# acoularh5.create_earray('/', 'time_data', atom=tables.Float32Atom(), title='',
# filters=None, expectedrows=data.shape[0],
# chunkshape=(256, data.shape[1]), obj=data)
# acoularh5.set_node_attr('/time_data', 'sample_freq', fs)
# acoularh5.close()
# start beamforming analysis
ts = acoular.TimeSamples( name='./output_folder/audio_0_trim_5000.h5' )
# to do beamforming in freq domain
ps = acoular.PowerSpectra( time_data=ts, block_size=128, window="Hanning" )
# rg = acoular.RectGrid( x_min=-2, x_max=2, y_min=-2, y_max=2, z=2, increment=0.1 )
# Define microphone spacing in meters (144 mm = 0.144 m)
mg = acoular.MicGeom( from_file="mic_array.xml" )
# Print microphone positions for verification
print("Microphone positions:\n", mg.mpos)
# Load microphone geometry from XML
mg = acoular.MicGeom(from_file="mic_array.xml")
# Check if microphone positions are loaded correctly
print("Microphone positions:\n", mg.mpos)
# Plot microphone positions
plt.figure() # Create a new figure for the microphone positions
if mg.mpos.size > 0:
plt.plot(mg.mpos[0], mg.mpos[1], 'o') # Plot x and y positions
plt.axis('equal')
plt.xlabel('X Position (m)')
plt.ylabel('Y Position (m)')
plt.title('Microphone Positions')
plt.show(block=False) # Show the plot and keep it open
else:
print("No microphone positions available.")
# plot sounds
rg = acoular.RectGrid(x_min=-1, x_max=1, y_min=-1, y_max=1, z=0.030, increment=0.001)
st = acoular.SteeringVector( grid=rg, mics=mg )
bb = acoular.BeamformerCapon(freq_data=ps, steer=st)
pm = bb.synthetic(5000, 0)
Lm = acoular.L_p(pm)
print(Lm.shape)
print(Lm)
# Plot beamforming map with bicubic interpolation
plt.figure() # Create a new figure for the beamforming plot
plt.imshow(Lm.T, origin="lower", vmin=Lm.max() - 3, # 3 dB threshold
extent=rg.extend(), interpolation='bicubic') # Bicubic interpolation for smoother visuals
plt.colorbar() # Show color scale
plt.title("Beamforming Map")
plt.xlabel("X Position (m)")
plt.ylabel("Y Position (m)")
# plt.show() # Show the plot
plt.show(block=True) # Ensure this plot stays open
# import acoular
# import matplotlib.pylab as plt
# from scipy.io import wavfile
# import numpy as np
# # Path to your stereo WAV file
# wav_file = './24y_10m_21d_20h_28m_13s/audio_0_trim.wav'
# # Start beamforming analysis, use WAV file directly
# ts = acoular.TimeSamples(name=wav_file)
# # Perform beamforming in the frequency domain
# ps = acoular.PowerSpectra(time_data=ts, block_size=128, window="Hanning")
# # Define microphone geometry
# mg = acoular.MicGeom(from_file="mic_array.xml")
# # Verify microphone positions
# print("Microphone positions:\n", mg.mpos)
# # Plot microphone positions
# plt.figure()
# if mg.mpos.size > 0:
# plt.plot(mg.mpos[0], mg.mpos[1], 'o')
# plt.axis('equal')
# plt.xlabel('X Position (m)')
# plt.ylabel('Y Position (m)')
# plt.title('Microphone Positions')
# plt.show(block=False)
# else:
# print("No microphone positions available.")
# # Rectangular grid for sound source localization
# rg = acoular.RectGrid(x_min=-1, x_max=1,
# y_min=-1, y_max=1,
# z=1, increment=0.005)
# # Create a steering vector and perform beamforming
# st = acoular.SteeringVector(grid=rg, mics=mg)
# bb = acoular.BeamformerBase(freq_data=ps, steer=st)
# pm = bb.synthetic(5000, 3) # Beamform at 5000 Hz
# # Convert beamforming result to decibel scale
# Lm = acoular.L_p(pm)
# # Print shape and values of the beamforming map
# print(Lm.shape)
# print(Lm)
# # Plot the beamforming map
# plt.figure()
# plt.imshow(Lm.T, origin="lower", vmin=Lm.max() - 15, extent=rg.extend())
# plt.colorbar()
# plt.title("Beamforming Map")
# plt.xlabel("X Position (m)")
# plt.ylabel("Y Position (m)")
# plt.show(block=True)