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_spectrogram.py 23 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 Spectrogram 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. from mindspore.dataset.audio.utils import WindowType, BorderType
  23. def count_unequal_element(data_expected, data_me, rtol, atol):
  24. """ Precision calculation func """
  25. assert data_expected.shape == data_me.shape
  26. total_count = len(data_expected.flatten())
  27. error = np.abs(data_expected - data_me)
  28. greater = np.greater(error, atol + np.abs(data_expected) * rtol)
  29. loss_count = np.count_nonzero(greater)
  30. assert (loss_count / total_count) < rtol, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
  31. data_expected[greater], data_me[greater], error[greater])
  32. def test_spectrogram_pipeline():
  33. """
  34. Feature: mindspore pipeline mode normal testcase: spectrogram op.
  35. Description: input audio signal to test pipeline.
  36. Expectation: success.
  37. """
  38. logger.info("test_spectrogram_pipeline")
  39. 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]]]
  40. dataset = ds.NumpySlicesDataset(wav, column_names=["audio"], shuffle=False)
  41. out = audio.Spectrogram(n_fft=8)
  42. dataset = dataset.map(operations=out, input_columns=["audio"], output_columns=["Spectrogram"],
  43. column_order=['Spectrogram'])
  44. result = np.array([[[2.8015e+01, 1.2100e+02, 3.1354e+02, 1.6900e+02, 2.5000e+01,
  45. 1.0843e+01, 1.2100e+02, 3.3150e+02],
  46. [3.2145e+00, 3.3914e+01, 9.4728e+01, 4.5914e+01, 9.9142e+00,
  47. 4.5858e+00, 3.3914e+01, 9.5685e+01],
  48. [1.0000e+00, 1.7157e-01, 1.5000e+00, 1.7157e-01, 1.7157e-01,
  49. 5.0000e-01, 1.7157e-01, 7.5000e-01],
  50. [4.2893e-02, 2.5736e-01, 5.8579e-01, 2.5736e-01, 2.5736e-01,
  51. 5.8579e-01, 2.5736e-01, 1.2868e-01],
  52. [5.0000e-01, 1.0000e+00, 8.5787e-02, 1.0000e+00, 1.0000e+00,
  53. 5.0000e-01, 1.0000e+00, 6.2868e-01]]])
  54. for data1 in dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
  55. count_unequal_element(data1["Spectrogram"], result, 0.0001, 0.0001)
  56. def test_spectrogram_eager():
  57. """
  58. Feature: mindspore eager mode normal testcase: spectrogram op.
  59. Description: input audio signal to test eager.
  60. Expectation: success.
  61. """
  62. logger.info("test_spectrogram_eager")
  63. wav = np.array([[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]])
  64. out = audio.Spectrogram(n_fft=8, win_length=8, window=WindowType.HANN,
  65. pad_mode=BorderType.REFLECT)(np.array(wav, dtype="float"))
  66. result = np.array([[[2.8015e+01, 1.2100e+02, 3.1354e+02, 1.6900e+02, 2.5000e+01,
  67. 1.0843e+01, 1.2100e+02, 3.3150e+02],
  68. [3.2145e+00, 3.3914e+01, 9.4728e+01, 4.5914e+01, 9.9142e+00,
  69. 4.5858e+00, 3.3914e+01, 9.5685e+01],
  70. [1.0000e+00, 1.7157e-01, 1.5000e+00, 1.7157e-01, 1.7157e-01,
  71. 5.0000e-01, 1.7157e-01, 7.5000e-01],
  72. [4.2893e-02, 2.5736e-01, 5.8579e-01, 2.5736e-01, 2.5736e-01,
  73. 5.8579e-01, 2.5736e-01, 1.2868e-01],
  74. [5.0000e-01, 1.0000e+00, 8.5787e-02, 1.0000e+00, 1.0000e+00,
  75. 5.0000e-01, 1.0000e+00, 6.2868e-01]]])
  76. count_unequal_element(out, result, 0.0001, 0.0001)
  77. def test_spectrogram_window_hamming_padmode_constant():
  78. """
  79. Feature: test spectrogram parameter: window, pad_mode.
  80. Description: test parameter.
  81. Expectation: success.
  82. """
  83. logger.info("test_spectrogram_window_hamming_padmode_constant")
  84. wav = np.array([[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]])
  85. out = audio.Spectrogram(n_fft=8, window=WindowType.HAMMING,
  86. pad_mode=BorderType.CONSTANT)(np.array(wav, dtype="float"))
  87. result = np.array([[[1.1389e+01, 1.3736e+02, 3.5534e+02, 2.0164e+02, 3.0914e+01,
  88. 1.3465e+01, 1.3736e+02, 2.5064e+02],
  89. [5.6934e+00, 3.1860e+01, 8.5291e+01, 3.8484e+01, 9.1907e+00,
  90. 4.5576e+00, 3.1860e+01, 1.0027e+02],
  91. [1.9633e-01, 7.4475e-02, 1.2696e+00, 7.4475e-02, 7.4475e-02,
  92. 4.2320e-01, 7.4475e-02, 1.1765e+01],
  93. [5.0570e-01, 3.8456e-01, 6.8326e-01, 3.8456e-01, 3.8456e-01,
  94. 6.8326e-01, 3.8456e-01, 5.4501e+00],
  95. [6.1665e-01, 8.4640e-01, 7.2610e-02, 8.4640e-01, 8.4640e-01,
  96. 4.2320e-01, 8.4640e-01, 1.0642e+00]]])
  97. count_unequal_element(out, result, 0.0001, 0.0001)
  98. def test_spectrogram_nfft_10_window_bartlett_padmode_edge():
  99. """
  100. Feature: test spectrogram parameter: n_fft, window, pad_mode.
  101. Description: test parameter.
  102. Expectation: success.
  103. """
  104. logger.info("test_spectrogram_nfft_10_window_bartlett_padmode_edge")
  105. wav = np.array([[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]])
  106. out = audio.Spectrogram(n_fft=10, window=WindowType.BARTLETT,
  107. pad_mode=BorderType.EDGE)(np.array(wav, dtype="float"))
  108. result = np.array([[[4.0960e+01, 2.6244e+02, 4.0000e+02, 7.7440e+01, 2.5000e+01,
  109. 2.6244e+02, 5.9536e+02],
  110. [4.7655e+00, 5.6721e+01, 9.4681e+01, 2.3822e+01, 5.9597e+00,
  111. 5.6721e+01, 1.1597e+02],
  112. [5.3889e-01, 5.8360e-03, 9.2361e-01, 5.8359e-03, 9.2361e-01,
  113. 5.8360e-03, 2.4944e-01],
  114. [1.1449e-01, 9.9859e-01, 3.1897e-01, 2.9828e-01, 1.0403e+00,
  115. 9.9859e-01, 1.3072e+00],
  116. [1.8111e-01, 2.7416e-01, 4.7639e-01, 2.7416e-01, 4.7639e-01,
  117. 2.7416e-01, 7.0557e-02],
  118. [6.4000e-01, 3.6000e-01, 0.0000e+00, 2.5600e+00, 1.0000e+00,
  119. 3.6000e-01, 1.4400e+00]]])
  120. count_unequal_element(out, result, 0.0001, 0.0001)
  121. def test_spectrogram_onsided_false():
  122. """
  123. Feature: test spectrogram parameter: onesided.
  124. Description: test parameter.
  125. Expectation: success.
  126. """
  127. logger.info("test_spectrogram_onsided_false")
  128. wav = np.array([[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]])
  129. out = audio.Spectrogram(n_fft=10, window=WindowType.BARTLETT,
  130. pad_mode=BorderType.EDGE, onesided=False)(np.array(wav, dtype="float"))
  131. result = np.array([[[4.0960e+01, 2.6244e+02, 4.0000e+02, 7.7440e+01, 2.5000e+01,
  132. 2.6244e+02, 5.9536e+02],
  133. [4.7655e+00, 5.6721e+01, 9.4681e+01, 2.3822e+01, 5.9597e+00,
  134. 5.6721e+01, 1.1597e+02],
  135. [5.3889e-01, 5.8360e-03, 9.2361e-01, 5.8359e-03, 9.2361e-01,
  136. 5.8360e-03, 2.4944e-01],
  137. [1.1449e-01, 9.9859e-01, 3.1897e-01, 2.9828e-01, 1.0403e+00,
  138. 9.9859e-01, 1.3072e+00],
  139. [1.8111e-01, 2.7416e-01, 4.7639e-01, 2.7416e-01, 4.7639e-01,
  140. 2.7416e-01, 7.0557e-02],
  141. [6.4000e-01, 3.6000e-01, 0.0000e+00, 2.5600e+00, 1.0000e+00,
  142. 3.6000e-01, 1.4400e+00],
  143. [1.8111e-01, 2.7416e-01, 4.7639e-01, 2.7416e-01, 4.7639e-01,
  144. 2.7416e-01, 7.0557e-02],
  145. [1.1449e-01, 9.9859e-01, 3.1897e-01, 2.9828e-01, 1.0403e+00,
  146. 9.9859e-01, 1.3072e+00],
  147. [5.3889e-01, 5.8360e-03, 9.2361e-01, 5.8359e-03, 9.2361e-01,
  148. 5.8360e-03, 2.4944e-01],
  149. [4.7655e+00, 5.6721e+01, 9.4681e+01, 2.3822e+01, 5.9597e+00,
  150. 5.6721e+01, 1.1597e+02]]])
  151. count_unequal_element(out, result, 0.0001, 0.0001)
  152. def test_spectrogram_power_0():
  153. """
  154. Feature: test spectrogram parameter: power.
  155. Description: test parameter.
  156. Expectation: success.
  157. """
  158. logger.info("test_spectrogram_power_0")
  159. wav = np.array([[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]])
  160. out = audio.Spectrogram(n_fft=8, window=WindowType.HANN,
  161. pad_mode=BorderType.REFLECT, power=0)(np.array(wav, dtype="float"))
  162. result = np.array([[[[5.2929e+00, 0.0000e+00],
  163. [1.1000e+01, 0.0000e+00],
  164. [1.7707e+01, 0.0000e+00],
  165. [1.3000e+01, 0.0000e+00],
  166. [5.0000e+00, 0.0000e+00],
  167. [3.2929e+00, 0.0000e+00],
  168. [1.1000e+01, 0.0000e+00],
  169. [1.8207e+01, 0.0000e+00]],
  170. [[-1.7929e+00, -2.5288e-07],
  171. [-5.5000e+00, 1.9142e+00],
  172. [-9.7071e+00, 7.0711e-01],
  173. [-6.5000e+00, -1.9142e+00],
  174. [-2.5000e+00, -1.9142e+00],
  175. [-1.2929e+00, 1.7071e+00],
  176. [-5.5000e+00, 1.9142e+00],
  177. [-9.7071e+00, 1.2071e+00]],
  178. [[-1.0000e+00, 0.0000e+00],
  179. [0.0000e+00, -4.1421e-01],
  180. [1.0000e+00, -7.0711e-01],
  181. [0.0000e+00, 4.1421e-01],
  182. [0.0000e+00, 4.1421e-01],
  183. [0.0000e+00, -7.0711e-01],
  184. [0.0000e+00, -4.1421e-01],
  185. [5.0000e-01, -7.0711e-01]],
  186. [[-2.0711e-01, -2.5288e-07],
  187. [-5.0000e-01, -8.5787e-02],
  188. [-2.9289e-01, 7.0711e-01],
  189. [5.0000e-01, 8.5786e-02],
  190. [5.0000e-01, 8.5786e-02],
  191. [-7.0711e-01, -2.9289e-01],
  192. [-5.0000e-01, -8.5787e-02],
  193. [-2.9289e-01, 2.0711e-01]],
  194. [[7.0711e-01, 0.0000e+00],
  195. [1.0000e+00, 0.0000e+00],
  196. [2.9289e-01, 0.0000e+00],
  197. [-1.0000e+00, 0.0000e+00],
  198. [-1.0000e+00, 0.0000e+00],
  199. [7.0711e-01, 0.0000e+00],
  200. [1.0000e+00, 0.0000e+00],
  201. [7.9289e-01, 0.0000e+00]]]])
  202. count_unequal_element(out, result, 0.0001, 0.0001)
  203. def test_spectrogram_center_false():
  204. """
  205. Feature: test spectrogram parameter: center.
  206. Description: test parameter.
  207. Expectation: success.
  208. """
  209. logger.info("test_spectrogram_center_false")
  210. wav = np.array([[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]])
  211. out = audio.Spectrogram(n_fft=8, window=WindowType.HANN,
  212. center=False, pad_mode=BorderType.REFLECT)(np.array(wav, dtype="float"))
  213. result = np.array([[[1.2100e+02, 3.1354e+02, 1.6900e+02, 2.5000e+01, 1.0843e+01,
  214. 1.2100e+02],
  215. [3.3914e+01, 9.4728e+01, 4.5914e+01, 9.9142e+00, 4.5858e+00,
  216. 3.3914e+01],
  217. [1.7157e-01, 1.5000e+00, 1.7157e-01, 1.7157e-01, 5.0000e-01,
  218. 1.7157e-01],
  219. [2.5736e-01, 5.8579e-01, 2.5736e-01, 2.5736e-01, 5.8579e-01,
  220. 2.5736e-01],
  221. [1.0000e+00, 8.5787e-02, 1.0000e+00, 1.0000e+00, 5.0000e-01,
  222. 1.0000e+00]]])
  223. count_unequal_element(out, result, 0.0001, 0.0001)
  224. def test_spectrogram_normalized_true():
  225. """
  226. Feature: test spectrogram parameter: normalized.
  227. Description: test parameter.
  228. Expectation: success.
  229. """
  230. logger.info("test_spectrogram_normalized_true")
  231. wav = np.array([[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]])
  232. out = audio.Spectrogram(n_fft=8, window=WindowType.HANN,
  233. center=False, normalized=True, pad_mode=BorderType.REFLECT)(np.array(wav, dtype="float"))
  234. result = np.array([[[4.0333e+01, 1.0451e+02, 5.6333e+01, 8.3333e+00, 3.6144e+00,
  235. 4.0333e+01],
  236. [1.1305e+01, 3.1576e+01, 1.5305e+01, 3.3047e+00, 1.5286e+00,
  237. 1.1305e+01],
  238. [5.7191e-02, 5.0000e-01, 5.7191e-02, 5.7191e-02, 1.6667e-01,
  239. 5.7191e-02],
  240. [8.5786e-02, 1.9526e-01, 8.5786e-02, 8.5786e-02, 1.9526e-01,
  241. 8.5786e-02],
  242. [3.3333e-01, 2.8596e-02, 3.3333e-01, 3.3333e-01, 1.6667e-01,
  243. 3.3333e-01]]])
  244. count_unequal_element(out, result, 0.0001, 0.0001)
  245. def test_spectrogram_inputrank_3():
  246. """
  247. Feature: test spectrogram parameter: input rank.
  248. Description: test input rank.
  249. Expectation: success.
  250. """
  251. logger.info("test_spectrogram_inputrank_3")
  252. wav = np.array([[[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1]],
  253. [[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1]]])
  254. out = audio.Spectrogram(n_fft=8, window=WindowType.HANN, pad_mode=BorderType.REFLECT)(np.array(wav, dtype="float"))
  255. result = np.array([[[[2.8015e+01, 1.2100e+02, 3.1354e+02, 1.6900e+02, 3.3558e+01],
  256. [3.2145e+00, 3.3914e+01, 9.4728e+01, 4.5914e+01, 6.7145e+00],
  257. [1.0000e+00, 1.7157e-01, 1.5000e+00, 1.7157e-01, 7.5000e-01],
  258. [4.2893e-02, 2.5736e-01, 5.8579e-01, 2.5736e-01, 1.2868e-01],
  259. [5.0000e-01, 1.0000e+00, 8.5787e-02, 1.0000e+00, 6.2868e-01]]],
  260. [[[2.8015e+01, 1.2100e+02, 3.1354e+02, 1.6900e+02, 3.3558e+01],
  261. [3.2145e+00, 3.3914e+01, 9.4728e+01, 4.5914e+01, 6.7145e+00],
  262. [1.0000e+00, 1.7157e-01, 1.5000e+00, 1.7157e-01, 7.5000e-01],
  263. [4.2893e-02, 2.5736e-01, 5.8579e-01, 2.5736e-01, 1.2868e-01],
  264. [5.0000e-01, 1.0000e+00, 8.5787e-02, 1.0000e+00, 6.2868e-01]]]])
  265. count_unequal_element(out, result, 0.0001, 0.0001)
  266. def test_spectrogram_winlength_7():
  267. """
  268. Feature: test spectrogram parameter: win_length.
  269. Description: test parameter.
  270. Expectation: success.
  271. """
  272. logger.info("test_spectrogram_winlength_7")
  273. wav = np.array([[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]])
  274. out = audio.Spectrogram(n_fft=8, win_length=7, window=WindowType.HANN,)(np.array(wav, dtype="float"))
  275. result = np.array([[[2.0140e+01, 4.9000e+01, 1.5006e+02, 2.5284e+02, 1.5006e+02,
  276. 4.9000e+01, 4.5220e+00, 1.2250e+01, 7.6562e+01, 1.9600e+02,
  277. 2.7265e+02],
  278. [5.0272e+00, 1.9488e+01, 5.5103e+01, 1.0153e+02, 5.5103e+01,
  279. 1.9488e+01, 2.0179e+00, 6.5089e+00, 2.9144e+01, 7.1406e+01,
  280. 1.0662e+02],
  281. [4.2200e-01, 5.4020e-01, 1.0554e+00, 4.8321e+00, 1.0554e+00,
  282. 5.4020e-01, 9.6867e-01, 4.0345e-01, 7.8188e-01, 1.0872e+00,
  283. 3.3055e+00],
  284. [1.2817e-01, 3.8618e-01, 2.1917e-01, 2.6102e-01, 2.1917e-01,
  285. 3.8618e-01, 4.3028e-01, 3.7738e-01, 2.0158e-01, 4.2135e-01,
  286. 5.7616e-02],
  287. [3.7364e-01, 7.1574e-01, 8.1719e-01, 9.0949e-13, 8.1720e-01,
  288. 7.1573e-01, 2.7823e-01, 7.1573e-01, 8.1719e-01, 7.1574e-01,
  289. 3.7364e-01]]])
  290. count_unequal_element(out, result, 0.0001, 0.0001)
  291. def test_spectrogram_param():
  292. """
  293. Feature: test spectrogram invalid parameter.
  294. Description: test some invalid parameters.
  295. Expectation: success.
  296. """
  297. try:
  298. _ = audio.Spectrogram(n_fft=-1)
  299. except ValueError as error:
  300. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  301. assert "Input n_fft is not within the required interval of [1, 2147483647]." in str(error)
  302. try:
  303. _ = audio.Spectrogram(n_fft=0)
  304. except ValueError as error:
  305. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  306. assert "Input n_fft is not within the required interval of [1, 2147483647]." in str(error)
  307. try:
  308. _ = audio.Spectrogram(win_length=-1)
  309. except ValueError as error:
  310. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  311. assert "Input win_length is not within the required interval of [1, 2147483647]." in str(error)
  312. try:
  313. _ = audio.Spectrogram(win_length="s")
  314. except TypeError as error:
  315. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  316. assert "Argument win_length with value s is not of type [<class 'int'>], but got <class 'str'>." in str(error)
  317. try:
  318. _ = audio.Spectrogram(hop_length=-1)
  319. except ValueError as error:
  320. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  321. assert "Input hop_length is not within the required interval of [1, 2147483647]." in str(error)
  322. try:
  323. _ = audio.Spectrogram(hop_length=-100)
  324. except ValueError as error:
  325. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  326. assert "Input hop_length is not within the required interval of [1, 2147483647]." in str(error)
  327. try:
  328. _ = audio.Spectrogram(win_length=300, n_fft=200)
  329. except ValueError as error:
  330. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  331. assert "Input win_length should be no more than n_fft, but got win_length: 300 and n_fft: 200." \
  332. in str(error)
  333. try:
  334. _ = audio.Spectrogram(pad=-1)
  335. except ValueError as error:
  336. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  337. assert "Input pad is not within the required interval of [0, 2147483647]." in str(error)
  338. try:
  339. _ = audio.Spectrogram(power=-1)
  340. except ValueError as error:
  341. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  342. assert "Input power is not within the required interval of [0, 16777216]." in str(error)
  343. try:
  344. _ = audio.Spectrogram(n_fft=False)
  345. except TypeError as error:
  346. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  347. assert "Argument n_fft with value False is not of type (<class 'int'>,)" in str(error)
  348. try:
  349. _ = audio.Spectrogram(n_fft="s")
  350. except TypeError as error:
  351. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  352. assert "Argument n_fft with value s is not of type [<class 'int'>], but got <class 'str'>." \
  353. in str(error)
  354. try:
  355. _ = audio.Spectrogram(window=False)
  356. except TypeError as error:
  357. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  358. assert "Argument window with value False is not of type [<enum 'WindowType'>], but got <class 'bool'>." \
  359. in str(error)
  360. try:
  361. _ = audio.Spectrogram(pad_mode=False)
  362. except TypeError as error:
  363. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  364. assert "Argument pad_mode with value False is not of type [<enum 'BorderType'>], but got <class 'bool'>." \
  365. in str(error)
  366. try:
  367. _ = audio.Spectrogram(onesided="s")
  368. except TypeError as error:
  369. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  370. assert "Argument onesided with value s is not of type [<class 'bool'>], but got <class 'str'>." in str(error)
  371. try:
  372. _ = audio.Spectrogram(center="s")
  373. except TypeError as error:
  374. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  375. assert "Argument center with value s is not of type [<class 'bool'>], but got <class 'str'>." in str(error)
  376. try:
  377. _ = audio.Spectrogram(normalized="s")
  378. except TypeError as error:
  379. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  380. assert "Argument normalized with value s is not of type [<class 'bool'>], but got <class 'str'>." in str(error)
  381. try:
  382. _ = audio.Spectrogram(normalized=1)
  383. except TypeError as error:
  384. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  385. assert "Argument normalized with value 1 is not of type [<class 'bool'>], but got <class 'int'>." in str(error)
  386. try:
  387. wav = np.array([[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]])
  388. _ = audio.Spectrogram(n_fft=100, center=False)(wav)
  389. except RuntimeError as error:
  390. logger.info("Got an exception in Spectrogram: {}".format(str(error)))
  391. assert "Unexpected error. Spectrogram: n_fft should be more than 0 and less than 30," \
  392. " but got n_fft: 100." in str(error)
  393. if __name__ == "__main__":
  394. test_spectrogram_pipeline()
  395. test_spectrogram_eager()
  396. test_spectrogram_window_hamming_padmode_constant()
  397. test_spectrogram_nfft_10_window_bartlett_padmode_edge()
  398. test_spectrogram_onsided_false()
  399. test_spectrogram_power_0()
  400. test_spectrogram_center_false()
  401. test_spectrogram_normalized_true()
  402. test_spectrogram_inputrank_3()
  403. test_spectrogram_winlength_7()
  404. test_spectrogram_param()