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_detect_pitch_frequency.py 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_detect_pitch_frequency_eager():
  30. """ mindspore eager mode normal testcase:detect_pitch_frequency op"""
  31. # Original waveform
  32. waveform = np.array([[2.716064453125e-03, 6.34765625e-03, 9.246826171875e-03, 1.0894775390625e-02,
  33. 1.1383056640625e-02, 1.1566162109375e-02, 1.3946533203125e-02, 1.55029296875e-02,
  34. 1.6143798828125e-02, 1.8402099609375e-02],
  35. [1.7181396484375e-02, 1.59912109375e-02, 1.64794921875e-02, 1.5106201171875e-02,
  36. 1.385498046875e-02, 1.3458251953125e-02, 1.4190673828125e-02, 1.2847900390625e-02,
  37. 1.0528564453125e-02, 9.368896484375e-03]], dtype=np.float64)
  38. # Expect waveform
  39. expect_waveform = np.array(
  40. [[10., 10., 10.], [5., 5., 10.]], dtype=np.float64)
  41. detect_pitch_frequency_op = audio.DetectPitchFrequency(30, 0.1, 3, 5, 25)
  42. # Detect pitch frequence
  43. output = detect_pitch_frequency_op(waveform)
  44. count_unequal_element(expect_waveform, output, 0.0001, 0.0001)
  45. def test_detect_pitch_frequency_pipeline():
  46. """ mindspore pipeline mode normal testcase:detect_pitch_frequency op"""
  47. # Original waveform
  48. waveform = np.array([[0.716064453125e-03, 5.34765625e-03, 6.246826171875e-03, 2.0894775390625e-02,
  49. 7.1383056640625e-02], [4.1566162109375e-02, 1.3946533203125e-02, 3.55029296875e-02,
  50. 0.6143798828125e-02, 3.8402099609375e-02]], dtype=np.float64)
  51. # Expect waveform
  52. expect_waveform = np.array([[10.0000], [7.5000]], dtype=np.float64)
  53. dataset = ds.NumpySlicesDataset(waveform, ["audio"], shuffle=False)
  54. detect_pitch_frequency_op = audio.DetectPitchFrequency(30, 0.1, 3, 5, 25)
  55. # Detect pitch frequence
  56. dataset = dataset.map(input_columns=["audio"],
  57. operations=detect_pitch_frequency_op, num_parallel_workers=8)
  58. i = 0
  59. for item in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  60. count_unequal_element(expect_waveform[i, :],
  61. item['audio'], 0.0001, 0.0001)
  62. i += 1
  63. def test_detect_pitch_frequency_invalid_input():
  64. def test_invalid_input(test_name, sample_rate, frame_time, win_length, freq_low, freq_high, error, error_msg):
  65. logger.info(
  66. "Test DetectPitchFrequency with bad input: {0}".format(test_name))
  67. with pytest.raises(error) as error_info:
  68. audio.DetectPitchFrequency(
  69. sample_rate, frame_time, win_length, freq_low, freq_high)
  70. assert error_msg in str(error_info.value)
  71. test_invalid_input("invalid sample_rate parameter type as a float", 44100.5, 0.01, 30, 85, 3400, 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", 0.01, 30, 85, 3400, TypeError,
  75. "Argument sample_rate with value 44100 is not of type [<class 'int'>], but got <class 'str'>.")
  76. test_invalid_input("invalid frame_time parameter type as a String", 44100, "0.01", 30, 85, 3400, TypeError,
  77. "Argument frame_time with value 0.01 is not of type [<class 'float'>, <class 'int'>],"
  78. " but got <class 'str'>.")
  79. test_invalid_input("invalid win_length parameter type as a float", 44100, 0.01, 30.1, 85, 3400, TypeError,
  80. "Argument win_length with value 30.1 is not of type [<class 'int'>], but got <class 'float'>.")
  81. test_invalid_input("invalid win_length parameter type as a String", 44100, 0.01, "30", 85, 3400, TypeError,
  82. "Argument win_length with value 30 is not of type [<class 'int'>], but got <class 'str'>.")
  83. test_invalid_input("invalid freq_low parameter type as a String", 44100, 0.01, 30, "85", 3400, TypeError,
  84. "Argument freq_low with value 85 is not of type [<class 'int'>, <class 'float'>],"
  85. " but got <class 'str'>.")
  86. test_invalid_input("invalid freq_high parameter type as a String", 44100, 0.01, 30, 85, "3400", TypeError,
  87. "Argument freq_high with value 3400 is not of type [<class 'int'>, <class 'float'>],"
  88. " but got <class 'str'>.")
  89. test_invalid_input("invalid sample_rate parameter value", 0, 0.01, 30, 85, 3400, ValueError,
  90. "Input sample_rate is not within the required interval of [-2147483648, 0) and (0, 2147483647].")
  91. test_invalid_input("invalid frame_time parameter value", 44100, 0, 30, 85, 3400, ValueError,
  92. "Input frame_time is not within the required interval of (0, 16777216].")
  93. test_invalid_input("invalid win_length parameter value", 44100, 0.01, 0, 85, 3400, ValueError,
  94. "Input win_length is not within the required interval of [1, 2147483647].")
  95. test_invalid_input("invalid freq_low parameter value", 44100, 0.01, 30, 0, 3400, ValueError,
  96. "Input freq_low is not within the required interval of (0, 16777216].")
  97. test_invalid_input("invalid freq_high parameter value", 44100, 0.01, 30, 85, 0, ValueError,
  98. "Input freq_high is not within the required interval of (0, 16777216].")
  99. if __name__ == "__main__":
  100. test_detect_pitch_frequency_eager()
  101. test_detect_pitch_frequency_pipeline()
  102. test_detect_pitch_frequency_invalid_input()