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_highpass_biquad.py 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. """
  16. Testing HighpassBiquad op in DE
  17. """
  18. import numpy as np
  19. import pytest
  20. import mindspore.dataset as ds
  21. import mindspore.dataset.audio.transforms as audio
  22. from mindspore import log as logger
  23. def count_unequal_element(data_expected, data_me, rtol, atol):
  24. assert data_expected.shape == data_me.shape
  25. total_count = len(data_expected.flatten())
  26. error = np.abs(data_expected - data_me)
  27. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  28. loss_count = np.count_nonzero(greater)
  29. assert (loss_count / total_count) < rtol, \
  30. "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}". \
  31. format(data_expected[greater], data_me[greater], error[greater])
  32. def test_highpass_biquad_eager():
  33. """ mindspore eager mode normal testcase:highpass_biquad op"""
  34. # Original waveform
  35. waveform = np.array([[0.8236, 0.2049, 0.3335], [0.5933, 0.9911, 0.2482],
  36. [0.3007, 0.9054, 0.7598], [0.5394, 0.2842, 0.5634], [0.6363, 0.2226, 0.2288]])
  37. # Expect waveform
  38. expect_waveform = np.array([[0.2745, -0.4808, 0.1576], [0.1978, -0.0652, -0.4462],
  39. [0.1002, 0.1013, -0.2835], [0.1798, -0.2649, 0.1182], [0.2121, -0.3500, 0.0693]])
  40. highpass_biquad_op = audio.HighpassBiquad(4000, 1000.0, 1)
  41. # Filtered waveform by highpass_biquad
  42. output = highpass_biquad_op(waveform)
  43. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  44. def test_highpass_biquad_pipeline():
  45. """ mindspore pipeline mode normal testcase:highpass_biquad op"""
  46. # Original waveform
  47. waveform = np.array([[0.4063, 0.7729, 0.2325], [0.2687, 0.1426, 0.8987],
  48. [0.6914, 0.6681, 0.1783], [0.2704, 0.2680, 0.7975], [0.5880, 0.1776, 0.6323]])
  49. # Expect waveform
  50. expect_waveform = np.array([[0.1354, -0.0133, -0.3474], [0.0896, -0.1316, 0.2642],
  51. [0.2305, -0.2382, -0.2323], [0.0901, -0.0909, 0.1473], [0.1960, -0.3328, 0.2230]])
  52. dataset = ds.NumpySlicesDataset(waveform, ["col1"], shuffle=False)
  53. highpass_biquad_op = audio.HighpassBiquad(4000, 1000.0, 1)
  54. # Filtered waveform by highpass_biquad
  55. dataset = dataset.map(
  56. input_columns=["col1"], operations=highpass_biquad_op, num_parallel_workers=4)
  57. i = 0
  58. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  59. count_unequal_element(expect_waveform[i, :],
  60. item["col1"], 0.0001, 0.0001)
  61. i += 1
  62. def test_highpass_biquad_invalid_input():
  63. """
  64. Test invalid input of HighpassBiquad
  65. """
  66. def test_invalid_input(test_name, sample_rate, cutoff_freq, Q, error, error_msg):
  67. logger.info("Test HighpassBiquad with bad input: {0}".format(test_name))
  68. with pytest.raises(error) as error_info:
  69. audio.HighpassBiquad(sample_rate, cutoff_freq, Q)
  70. assert error_msg in str(error_info.value)
  71. test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 1000, 0.707, TypeError,
  72. "Argument sample_rate with value 44100.5 is not of type [<class 'int'>],"
  73. " but got <class 'float'>.")
  74. test_invalid_input("invalid sample_rate parameter type as a String", "44100", 1000, 0.707, TypeError,
  75. "Argument sample_rate with value 44100 is not of type [<class 'int'>],"
  76. " but got <class 'str'>.")
  77. test_invalid_input("invalid cutoff_freq parameter type as a String", 44100, "1000", 0.707, TypeError,
  78. "Argument cutoff_freq with value 1000 is not of type [<class 'float'>, <class 'int'>],"
  79. " but got <class 'str'>.")
  80. test_invalid_input("invalid Q parameter type as a String", 44100, 1000, "0.707", TypeError,
  81. "Argument Q with value 0.707 is not of type [<class 'float'>, <class 'int'>],"
  82. " but got <class 'str'>.")
  83. test_invalid_input("invalid sample_rate parameter value", 441324343243242342345300, 1000, 0.707, ValueError,
  84. "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
  85. test_invalid_input("invalid cutoff_freq parameter value", 44100, 32434324324234321, 0.707, ValueError,
  86. "Input cutoff_freq is not within the required interval of [-16777216, 16777216].")
  87. test_invalid_input("invalid sample_rate parameter value", None, 1000, 0.707, TypeError,
  88. "Argument sample_rate with value None is not of type [<class 'int'>], "
  89. "but got <class 'NoneType'>.")
  90. test_invalid_input("invalid cutoff_rate parameter value", 44100, None, 0.707, TypeError,
  91. "Argument cutoff_freq with value None is not of type [<class 'float'>, <class 'int'>],"
  92. " but got <class 'NoneType'>.")
  93. test_invalid_input("invalid sample_rate parameter value", 0, 1000, 0.707, ValueError,
  94. "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
  95. test_invalid_input("invalid Q parameter value", 44100, 1000, 0, ValueError,
  96. "Input Q is not within the required interval of (0, 1].")
  97. if __name__ == "__main__":
  98. test_highpass_biquad_eager()
  99. test_highpass_biquad_pipeline()
  100. test_highpass_biquad_invalid_input()