janitor

janitor
git clone https://git.prime8.dev/janitor.git
Log | Files | Refs | README | LICENSE

test_frame_extraction_options.py (2802B)


      1 #!/usr/bin/env python3
      2 #
      3 # Copyright (c)  2022  Xiaomi Corporation (authors: Fangjun Kuang)
      4 
      5 import pickle
      6 
      7 import kaldi_native_fbank as knf
      8 
      9 
     10 def test_default():
     11     opts = knf.FrameExtractionOptions()
     12     assert opts.samp_freq == 16000
     13     assert opts.frame_shift_ms == 10.0
     14     assert opts.frame_length_ms == 25.0
     15     assert abs(opts.dither - 0.00003) < 1e-6
     16     assert abs(opts.preemph_coeff - 0.97) < 1e-6
     17     assert opts.remove_dc_offset is True
     18     assert opts.window_type == "povey"
     19     assert opts.round_to_power_of_two is True
     20     assert abs(opts.blackman_coeff - 0.42) < 1e-6
     21     assert opts.snip_edges is True
     22 
     23 
     24 def test_set_get():
     25     opts = knf.FrameExtractionOptions()
     26     opts.samp_freq = 44100
     27     assert opts.samp_freq == 44100
     28 
     29     opts.frame_shift_ms = 20.5
     30     assert opts.frame_shift_ms == 20.5
     31 
     32     opts.frame_length_ms = 1
     33     assert opts.frame_length_ms == 1
     34 
     35     opts.dither = 0.5
     36     assert opts.dither == 0.5
     37 
     38     opts.preemph_coeff = 0.25
     39     assert opts.preemph_coeff == 0.25
     40 
     41     opts.remove_dc_offset = False
     42     assert opts.remove_dc_offset is False
     43 
     44     opts.window_type = "hanning"
     45     assert opts.window_type == "hanning"
     46 
     47     opts.round_to_power_of_two = False
     48     assert opts.round_to_power_of_two is False
     49 
     50     opts.blackman_coeff = 0.25
     51     assert opts.blackman_coeff == 0.25
     52 
     53     opts.snip_edges = False
     54     assert opts.snip_edges is False
     55 
     56 
     57 def test_from_empty_dict():
     58     opts = knf.FrameExtractionOptions.from_dict({})
     59     opts2 = knf.FrameExtractionOptions()
     60 
     61     assert str(opts) == str(opts2)
     62 
     63 
     64 def test_from_dict_partial():
     65     d = {"samp_freq": 10, "frame_shift_ms": 2}
     66 
     67     opts = knf.FrameExtractionOptions.from_dict(d)
     68 
     69     opts2 = knf.FrameExtractionOptions()
     70     assert str(opts) != str(opts2)
     71 
     72     opts2.samp_freq = 10
     73     assert str(opts) != str(opts2)
     74 
     75     opts2.frame_shift_ms = 2
     76     assert str(opts) == str(opts2)
     77 
     78     opts2.frame_shift_ms = 3
     79     assert str(opts) != str(opts2)
     80 
     81 
     82 def test_from_dict_full_and_as_dict():
     83     opts = knf.FrameExtractionOptions()
     84     opts.samp_freq = 20
     85     opts.frame_length_ms = 100
     86 
     87     d = opts.as_dict()
     88     for key, value in d.items():
     89         assert value == getattr(opts, key)
     90 
     91     opts2 = knf.FrameExtractionOptions.from_dict(d)
     92     assert str(opts2) == str(opts)
     93 
     94     d["window_type"] = "hanning"
     95     opts3 = knf.FrameExtractionOptions.from_dict(d)
     96     assert opts3.window_type == "hanning"
     97 
     98 
     99 def test_pickle():
    100     opts = knf.FrameExtractionOptions()
    101     opts.samp_freq = 44100
    102     opts.dither = 5.5
    103     data = pickle.dumps(opts)
    104 
    105     opts2 = pickle.loads(data)
    106     assert str(opts) == str(opts2)
    107 
    108 
    109 def main():
    110     test_default()
    111     test_set_get()
    112     test_from_empty_dict()
    113     test_from_dict_partial()
    114     test_from_dict_full_and_as_dict()
    115     test_pickle()
    116 
    117 
    118 if __name__ == "__main__":
    119     main()