11import  spafe 
22import  pytest 
33import  numpy  as  np 
4- from  spafe .utils .exceptions  import  assert_function_availability 
4+ from  spafe .utils .exceptions  import  ParameterError ,  assert_function_availability 
55from  spafe .utils .preprocessing  import  (zero_handling , pre_emphasis , framing ,
6-                                        windowing )
6+                                        windowing ,  remove_silence )
77
88
99def  test_functions_availability ():
@@ -15,6 +15,8 @@ def test_functions_availability():
1515    assert_function_availability (hasattr (spafe .utils .preprocessing , 'framing' ))
1616    assert_function_availability (
1717        hasattr (spafe .utils .preprocessing , 'windowing' ))
18+     assert_function_availability (
19+         hasattr (spafe .utils .preprocessing , 'remove_silence' ))
1820
1921
2022@pytest .mark .parametrize ('x' , [np .arange (4 )]) 
@@ -39,25 +41,35 @@ def test_pre_emphasis(sig, pre_emph_coeff):
3941        precomputed_result , 3 )
4042
4143
42- @pytest .mark .parametrize ('sig' , [np .arange (16  *  3 )]) 
44+ @pytest .mark .parametrize ('sig' , [np .arange (16000  *  3 )])  # 3 seconds  
4345@pytest .mark .parametrize ('fs' , [16000 ]) 
44- @pytest .mark .parametrize ('win_len' , [0.001 ]) 
45- @pytest .mark .parametrize ('win_hop' , [0.001 , 0.0005  ]) 
46+ @pytest .mark .parametrize ('win_len' , [1 ,  2 ,  0.025 ,  0.030 ]) 
47+ @pytest .mark .parametrize ('win_hop' , [1 ,  1 ,  0.025 , 0.015  ]) 
4648def  test_framing (sig , fs , win_len , win_hop ):
4749    """ 
4850    test if overlapped frames are correctly generated. 
4951    """ 
50-     frames ,  frames_length   =   framing ( sig = sig , 
51-                                      fs = fs , 
52-                                      win_len = win_len , 
53-                                      win_hop = win_hop ) 
54-     # check frames shape 
55-     if   not   frames . shape   ==  ( 2   *   int ( win_len   /   win_hop ),  16 ): 
56-         raise   AssertionError 
52+     if   win_len   <   win_hop : 
53+         # check error for number of filters is smaller than number of cepstrums 
54+         with   pytest . raises ( ParameterError ): 
55+             frames ,  frame_length   =   framing ( sig = sig , 
56+                                             fs = fs , 
57+                                             win_len = win_len , 
58+                                             win_hop = win_hop ) 
5759
58-     # check frames length 
59-     if  not  frames_length  ==  win_len  *  fs :
60-         raise  AssertionError 
60+     else :
61+         frames , frame_length  =  framing (sig = sig ,
62+                                        fs = fs ,
63+                                        win_len = win_len ,
64+                                        win_hop = win_hop )
65+ 
66+         # check frames length 
67+         if  not  frame_length  ==  win_len  *  fs :
68+             raise  AssertionError 
69+ 
70+         # check signal was not cropping 
71+         if  len (sig ) >  len (frames )* frame_length :
72+             raise  AssertionError 
6173
6274
6375@pytest .mark .parametrize ('frames' , [[[7 , 8 , 9 ], [1 , 2 , 3 ]]]) 
@@ -71,3 +83,16 @@ def test_windowing(frames, frame_len, win_type):
7183    for  window  in  windows :
7284        if  not  window [0 ] <  0.0  and  window [- 1 ] <  0.0 :
7385            raise  AssertionError 
86+ 
87+ 
88+ @pytest .mark .parametrize ('sig' , [np .arange (16000  *  3 )]) 
89+ @pytest .mark .parametrize ('fs' , [16000 ]) 
90+ @pytest .mark .parametrize ('win_len' , [0.001 ]) 
91+ @pytest .mark .parametrize ('win_hop' , [0.001 , 0.0005 ]) 
92+ @pytest .mark .parametrize ('threshold' , [- 35 , - 45 ]) 
93+ def  test_remove_silence (sig , fs , win_len , win_hop , threshold ):
94+     # get voiced frames 
95+     no_silence_sig  =  remove_silence (sig , fs )
96+     print ("LENGHTS: " , len (no_silence_sig ), len (sig ))
97+     if  not  len (sig ) >=  len (no_silence_sig ):
98+         raise  AssertionError 
0 commit comments