You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_bass_biquad.py 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. import numpy as np
  16. import pytest
  17. import mindspore.dataset as ds
  18. import mindspore.dataset.audio.transforms as audio
  19. from mindspore import log as logger
  20. def _count_unequal_element(data_expected, data_me, rtol, atol):
  21. assert data_expected.shape == data_me.shape
  22. total_count = len(data_expected.flatten())
  23. error = np.abs(data_expected - data_me)
  24. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  25. loss_count = np.count_nonzero(greater)
  26. assert (loss_count / total_count) < rtol, \
  27. "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \
  28. format(data_expected[greater], data_me[greater], error[greater])
  29. def test_func_bass_biquad_eager():
  30. """ mindspore eager mode normal testcase:bass_biquad op"""
  31. # Original waveform
  32. waveform = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float64)
  33. # Expect waveform
  34. expect_waveform = np.array([[0.10409035359, 0.21652136269, 0.33761211292],
  35. [0.41636141439, 0.55381438997, 0.70088436361]], dtype=np.float64)
  36. bass_biquad_op = audio.BassBiquad(44100, 50.0, 100.0, 0.707)
  37. # Filtered waveform by bassbiquad
  38. output = bass_biquad_op(waveform)
  39. _count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  40. def test_func_bass_biquad_pipeline():
  41. """ mindspore pipeline mode normal testcase:bass_biquad op"""
  42. # Original waveform
  43. waveform = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]], dtype=np.float64)
  44. # Expect waveform
  45. expect_waveform = np.array([[0.10409035359, 0.21652136269, 0.33761211292],
  46. [0.41636141439, 0.55381438997, 0.70088436361]], dtype=np.float64)
  47. label = np.random.sample((2, 1))
  48. data = (waveform, label)
  49. dataset = ds.NumpySlicesDataset(data, ["channel", "sample"], shuffle=False)
  50. bass_biquad_op = audio.BassBiquad(44100, 50, 100.0, 0.707)
  51. # Filtered waveform by bassbiquad
  52. dataset = dataset.map(
  53. input_columns=["channel"], operations=bass_biquad_op, num_parallel_workers=8)
  54. i = 0
  55. for _ in dataset.create_dict_iterator(output_numpy=True):
  56. _count_unequal_element(expect_waveform[i, :],
  57. _['channel'], 0.0001, 0.0001)
  58. i += 1
  59. def test_invalid_invalid_input():
  60. def test_invalid_input(test_name, sample_rate, gain, central_freq, Q, error, error_msg):
  61. logger.info("Test BassBiquad with bad input: {0}".format(test_name))
  62. with pytest.raises(error) as error_info:
  63. audio.BassBiquad(sample_rate, gain, central_freq, Q)
  64. assert error_msg in str(error_info.value)
  65. test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 50.0, 200, 0.707, TypeError,
  66. "Argument sample_rate with value 44100.5 is not of type [<class 'int'>],"
  67. " but got <class 'float'>.")
  68. test_invalid_input("invalid sample_rate parameter type as a String", "44100", 50.0, 200, 0.707, TypeError,
  69. "Argument sample_rate with value 44100 is not of type [<class 'int'>],"
  70. " but got <class 'str'>.")
  71. test_invalid_input("invalid gain parameter type as a String", 44100, "50.0", 200, 0.707, TypeError,
  72. "Argument gain with value 50.0 is not of type [<class 'float'>, <class 'int'>],"
  73. " but got <class 'str'>.")
  74. test_invalid_input("invalid contral_freq parameter type as a String", 44100, 50.0, "200", 0.707, TypeError,
  75. "Argument central_freq with value 200 is not of type [<class 'float'>, <class 'int'>],"
  76. " but got <class 'str'>.")
  77. test_invalid_input("invalid Q parameter type as a String", 44100, 50.0, 200, "0.707", TypeError,
  78. "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>],"
  79. " but got <class 'str'>.")
  80. test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 50.0, 200, 0.707, ValueError,
  81. "Input sample_rate is not within the required interval of [-2147483648, 2147483647].")
  82. test_invalid_input("invalid gain parameter value", 44100, 32434324324234321, 200, 0.707, ValueError,
  83. "Input gain is not within the required interval of [-16777216, 16777216].")
  84. test_invalid_input("invalid contral_freq parameter value", 44100, 50, 32434324324234321, 0.707, ValueError,
  85. "Input central_freq is not within the required interval of [-16777216, 16777216].")
  86. test_invalid_input("invalid sample_rate parameter value", None, 50.0, 200, 0.707, TypeError,
  87. "Argument sample_rate with value None is not of type [<class 'int'>], "
  88. "but got <class 'NoneType'>.")
  89. test_invalid_input("invalid gain parameter value", 44100, None, 200, 0.707, TypeError,
  90. "Argument gain with value None is not of type [<class 'float'>, <class 'int'>], "
  91. "but got <class 'NoneType'>.")
  92. test_invalid_input("invalid central_rate parameter value", 44100, 50.0, None, 0.707, TypeError,
  93. "Argument central_freq with value None is not of type [<class 'float'>, <class 'int'>],"
  94. " but got <class 'NoneType'>.")
  95. test_invalid_input("invalid sample_rate parameter value", 0, 50.0, 200, 0.707, ValueError,
  96. "Input sample_rate can not be 0.")
  97. test_invalid_input("invalid Q parameter value", 44100, 50.0, 200, 1.707, ValueError,
  98. "Input Q is not within the required interval of (0, 1].")
  99. if __name__ == '__main__':
  100. test_func_bass_biquad_eager()
  101. test_func_bass_biquad_pipeline()
  102. test_invalid_invalid_input()