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 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2024 Tencent
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. #include "testutil.h"
  4. 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)
  5. {
  6. ncnn::Mat a = RandomMat(size);
  7. ncnn::ParamDict pd;
  8. pd.set(0, n_fft);
  9. pd.set(1, power);
  10. pd.set(2, hoplen);
  11. pd.set(3, winlen);
  12. pd.set(4, window_type);
  13. pd.set(5, center);
  14. pd.set(6, pad_type);
  15. pd.set(7, normalized);
  16. pd.set(8, onesided);
  17. std::vector<ncnn::Mat> weights(0);
  18. int ret = test_layer("Spectrogram", pd, weights, a);
  19. if (ret != 0)
  20. {
  21. 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);
  22. }
  23. return ret;
  24. }
  25. static int test_spectrogram_0()
  26. {
  27. return 0
  28. || test_spectrogram(17, 1, 0, 1, 1, 0, 1, 0, 0, 0)
  29. || test_spectrogram(39, 17, 0, 7, 15, 0, 0, 0, 1, 0)
  30. || test_spectrogram(128, 10, 0, 2, 7, 1, 1, 1, 1, 1)
  31. || test_spectrogram(255, 17, 1, 14, 17, 2, 0, 0, 0, 1)
  32. || test_spectrogram(124, 55, 2, 12, 55, 1, 1, 2, 2, 0);
  33. }
  34. 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)
  35. {
  36. ncnn::Layer* layer = ncnn::create_layer("Spectrogram");
  37. ncnn::ParamDict pd;
  38. pd.set(0, n_fft);
  39. pd.set(1, power);
  40. pd.set(2, hoplen);
  41. pd.set(3, winlen);
  42. pd.set(4, window_type);
  43. pd.set(5, center);
  44. pd.set(6, pad_type);
  45. pd.set(7, normalized);
  46. pd.set(8, onesided);
  47. ncnn::Mat input = ncnn::Mat(size);
  48. memcpy(input, in, size * sizeof(float));
  49. ncnn::Mat output;
  50. ncnn::Option opt;
  51. opt.num_threads = 2;
  52. layer->load_param(pd);
  53. layer->create_pipeline(opt);
  54. layer->forward(input, output, opt);
  55. layer->destroy_pipeline(opt);
  56. const float epsilon = 1e-6;
  57. for (int i = 0; i < output.c; i++)
  58. {
  59. float* output_data = output.channel(i);
  60. for (int j = 0; j < output.h; j++)
  61. {
  62. for (int k = 0; k < output.w; k++)
  63. {
  64. if (fabs(output_data[j * output.w + k] - std[i * output.h * output.w + j * output.w + k]) > epsilon)
  65. {
  66. 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);
  67. return 1;
  68. }
  69. }
  70. }
  71. }
  72. delete layer;
  73. return 0;
  74. }
  75. static int test_spectrogram_1()
  76. {
  77. 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};
  78. float std_0[] = {
  79. 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
  80. };
  81. float std_1[] = {
  82. 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
  83. };
  84. float std_2[] = {
  85. 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
  86. };
  87. return test_spectrogram_eval(16, 4, 1, 2, 4, 1, 1, 0, 0, 1, input_0, std_0)
  88. || test_spectrogram_eval(16, 8, 1, 2, 4, 1, 0, 0, 0, 1, input_0, std_1)
  89. || test_spectrogram_eval(16, 8, 1, 3, 4, 1, 0, 0, 1, 0, input_0, std_2);
  90. }
  91. int main()
  92. {
  93. SRAND(7767517);
  94. return test_spectrogram_0() || test_spectrogram_1();
  95. }