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.

spectrogram_x86.cpp 7.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "spectrogram_x86.h"
  15. namespace ncnn {
  16. Spectrogram_x86::Spectrogram_x86()
  17. : conv_transpose(0)
  18. {
  19. one_blob_only = true;
  20. support_inplace = false;
  21. }
  22. Spectrogram_x86::~Spectrogram_x86()
  23. {
  24. delete conv_transpose;
  25. }
  26. int Spectrogram_x86::load_param(const ParamDict& pd)
  27. {
  28. n_fft = pd.get(0, 0);
  29. power = pd.get(1, 0);
  30. hoplen = pd.get(2, n_fft / 4);
  31. winlen = pd.get(3, n_fft);
  32. window_type = pd.get(4, 0);
  33. center = pd.get(5, 1);
  34. pad_type = pd.get(6, 2);
  35. normalized = pd.get(7, 0);
  36. onesided = pd.get(8, 1);
  37. // assert winlen <= n_fft
  38. // generate window
  39. window_data.create(n_fft);
  40. {
  41. float* p = window_data;
  42. for (int i = 0; i < (n_fft - winlen) / 2; i++)
  43. {
  44. *p++ = 0.f;
  45. }
  46. if (window_type == 0)
  47. {
  48. // all ones
  49. for (int i = 0; i < winlen; i++)
  50. {
  51. *p++ = 1.f;
  52. }
  53. }
  54. if (window_type == 1)
  55. {
  56. // hann window
  57. for (int i = 0; i < winlen; i++)
  58. {
  59. *p++ = 0.5f * (1 - cosf(2 * 3.14159265358979323846 * i / winlen));
  60. }
  61. }
  62. if (window_type == 2)
  63. {
  64. // hamming window
  65. for (int i = 0; i < winlen; i++)
  66. {
  67. *p++ = 0.54f - 0.46f * cosf(2 * 3.14159265358979323846 * i / winlen);
  68. }
  69. }
  70. for (int i = 0; i < n_fft - winlen - (n_fft - winlen) / 2; i++)
  71. {
  72. *p++ = 0.f;
  73. }
  74. // pre-calculated window norm factor
  75. if (normalized == 2)
  76. {
  77. float sqsum = 0.f;
  78. for (int i = 0; i < n_fft; i++)
  79. {
  80. sqsum += window_data[i] * window_data[i];
  81. }
  82. float scale = 1.f / sqrt(sqsum);
  83. for (int i = 0; i < n_fft; i++)
  84. {
  85. window_data[i] *= scale;
  86. }
  87. }
  88. }
  89. Mat theta;
  90. if (onesided)
  91. {
  92. n_freq = n_fft / 2 + 1;
  93. }
  94. else
  95. {
  96. n_freq = n_fft;
  97. }
  98. theta.create(n_fft, n_freq, size_t(8));
  99. for (int i = 0; i < n_freq; i++)
  100. {
  101. for (int j = 0; j < n_fft; j++)
  102. {
  103. theta.row<double>(i)[j] = 2 * 3.14159265358979323846 * i * j / n_fft;
  104. }
  105. }
  106. Mat real_basis, imag_basis;
  107. real_basis.create(n_fft, n_freq, size_t(8));
  108. imag_basis.create(n_fft, n_freq, size_t(8));
  109. for (int i = 0; i < n_freq; i++)
  110. {
  111. for (int j = 0; j < n_fft; j++)
  112. {
  113. real_basis.row<double>(i)[j] = cos(theta.row<double>(i)[j]);
  114. imag_basis.row<double>(i)[j] = -sin(theta.row<double>(i)[j]);
  115. }
  116. }
  117. // multiply window
  118. for (int i = 0; i < n_freq; i++)
  119. {
  120. for (int j = 0; j < n_fft; j++)
  121. {
  122. real_basis.row<double>(i)[j] *= window_data[j];
  123. imag_basis.row<double>(i)[j] *= window_data[j];
  124. }
  125. }
  126. if (normalized == 1)
  127. {
  128. double scale = 1.f / sqrt(n_fft);
  129. for (int i = 0; i < n_freq; i++)
  130. {
  131. for (int j = 0; j < n_fft; j++)
  132. {
  133. real_basis.row<double>(i)[j] *= scale;
  134. imag_basis.row<double>(i)[j] *= scale;
  135. }
  136. }
  137. }
  138. conv_data.create(n_fft, 1, n_freq * 2);
  139. for (int i = 0; i < n_freq; i++)
  140. {
  141. for (int j = 0; j < n_fft; j++)
  142. {
  143. conv_data.channel(i).row<float>(0)[j] = (float)real_basis.row<double>(i)[j];
  144. conv_data.channel(i + n_freq).row<float>(0)[j] = (float)imag_basis.row<double>(i)[j];
  145. }
  146. }
  147. conv_transpose = ncnn::create_layer("Convolution1D");
  148. ncnn::ParamDict conv_transpose_pd;
  149. conv_transpose_pd.set(0, 2 * n_freq); // num_output
  150. conv_transpose_pd.set(1, n_fft); // kernel_w
  151. conv_transpose_pd.set(3, hoplen); // stride_w
  152. conv_transpose_pd.set(19, 1); // dynamic_weight
  153. conv_transpose->load_param(conv_transpose_pd);
  154. return 0;
  155. }
  156. int Spectrogram_x86::forward(const Mat& bottom_blob, Mat& top_blob, const Option& opt) const
  157. {
  158. // https://pytorch.org/audio/stable/generated/torchaudio.functional.spectrogram.html
  159. // TODO custom window
  160. Mat bottom_blob_bordered = bottom_blob;
  161. if (center == 1)
  162. {
  163. Option opt_b = opt;
  164. opt_b.blob_allocator = opt.workspace_allocator;
  165. if (pad_type == 0)
  166. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, n_fft / 2, n_fft / 2, BORDER_CONSTANT, 0.f, opt_b);
  167. if (pad_type == 1)
  168. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, n_fft / 2, n_fft / 2, BORDER_REPLICATE, 0.f, opt_b);
  169. if (pad_type == 2)
  170. copy_make_border(bottom_blob, bottom_blob_bordered, 0, 0, n_fft / 2, n_fft / 2, BORDER_REFLECT, 0.f, opt_b);
  171. }
  172. const int size = bottom_blob_bordered.w;
  173. // const int frames = size / hoplen + 1;
  174. const int frames = (size - n_fft) / hoplen + 1;
  175. const size_t elemsize = bottom_blob_bordered.elemsize;
  176. if (elemsize != sizeof(float))
  177. {
  178. return -100;
  179. }
  180. if (power == 0)
  181. {
  182. top_blob.create(2, frames, n_freq, elemsize, opt.blob_allocator);
  183. }
  184. else
  185. {
  186. top_blob.create(frames, n_freq, elemsize, opt.blob_allocator);
  187. }
  188. if (top_blob.empty())
  189. return -100;
  190. std::vector<Mat> inputs;
  191. inputs.push_back(bottom_blob_bordered);
  192. inputs.push_back(conv_data);
  193. std::vector<Mat> outputs;
  194. outputs.push_back(Mat());
  195. Option opt_conv = opt;
  196. opt_conv.use_packing_layout = false;
  197. conv_transpose->create_pipeline(opt_conv);
  198. conv_transpose->forward(inputs, outputs, opt_conv);
  199. conv_transpose->destroy_pipeline(opt_conv);
  200. Mat conv_top_blob = outputs[0]; // (2 * n_freq, frames)
  201. float* conv_top_data = conv_top_blob;
  202. if (power == 0) // as complex
  203. {
  204. // copy
  205. for (int i = 0; i < frames; i++)
  206. {
  207. for (int j = 0; j < n_freq; j++)
  208. {
  209. top_blob.channel(j).row<float>(i)[0] = conv_top_data[j * frames + i];
  210. top_blob.channel(j).row<float>(i)[1] = conv_top_data[(j + n_freq) * frames + i];
  211. }
  212. }
  213. }
  214. else
  215. {
  216. if (power == 1) // magnitude sqrt(re * re + im * im);
  217. {
  218. // copy
  219. for (int i = 0; i < frames; i++)
  220. {
  221. for (int j = 0; j < n_freq; j++)
  222. {
  223. top_blob.row<float>(j)[i] = sqrtf(conv_top_data[j * frames + i] * conv_top_data[j * frames + i] + conv_top_data[(j + n_freq) * frames + i] * conv_top_data[(j + n_freq) * frames + i]);
  224. }
  225. }
  226. }
  227. else if (power == 2) // power re * re + im * im;
  228. {
  229. // copy
  230. for (int i = 0; i < frames; i++)
  231. {
  232. for (int j = 0; j < n_freq; j++)
  233. {
  234. top_blob.row<float>(j)[i] = conv_top_data[j * frames + i] * conv_top_data[j * frames + i] + conv_top_data[(j + n_freq) * frames + i] * conv_top_data[(j + n_freq) * frames + i];
  235. }
  236. }
  237. }
  238. }
  239. return 0;
  240. }
  241. } // namespace ncnn