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_spectral_centroid.py 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 SpectralCentroid Python API
  17. """
  18. import numpy as np
  19. import mindspore.dataset as ds
  20. import mindspore.dataset.audio.transforms as audio
  21. from mindspore import log as logger
  22. def count_unequal_element(data_expected, data_me, rtol, atol):
  23. """ Precision calculation func """
  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_spectral_centroid_pipeline():
  32. """
  33. Feature: mindspore pipeline mode normal testcase: spectral_centroid op.
  34. Description: input audio signal to test pipeline.
  35. Expectation: success.
  36. """
  37. logger.info("test_spectral_centroid_pipeline")
  38. wav = [[[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5]]]
  39. dataset = ds.NumpySlicesDataset(wav, column_names=["audio"], shuffle=False)
  40. out = audio.SpectralCentroid(sample_rate=44100, n_fft=8)
  41. dataset = dataset.map(operations=out, input_columns=["audio"], output_columns=["SpectralCentroid"],
  42. column_order=['SpectralCentroid'])
  43. result = np.array([[[4436.1182, 3580.0718, 2902.4917, 3334.8962, 5199.8350, 6284.4814,
  44. 3580.0718, 2895.5659]]])
  45. for data1 in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  46. count_unequal_element(data1["SpectralCentroid"], result, 0.0001, 0.0001)
  47. def test_spectral_centroid_eager():
  48. """
  49. Feature: mindspore eager mode normal testcase: spectral_centroid op.
  50. Description: input audio signal to test eager.
  51. Expectation: success.
  52. """
  53. logger.info("test_spectral_centroid_eager")
  54. wav = np.array([[1.2, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5.5, 6.5]])
  55. spectral_centroid_op = audio.SpectralCentroid(sample_rate=48000, n_fft=8)
  56. out = spectral_centroid_op(wav)
  57. result = np.array([[[5276.65022959, 3896.67543098, 3159.17400004, 3629.81957922,
  58. 5659.68456649, 6840.25126846, 3896.67543098, 3316.97434286]]])
  59. count_unequal_element(out, result, 0.0001, 0.0001)
  60. def test_spectral_centroid_param():
  61. """
  62. Feature: test spectral_centroid invalid parameter.
  63. Description: test some invalid parameters.
  64. Expectation: success.
  65. """
  66. try:
  67. _ = audio.SpectralCentroid(sample_rate=-1)
  68. except ValueError as error:
  69. logger.info("Got an exception in SpectralCentroid: {}".format(str(error)))
  70. assert "Input sample_rate is not within the required interval of [0, 2147483647]." in str(error)
  71. try:
  72. _ = audio.SpectralCentroid(sample_rate=48000, n_fft=-1)
  73. except ValueError as error:
  74. logger.info("Got an exception in SpectralCentroid: {}".format(str(error)))
  75. assert "Input n_fft is not within the required interval of [1, 2147483647]." in str(error)
  76. try:
  77. _ = audio.SpectralCentroid(sample_rate=48000, n_fft=0)
  78. except ValueError as error:
  79. logger.info("Got an exception in SpectralCentroid: {}".format(str(error)))
  80. assert "Input n_fft is not within the required interval of [1, 2147483647]." in str(error)
  81. try:
  82. _ = audio.SpectralCentroid(sample_rate=48000, win_length=-1)
  83. except ValueError as error:
  84. logger.info("Got an exception in SpectralCentroid: {}".format(str(error)))
  85. assert "Input win_length is not within the required interval of [1, 2147483647]." in str(error)
  86. try:
  87. _ = audio.SpectralCentroid(sample_rate=48000, win_length="s")
  88. except TypeError as error:
  89. logger.info("Got an exception in SpectralCentroid: {}".format(str(error)))
  90. assert "Argument win_length with value s is not of type [<class 'int'>], but got <class 'str'>." in str(error)
  91. try:
  92. _ = audio.SpectralCentroid(sample_rate=48000, hop_length=-1)
  93. except ValueError as error:
  94. logger.info("Got an exception in SpectralCentroid: {}".format(str(error)))
  95. assert "Input hop_length is not within the required interval of [1, 2147483647]." in str(error)
  96. try:
  97. _ = audio.SpectralCentroid(sample_rate=48000, hop_length=-100)
  98. except ValueError as error:
  99. logger.info("Got an exception in SpectralCentroid: {}".format(str(error)))
  100. assert "Input hop_length is not within the required interval of [1, 2147483647]." in str(error)
  101. try:
  102. _ = audio.SpectralCentroid(sample_rate=48000, win_length=300, n_fft=200)
  103. except ValueError as error:
  104. logger.info("Got an exception in SpectralCentroid: {}".format(str(error)))
  105. assert "Input win_length should be no more than n_fft, but got win_length: 300 and n_fft: 200." \
  106. in str(error)
  107. if __name__ == "__main__":
  108. test_spectral_centroid_pipeline()
  109. test_spectral_centroid_eager()
  110. test_spectral_centroid_param()