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_bandpass_biquad.py 6.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
  27. data_expected[greater], data_me[greater], error[greater])
  28. def test_func_bandpass_biquad_eager():
  29. """ mindspore eager mode normal testcase:bandpass_biquad op"""
  30. # Original waveform
  31. waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
  32. # Expect waveform
  33. expect_waveform = np.array([[0.01979545, 0.07838227, 0.17417782],
  34. [0.07918181, 0.25414270, 0.46156447]], dtype=np.float64)
  35. bandpass_biquad_op = audio.BandpassBiquad(44000, 200.0, 0.707, False)
  36. # Filtered waveform by bandpassbiquad
  37. output = bandpass_biquad_op(waveform)
  38. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  39. def test_func_bandpass_biquad_pipeline():
  40. """ mindspore pipeline mode normal testcase:bandpass_biquad op"""
  41. # Original waveform
  42. waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
  43. # Expect waveform
  44. expect_waveform = np.array([[0.01979545, 0.07838227, 0.17417782],
  45. [0.07918181, 0.25414270, 0.46156447]], dtype=np.float64)
  46. label = np.random.sample((2, 1))
  47. data = (waveform, label)
  48. dataset = ds.NumpySlicesDataset(data, ["channel", "sample"], shuffle=False)
  49. bandpass_biquad_op = audio.BandpassBiquad(44000, 200.0)
  50. # Filtered waveform by bandpassbiquad
  51. dataset = dataset.map(input_columns=["channel"], operations=bandpass_biquad_op, num_parallel_workers=8)
  52. i = 0
  53. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  54. count_unequal_element(expect_waveform[i, :], item['channel'], 0.0001, 0.0001)
  55. i += 1
  56. def test_bandpass_biquad_invalid_input():
  57. def test_invalid_input(test_name, sample_rate, central_freq, Q, const_skirt_gain, error, error_msg):
  58. logger.info(
  59. "Test BandpassBiquad with bad input: {0}".format(test_name))
  60. with pytest.raises(error) as error_info:
  61. audio.BandpassBiquad(sample_rate, central_freq, Q, const_skirt_gain)
  62. assert error_msg in str(error_info.value)
  63. test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 200, 0.707, True, TypeError,
  64. "Argument sample_rate with value 44100.5 is not of type [<class 'int'>],"
  65. " but got <class 'float'>.")
  66. test_invalid_input("invalid sample_rate parameter type as a String", "44100", 200, 0.707, True, TypeError,
  67. "Argument sample_rate with value 44100 is not of type [<class 'int'>], but got <class 'str'>.")
  68. test_invalid_input("invalid contral_freq parameter type as a String", 44100, "200", 0.707, True, TypeError,
  69. "Argument central_freq with value 200 is not of type [<class 'float'>, <class 'int'>],"
  70. " but got <class 'str'>.")
  71. test_invalid_input("invalid sample_rate parameter value", 0, 200, 0.707, True, ValueError,
  72. "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
  73. test_invalid_input("invalid contral_freq parameter value", 44100, 32434324324234321, 0.707, True, ValueError,
  74. "Input central_freq is not within the required interval of [-16777216, 16777216].")
  75. test_invalid_input("invalid Q parameter type as a String", 44100, 200, "0.707", True, TypeError,
  76. "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>],"
  77. " but got <class 'str'>.")
  78. test_invalid_input("invalid Q parameter value", 44100, 200, 1.707, True, ValueError,
  79. "Input Q is not within the required interval of (0, 1].")
  80. test_invalid_input("invalid Q parameter value", 44100, 200, 0, True, ValueError,
  81. "Input Q is not within the required interval of (0, 1].")
  82. test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 200, 0.707, True, ValueError,
  83. "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
  84. test_invalid_input("invalid sample_rate parameter value", None, 200, 0.707, True, TypeError,
  85. "Argument sample_rate with value None is not of type [<class 'int'>],"
  86. " but got <class 'NoneType'>.")
  87. test_invalid_input("invalid central_rate parameter value", 44100, None, 0.707, True, TypeError,
  88. "Argument central_freq with value None is not of type [<class 'float'>, <class 'int'>],"
  89. " but got <class 'NoneType'>.")
  90. test_invalid_input("invalid const_skirt_gain parameter type as a String", 44100, 200, 0.707, "False", TypeError,
  91. "Argument const_skirt_gain with value False is not of type [<class 'bool'>], " +
  92. "but got <class 'str'>.")
  93. if __name__ == "__main__":
  94. test_func_bandpass_biquad_eager()
  95. test_func_bandpass_biquad_pipeline()
  96. test_bandpass_biquad_invalid_input()