janitor

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

test_fbank_options.py (5217B)


      1 #!/usr/bin/env python3
      2 #
      3 # Copyright (c)  2021  Xiaomi Corporation (authors: Fangjun Kuang)
      4 
      5 
      6 import pickle
      7 
      8 import kaldi_native_fbank as knf
      9 
     10 
     11 def test_default():
     12     opts = knf.FbankOptions()
     13     assert opts.frame_opts.samp_freq == 16000
     14     assert opts.frame_opts.frame_shift_ms == 10.0
     15     assert opts.frame_opts.frame_length_ms == 25.0
     16     assert abs(opts.frame_opts.dither - 0.00003) < 1e-6
     17     assert abs(opts.frame_opts.preemph_coeff - 0.97) < 1e-6
     18     assert opts.frame_opts.remove_dc_offset is True
     19     assert opts.frame_opts.window_type == "povey"
     20     assert opts.frame_opts.round_to_power_of_two is True
     21     assert abs(opts.frame_opts.blackman_coeff - 0.42) < 1e-6
     22     assert opts.frame_opts.snip_edges is True
     23 
     24     assert opts.mel_opts.num_bins == 23
     25     assert opts.mel_opts.low_freq == 20
     26     assert opts.mel_opts.high_freq == 0
     27     assert opts.mel_opts.vtln_low == 100
     28     assert opts.mel_opts.vtln_high == -500
     29     assert opts.mel_opts.debug_mel is False
     30     assert opts.mel_opts.htk_mode is False
     31 
     32     assert opts.use_energy is False
     33     assert opts.energy_floor == 0.0
     34     assert opts.raw_energy is True
     35     assert opts.htk_compat is False
     36     assert opts.use_log_fbank is True
     37     assert opts.use_power is True
     38 
     39 
     40 def test_set_get():
     41     opts = knf.FbankOptions()
     42     opts.use_energy = True
     43     assert opts.use_energy is True
     44 
     45     opts.energy_floor = 1
     46     assert opts.energy_floor == 1
     47 
     48     opts.raw_energy = False
     49     assert opts.raw_energy is False
     50 
     51     opts.htk_compat = True
     52     assert opts.htk_compat is True
     53 
     54     opts.use_log_fbank = False
     55     assert opts.use_log_fbank is False
     56 
     57     opts.use_power = False
     58     assert opts.use_power is False
     59 
     60 
     61 def test_set_get_frame_opts():
     62     opts = knf.FbankOptions()
     63 
     64     opts.frame_opts.samp_freq = 44100
     65     assert opts.frame_opts.samp_freq == 44100
     66 
     67     opts.frame_opts.frame_shift_ms = 20.5
     68     assert opts.frame_opts.frame_shift_ms == 20.5
     69 
     70     opts.frame_opts.frame_length_ms = 1
     71     assert opts.frame_opts.frame_length_ms == 1
     72 
     73     opts.frame_opts.dither = 0.5
     74     assert opts.frame_opts.dither == 0.5
     75 
     76     opts.frame_opts.preemph_coeff = 0.25
     77     assert opts.frame_opts.preemph_coeff == 0.25
     78 
     79     opts.frame_opts.remove_dc_offset = False
     80     assert opts.frame_opts.remove_dc_offset is False
     81 
     82     opts.frame_opts.window_type = "hanning"
     83     assert opts.frame_opts.window_type == "hanning"
     84 
     85     opts.frame_opts.round_to_power_of_two = False
     86     assert opts.frame_opts.round_to_power_of_two is False
     87 
     88     opts.frame_opts.blackman_coeff = 0.25
     89     assert opts.frame_opts.blackman_coeff == 0.25
     90 
     91     opts.frame_opts.snip_edges = False
     92     assert opts.frame_opts.snip_edges is False
     93 
     94 
     95 def test_set_get_mel_opts():
     96     opts = knf.FbankOptions()
     97 
     98     opts.mel_opts.num_bins = 100
     99     assert opts.mel_opts.num_bins == 100
    100 
    101     opts.mel_opts.low_freq = 22
    102     assert opts.mel_opts.low_freq == 22
    103 
    104     opts.mel_opts.high_freq = 1
    105     assert opts.mel_opts.high_freq == 1
    106 
    107     opts.mel_opts.vtln_low = 101
    108     assert opts.mel_opts.vtln_low == 101
    109 
    110     opts.mel_opts.vtln_high = -100
    111     assert opts.mel_opts.vtln_high == -100
    112 
    113     opts.mel_opts.debug_mel = True
    114     assert opts.mel_opts.debug_mel is True
    115 
    116     opts.mel_opts.htk_mode = True
    117     assert opts.mel_opts.htk_mode is True
    118 
    119 
    120 def test_from_empty_dict():
    121     opts = knf.FbankOptions.from_dict({})
    122     opts2 = knf.FbankOptions()
    123 
    124     assert str(opts) == str(opts2)
    125 
    126 
    127 def test_from_dict_partial():
    128     d = {
    129         "energy_floor": 10.5,
    130         "htk_compat": True,
    131         "mel_opts": {"num_bins": 80, "vtln_low": 1},
    132         "frame_opts": {"window_type": "hanning"},
    133     }
    134     opts = knf.FbankOptions.from_dict(d)
    135     assert opts.energy_floor == 10.5
    136     assert opts.htk_compat is True
    137     assert opts.mel_opts.num_bins == 80
    138     assert opts.mel_opts.vtln_low == 1
    139     assert opts.frame_opts.window_type == "hanning"
    140 
    141     mel_opts = knf.MelBanksOptions.from_dict(d["mel_opts"])
    142     assert str(opts.mel_opts) == str(mel_opts)
    143 
    144 
    145 def test_from_dict_full_and_as_dict():
    146     opts = knf.FbankOptions()
    147     opts.htk_compat = True
    148     opts.mel_opts.num_bins = 80
    149     opts.frame_opts.samp_freq = 10
    150 
    151     d = opts.as_dict()
    152     assert d["htk_compat"] is True
    153     assert d["mel_opts"]["num_bins"] == 80
    154     assert d["frame_opts"]["samp_freq"] == 10
    155 
    156     mel_opts = knf.MelBanksOptions()
    157     mel_opts.num_bins = 80
    158     assert d["mel_opts"] == mel_opts.as_dict()
    159 
    160     frame_opts = knf.FrameExtractionOptions()
    161     frame_opts.samp_freq = 10
    162     assert d["frame_opts"] == frame_opts.as_dict()
    163 
    164     opts2 = knf.FbankOptions.from_dict(d)
    165     assert str(opts2) == str(opts)
    166 
    167     d["htk_compat"] = False
    168     opts3 = knf.FbankOptions.from_dict(d)
    169     assert opts3.htk_compat is False
    170 
    171 
    172 def test_pickle():
    173     opts = knf.FbankOptions()
    174     opts.use_energy = True
    175     opts.use_power = False
    176 
    177     opts.frame_opts.samp_freq = 44100
    178     opts.mel_opts.num_bins = 100
    179 
    180     data = pickle.dumps(opts)
    181 
    182     opts2 = pickle.loads(data)
    183     assert str(opts) == str(opts2)
    184 
    185 
    186 def main():
    187     test_default()
    188     test_set_get()
    189     test_set_get_frame_opts()
    190     test_set_get_mel_opts()
    191     test_from_empty_dict()
    192     test_from_dict_partial()
    193     test_from_dict_full_and_as_dict()
    194     test_pickle()
    195 
    196 
    197 if __name__ == "__main__":
    198     main()