test_frame_extraction_options.py 2.7 KB

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