Skip to content

Commit

Permalink
post_processing
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkim-kaist committed Dec 10, 2018
1 parent 4a343b0 commit 926b68c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 4 deletions.
72 changes: 70 additions & 2 deletions lib/matlab_py/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ def mrcg_extract(audio_dir) :
return data_len, winlen, winstep


def vad_func(audio_dir, mode, th, output_type, is_default):
def vad_func(audio_dir, mode, th, output_type, is_default, off_on_length=20, on_off_length=20, hang_before=10,
hang_over=10):

os.system('rm -rf result')
os.system('rm -rf sample_data')
Expand All @@ -180,7 +181,74 @@ def vad_func(audio_dir, mode, th, output_type, is_default):
pp = pred_result['pred']
result = np.zeros([len(pp), 1])
result = th_classifier(pp, th)
if output_type == 1 :
result = vad_post(result, off_on_length, on_off_length, hang_before, hang_over)
if output_type == 1:
result = frame2rawlabel(result, winlen, winstep)

return result


def vad_post(post_label, off_on_length=20, on_off_length=20, hang_before=10, hang_over=10):
# plt.subplot(4,1,1)
# plt.plot(post_label)

'''fill 1 to short valley'''
offset = False
onset = False
for i in range(post_label.shape[0]):

if i < post_label.shape[0] - 1:
if post_label[i] == 1 and post_label[i+1] == 0: # offset detection
offset = True
offset_point = i

if post_label[i] == 0 and post_label[i+1] == 1 and offset: # offset -> onset detection

if i - offset_point < off_on_length:
post_label[offset_point:i+1, :] = 1 # fill 1 to valley
offset = False

'''remove impulse like detection'''
# plt.subplot(4,1,2)
# plt.plot(post_label)
post_label = np.concatenate([np.zeros((1, 1)), post_label], axis=0)

for i in range(post_label.shape[0]):

if i < post_label.shape[0] - 1:
if post_label[i] == 0 and post_label[i + 1] == 1: # onset detection
onset = True
onset_point = i

if post_label[i] == 1 and post_label[i + 1] == 0 and onset: # onset -> offset detection

if i - onset_point < on_off_length:
post_label[onset_point:i + 1, :] = 0 # fill 0 to hill
onset = False

post_label = post_label[1:]

'''hang before & over'''

for i in range(post_label.shape[0]):

if i < post_label.shape[0] - 1:
if post_label[i] == 0 and post_label[i + 1] == 1: # onset detection
onset = True

if i - hang_before < 0:
post_label[0:i + 1] = 1
else:
post_label[i-hang_before:i + 1] = 1

if post_label[i] == 1 and post_label[i + 1] == 0 and onset: # onset -> offset detection

onset = False
print(i)
if i + hang_over > post_label.shape[0]:
post_label[i:, :] = 1

else:
post_label[i:i+hang_over, :] = 1

return post_label
6 changes: 4 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

audio_dir = './data/example/clean_speech.wav'

mode = 3
mode = 0
th = 0.4

output_type = 1
is_default = 1

result = utils.vad_func(audio_dir, mode, th, output_type, is_default)
result = utils.vad_func(audio_dir, mode, th, output_type, is_default, off_on_length=20, on_off_length=20,
hang_before=20, hang_over=20)
s, audio_sr = librosa.load(audio_dir, sr=16000)

t_max = np.minimum(s.shape[0], result.shape[0])
Expand All @@ -23,3 +24,4 @@
result = np.multiply(result, 0.3)
plt.plot(t, s[0:t_max], 'b')
plt.plot(t, result, 'g')
plt.show()
Binary file modified result/label.mat
Binary file not shown.
Binary file modified result/pred.mat
Binary file not shown.
Binary file modified sample_data/normalize_factor.mat
Binary file not shown.

0 comments on commit 926b68c

Please sign in to comment.