Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jean Ibarz
ml_binaural_audio
Commits
2b0dd97e
Commit
2b0dd97e
authored
Feb 10, 2021
by
Jean Ibarz
Browse files
Refactoring of split_long_wav().
parent
e83bc5cc
Changes
1
Hide whitespace changes
Inline
Side-by-side
core/utils.py
View file @
2b0dd97e
...
...
@@ -113,17 +113,9 @@ def generate_augmented_labelled_dataset(signals, labels, symmetry=True, n_augmen
# mode='valid')
augmented_signals
.
append
(
new_signal
)
augmented_labels
.
append
(
label
)
augmented_signals
=
np
.
array
(
augmented_signals
)
augmented_labels
=
np
.
array
(
augmented_labels
)
if
symmetry
is
True
:
# inverse left and right ears signals, and changes the azimuth accordingly
# e.g. if signal is coming from 45°, it comes from 315° after left/right ears signals inversion
augmented_signals2
=
np
.
flip
(
augmented_signals
.
copy
(),
axis
=
2
)
augmented_labels2
=
360
-
augmented_labels
.
copy
()
augmented_signals
=
np
.
concatenate
([
augmented_signals
,
augmented_signals2
])
augmented_labels
=
np
.
concatenate
([
augmented_labels
,
augmented_labels2
])
return
augmented_signals
,
augmented_labels
...
...
@@ -146,13 +138,15 @@ def data_generator(signal):
return
signal
def
split_long_wav
(
rootpath
,
max_
part
s
=
None
):
def
split_long_wav
(
rootpath
,
max_
chunk
s
=
None
,
chunk_length
=
1223
,
quantile
=
0.5
):
"""
Read all wav files in rootpath, and return the list of wav parts of length 40000 samples who's RMS values are
greater the median.
:param rootpath:
:param max_parts:
:param max_chunks:
:param chunk_length:
:param quantile:
:return:
"""
import
scipy.io.wavfile
...
...
@@ -160,30 +154,34 @@ def split_long_wav(rootpath, max_parts=None):
import
os
input_files
=
[
file
for
file
in
glob
.
iglob
(
os
.
path
.
join
(
rootpath
,
'*.wav'
),
recursive
=
True
)]
nb_chunks
=
0
all_data
=
[]
total_length
=
0
for
file
in
input_files
:
samplerate
,
data
=
scipy
.
io
.
wavfile
.
read
(
filename
=
file
)
all_data
.
append
((
data
/
np
.
max
(
np
.
abs
(
data
)))[:,
0
])
# keep only the left channel
total_length
+=
len
(
data
)
if
max_parts
is
not
None
and
total_length
>=
40000
*
max_parts
:
# file is stereo, keep only the left channel
if
data
.
shape
[
-
1
]
==
2
:
data
=
data
[:,
0
]
# keep only a length that is splittable into N chunks of equal length
_nb_chunks
=
len
(
data
)
//
chunk_length
if
nb_chunks
+
_nb_chunks
>
max_chunks
/
quantile
:
_nb_chunks
=
int
(
max_chunks
//
quantile
-
nb_chunks
)
data
=
data
[
0
:
_nb_chunks
*
chunk_length
]
max_abs
=
np
.
max
(
np
.
abs
(
data
))
assert
max_abs
>=
0.1
all_data
.
append
(
data
/
max_abs
)
# keep only the left channel
nb_chunks
+=
_nb_chunks
if
max_chunks
is
not
None
and
nb_chunks
>=
max_chunks
/
quantile
:
# it is only a heuristic of the max parts... in reality, the number of parts may be lower in the end
break
all_data
=
np
.
concatenate
(
all_data
,
axis
=
0
)
n
=
int
(
len
(
all_data
)
/
40000
)
# data_parts = np.array_split(ary=data, indices_or_sections=n)
data_parts
=
[]
# remove the last part if it has a different length
for
i
in
range
(
n
):
if
(
i
+
1
)
*
40000
<
len
(
all_data
):
data_parts
.
append
(
all_data
[
i
*
40000
:(
i
+
1
)
*
40000
])
data_parts
=
np
.
array
(
data_parts
)
if
len
(
data_parts
[
0
])
!=
len
(
data_parts
[
-
1
]):
data_parts
=
data_parts
[:
-
1
]
# peak_values = [np.max(np.abs(part)) for part in data_parts]
rms_values
=
[
np
.
sqrt
(
np
.
mean
(
part
**
2
))
for
part
in
data_parts
]
rms_quantile
=
np
.
quantile
(
a
=
rms_values
,
q
=
[
0.5
])
data_parts_loud_enough
=
[
part
for
i
,
part
in
enumerate
(
data_parts
)
if
rms_values
[
i
]
>=
rms_quantile
]
print
(
f
'selected
{
len
(
data_parts_loud_enough
)
}
/
{
len
(
data_parts
)
}
'
)
all_data
=
np
.
concatenate
(
all_data
)
assert
len
(
all_data
)
//
chunk_length
==
len
(
all_data
)
/
chunk_length
chunks
=
np
.
array_split
(
all_data
,
len
(
all_data
)
//
chunk_length
)
rms_values
=
[
np
.
sqrt
(
np
.
mean
(
part
**
2
))
for
part
in
chunks
]
rms_quantile
=
np
.
quantile
(
a
=
rms_values
,
q
=
[
quantile
])
data_parts_loud_enough
=
[
part
for
i
,
part
in
enumerate
(
chunks
)
if
rms_values
[
i
]
>=
rms_quantile
]
print
(
f
'selected
{
len
(
data_parts_loud_enough
)
}
/
{
len
(
chunks
)
}
'
)
return
data_parts_loud_enough
...
...
@@ -225,3 +223,10 @@ def get_gtf_kernels(gtf_dirpath):
kernels
=
np
.
flip
(
gtf_irs
,
axis
=
1
)
kernels
=
np
.
transpose
(
kernels
)
return
kernels
def
one_hot_encoding_left_or_right
(
a
,
dtype
=
'int'
):
a
=
(
a
>
180
).
astype
(
dtype
=
dtype
)
b
=
np
.
zeros
((
a
.
size
,
a
.
max
()
+
1
))
b
[
np
.
arange
(
a
.
size
),
a
]
=
1
return
b
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment