From 8f63b2be5809ae68ff89747429d3d32433a54931 Mon Sep 17 00:00:00 2001 From: SuperKogito Date: Tue, 28 Jan 2020 16:52:25 +0100 Subject: [PATCH] add remove_silence test and update tests --- tests/test_utils_preprocessing.py | 55 ++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/tests/test_utils_preprocessing.py b/tests/test_utils_preprocessing.py index 27db6b7..d473172 100644 --- a/tests/test_utils_preprocessing.py +++ b/tests/test_utils_preprocessing.py @@ -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(): @@ -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)]) @@ -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]]]) @@ -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