Skip to content

Commit

Permalink
add remove_silence test and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperKogito committed Jan 28, 2020
1 parent 77931db commit 8f63b2b
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions tests/test_utils_preprocessing.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import spafe
import pytest
import numpy as np
from spafe.utils.exceptions import assert_function_availability
from spafe.utils.exceptions import ParameterError, assert_function_availability
from spafe.utils.preprocessing import (zero_handling, pre_emphasis, framing,
windowing)
windowing, remove_silence)


def test_functions_availability():
Expand All @@ -15,6 +15,8 @@ def test_functions_availability():
assert_function_availability(hasattr(spafe.utils.preprocessing, 'framing'))
assert_function_availability(
hasattr(spafe.utils.preprocessing, 'windowing'))
assert_function_availability(
hasattr(spafe.utils.preprocessing, 'remove_silence'))


@pytest.mark.parametrize('x', [np.arange(4)])
Expand All @@ -39,25 +41,35 @@ def test_pre_emphasis(sig, pre_emph_coeff):
precomputed_result, 3)


@pytest.mark.parametrize('sig', [np.arange(16 * 3)])
@pytest.mark.parametrize('sig', [np.arange(16000 * 3)]) # 3 seconds
@pytest.mark.parametrize('fs', [16000])
@pytest.mark.parametrize('win_len', [0.001])
@pytest.mark.parametrize('win_hop', [0.001, 0.0005])
@pytest.mark.parametrize('win_len', [1, 2, 0.025, 0.030])
@pytest.mark.parametrize('win_hop', [1, 1, 0.025, 0.015])
def test_framing(sig, fs, win_len, win_hop):
"""
test if overlapped frames are correctly generated.
"""
frames, frames_length = framing(sig=sig,
fs=fs,
win_len=win_len,
win_hop=win_hop)
# check frames shape
if not frames.shape == (2 * int(win_len / win_hop), 16):
raise AssertionError
if win_len < win_hop:
# check error for number of filters is smaller than number of cepstrums
with pytest.raises(ParameterError):
frames, frame_length = framing(sig=sig,
fs=fs,
win_len=win_len,
win_hop=win_hop)

# check frames length
if not frames_length == win_len * fs:
raise AssertionError
else:
frames, frame_length = framing(sig=sig,
fs=fs,
win_len=win_len,
win_hop=win_hop)

# check frames length
if not frame_length == win_len * fs:
raise AssertionError

# check signal was not cropping
if len(sig) > len(frames)*frame_length:
raise AssertionError


@pytest.mark.parametrize('frames', [[[7, 8, 9], [1, 2, 3]]])
Expand All @@ -71,3 +83,16 @@ def test_windowing(frames, frame_len, win_type):
for window in windows:
if not window[0] < 0.0 and window[-1] < 0.0:
raise AssertionError


@pytest.mark.parametrize('sig', [np.arange(16000 * 3)])
@pytest.mark.parametrize('fs', [16000])
@pytest.mark.parametrize('win_len', [0.001])
@pytest.mark.parametrize('win_hop', [0.001, 0.0005])
@pytest.mark.parametrize('threshold', [-35, -45])
def test_remove_silence(sig, fs, win_len, win_hop, threshold):
# get voiced frames
no_silence_sig = remove_silence(sig, fs)
print("LENGHTS: ", len(no_silence_sig), len(sig))
if not len(sig) >= len(no_silence_sig):
raise AssertionError

0 comments on commit 8f63b2b

Please sign in to comment.