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_riaa_biquad.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 RiaaBiquad 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, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
  30. data_expected[greater], data_me[greater], error[greater])
  31. def test_riaa_biquad_eager():
  32. """ mindspore eager mode normal testcase:riaa_biquad op"""
  33. # Original waveform
  34. waveform = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
  35. # Expect waveform
  36. expect_waveform = np.array([[0.23806122, 0.70914434, 1.],
  37. [0.95224489, 1., 1.]], dtype=np.float64)
  38. riaa_biquad_op = audio.RiaaBiquad(44100)
  39. # Filtered waveform by riaabiquad
  40. output = riaa_biquad_op(waveform)
  41. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  42. def test_riaa_biquad_pipeline():
  43. """ mindspore pipeline mode normal testcase:riaa_biquad op"""
  44. # Original waveform
  45. waveform = np.array([[1.47, 4.722, 5.863], [0.492, 0.235, 0.56]], dtype=np.float32)
  46. # Expect waveform
  47. expect_waveform = np.array([[0.18626465, 0.7859906, 1.],
  48. [0.06234163, 0.09258664, 0.15710703]], dtype=np.float64)
  49. dataset = ds.NumpySlicesDataset(waveform, ["waveform"], shuffle=False)
  50. riaa_biquad_op = audio.RiaaBiquad(88200)
  51. # Filtered waveform by riaabiquad
  52. dataset = dataset.map(input_columns=["waveform"], operations=riaa_biquad_op)
  53. i = 0
  54. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  55. count_unequal_element(expect_waveform[i, :], item['waveform'], 0.0001, 0.0001)
  56. i += 1
  57. def test_riaa_biquad_invalid_parameter():
  58. def test_invalid_input(test_name, sample_rate, error, error_msg):
  59. logger.info("Test RiaaBiquad with bad input: {0}".format(test_name))
  60. with pytest.raises(error) as error_info:
  61. audio.RiaaBiquad(sample_rate)
  62. assert error_msg in str(error_info.value)
  63. test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 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", TypeError,
  67. "Argument sample_rate with value 44100 is not of type [<class 'int'>],"
  68. + " but got <class 'str'>.")
  69. test_invalid_input("invalid sample_rate parameter value", 45670, ValueError,
  70. "sample_rate should be one of [44100, 48000, 88200, 96000], but got 45670.")
  71. if __name__ == "__main__":
  72. test_riaa_biquad_eager()
  73. test_riaa_biquad_pipeline()
  74. test_riaa_biquad_invalid_parameter()