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.cpp 5.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "testutil.h"
  15. static int test_spectrogram(int size, int n_fft, int power, int hoplen, int winlen, int window_type, int center, int pad_type, int normalized, int onesided)
  16. {
  17. ncnn::Mat a = RandomMat(size);
  18. ncnn::ParamDict pd;
  19. pd.set(0, n_fft);
  20. pd.set(1, power);
  21. pd.set(2, hoplen);
  22. pd.set(3, winlen);
  23. pd.set(4, window_type);
  24. pd.set(5, center);
  25. pd.set(6, pad_type);
  26. pd.set(7, normalized);
  27. pd.set(8, onesided);
  28. std::vector<ncnn::Mat> weights(0);
  29. int ret = test_layer("Spectrogram", pd, weights, a);
  30. if (ret != 0)
  31. {
  32. fprintf(stderr, "test_spectrogram failed size=%d n_fft=%d power=%d hoplen=%d winlen=%d window_type=%d center=%d pad_type=%d normalized=%d onesided=%d\n", size, n_fft, power, hoplen, winlen, window_type, center, pad_type, normalized, onesided);
  33. }
  34. return ret;
  35. }
  36. static int test_spectrogram_0()
  37. {
  38. return 0
  39. || test_spectrogram(17, 1, 0, 1, 1, 0, 1, 0, 0, 0)
  40. || test_spectrogram(39, 17, 0, 7, 15, 0, 0, 0, 1, 0)
  41. || test_spectrogram(128, 10, 0, 2, 7, 1, 1, 1, 1, 1)
  42. || test_spectrogram(255, 17, 1, 14, 17, 2, 0, 0, 0, 1)
  43. || test_spectrogram(124, 55, 2, 12, 55, 1, 1, 2, 2, 0);
  44. }
  45. static int test_spectrogram_eval(int size, int n_fft, int power, int hoplen, int winlen, int window_type, int center, int pad_type, int normalized, int onesided,float * in,float * std)
  46. {
  47. ncnn::Layer * layer = ncnn::create_layer("Spectrogram");
  48. ncnn::ParamDict pd;
  49. pd.set(0, n_fft);
  50. pd.set(1, power);
  51. pd.set(2, hoplen);
  52. pd.set(3, winlen);
  53. pd.set(4, window_type);
  54. pd.set(5, center);
  55. pd.set(6, pad_type);
  56. pd.set(7, normalized);
  57. pd.set(8, onesided);
  58. ncnn::Mat input = ncnn::Mat(size);
  59. memcpy(input, in, size * sizeof(float));
  60. ncnn::Mat output;
  61. ncnn::Option opt;
  62. opt.num_threads = 2;
  63. layer->load_param(pd);
  64. layer->create_pipeline(opt);
  65. layer->forward(input, output, opt);
  66. layer->destroy_pipeline(opt);
  67. const float epsilon = 1e-6;
  68. for (int i = 0; i < output.c; i++)
  69. {
  70. float * output_data = output.channel(i);
  71. for (int j = 0; j < output.h; j++)
  72. {
  73. for (int k = 0; k < output.w; k++)
  74. {
  75. if (fabs(output_data[j * output.w + k] - std[i * output.h * output.w + j * output.w + k]) > epsilon)
  76. {
  77. fprintf(stderr, "test_spectrogram failed size=%d n_fft=%d power=%d hoplen=%d winlen=%d window_type=%d center=%d pad_type=%d normalized=%d onesided=%d\n", size, n_fft, power, hoplen, winlen, window_type, center, pad_type, normalized, onesided);
  78. return 1;
  79. }
  80. }
  81. }
  82. }
  83. delete layer;
  84. return 0;
  85. }
  86. static int test_spectrogram_1()
  87. {
  88. float input_0[16] = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1.0f, 1.1f, 1.2f, 1.3f, 1.4f, 1.5f};
  89. float std_0[] = {
  90. 0.05000000f, 0.40000001f, 0.80000001f, 1.20000005f, 1.59999990f, 2.00000000f, 2.40000010f, 2.79999995f, 0.75000000f, 0.05000000f, 0.22360681f, 0.41231057f, 0.60827625f, 0.80622578f, 1.00498760f, 1.20415950f, 1.40356684f, 0.75000000f, 0.05000000f, 0.00000000f, 0.00000000f, 0.00000000f, 0.00000006f, 0.00000000f, 0.00000000f, 0.00000000f, 0.75000000f
  91. };
  92. float std_1[] = {
  93. 0.80000001f, 1.20000005f, 1.59999990f, 2.00000000f, 2.40000010f, 0.68649411f, 1.02670193f, 1.36751485f, 1.70857072f, 2.04974818f, 0.41231057f, 0.60827625f, 0.80622578f, 1.00498760f, 1.20415950f, 0.13684234f, 0.18942842f, 0.24475159f, 0.30130789f, 0.35851428f, 0.00000000f, 0.00000000f, 0.00000006f, 0.00000000f, 0.00000000f
  94. };
  95. float std_2[] = {
  96. 0.28284273f, 0.49497476f, 0.70710677f, 0.24271232f, 0.42322639f, 0.60407096f, 0.14577380f, 0.25000000f, 0.35531676f, 0.04838108f, 0.07667736f, 0.10652842f, 0.00000000f, 0.00000002f, 0.00000000f, 0.04838108f, 0.07667736f, 0.10652842f, 0.14577380f, 0.25000000f, 0.35531676f, 0.24271232f, 0.42322639f, 0.60407096f
  97. };
  98. return
  99. test_spectrogram_eval(16, 4, 1, 2, 4, 1, 1, 0, 0, 1, input_0, std_0)
  100. || test_spectrogram_eval(16, 8, 1, 2, 4, 1, 0, 0, 0, 1, input_0, std_1)
  101. || test_spectrogram_eval(16, 8, 1, 3, 4, 1, 0, 0, 1, 0, input_0, std_2);
  102. }
  103. int main()
  104. {
  105. SRAND(7767517);
  106. return test_spectrogram_0() || test_spectrogram_1();
  107. }