You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
4.8KB

  1. #include <cuda_runtime.h>
  2. #include <math.h>
  3. #if defined(_WIN32)
  4. #define GPUD_API extern "C" __declspec(dllexport)
  5. #define GPUD_CALL __stdcall
  6. #else
  7. #define GPUD_API extern "C"
  8. #define GPUD_CALL
  9. #endif
  10. GPUD_API __global__ void gpud_freq_shift_kernel(
  11. const float2* __restrict__ in,
  12. float2* __restrict__ out,
  13. int n,
  14. double phase_inc,
  15. double phase_start
  16. ) {
  17. int idx = blockIdx.x * blockDim.x + threadIdx.x;
  18. if (idx >= n) return;
  19. double phase = phase_start + phase_inc * (double)idx;
  20. float si, co;
  21. sincosf((float)phase, &si, &co);
  22. float2 v = in[idx];
  23. out[idx].x = v.x * co - v.y * si;
  24. out[idx].y = v.x * si + v.y * co;
  25. }
  26. GPUD_API int GPUD_CALL gpud_launch_freq_shift_cuda(
  27. const float2* in,
  28. float2* out,
  29. int n,
  30. double phase_inc,
  31. double phase_start
  32. ) {
  33. if (n <= 0) return 0;
  34. const int block = 256;
  35. const int grid = (n + block - 1) / block;
  36. gpud_freq_shift_kernel<<<grid, block>>>(in, out, n, phase_inc, phase_start);
  37. return (int)cudaGetLastError();
  38. }
  39. GPUD_API __global__ void gpud_fm_discrim_kernel(
  40. const float2* __restrict__ in,
  41. float* __restrict__ out,
  42. int n
  43. ) {
  44. int idx = blockIdx.x * blockDim.x + threadIdx.x;
  45. if (idx >= n - 1) return;
  46. float2 prev = in[idx];
  47. float2 curr = in[idx + 1];
  48. float re = prev.x * curr.x + prev.y * curr.y;
  49. float im = prev.x * curr.y - prev.y * curr.x;
  50. out[idx] = atan2f(im, re);
  51. }
  52. GPUD_API int GPUD_CALL gpud_launch_fm_discrim_cuda(
  53. const float2* in,
  54. float* out,
  55. int n
  56. ) {
  57. if (n <= 1) return 0;
  58. const int block = 256;
  59. const int grid = (n + block - 1) / block;
  60. gpud_fm_discrim_kernel<<<grid, block>>>(in, out, n);
  61. return (int)cudaGetLastError();
  62. }
  63. GPUD_API __global__ void gpud_decimate_kernel(
  64. const float2* __restrict__ in,
  65. float2* __restrict__ out,
  66. int n_out,
  67. int factor
  68. ) {
  69. int idx = blockIdx.x * blockDim.x + threadIdx.x;
  70. if (idx >= n_out) return;
  71. out[idx] = in[idx * factor];
  72. }
  73. __device__ __constant__ float gpud_fir_taps[256];
  74. __global__ void gpud_fir_kernel(
  75. const float2* __restrict__ in,
  76. float2* __restrict__ out,
  77. int n,
  78. int num_taps
  79. ) {
  80. int idx = blockIdx.x * blockDim.x + threadIdx.x;
  81. if (idx >= n) return;
  82. float acc_r = 0.0f;
  83. float acc_i = 0.0f;
  84. for (int k = 0; k < num_taps; ++k) {
  85. int src = idx - k;
  86. if (src < 0) break;
  87. float2 v = in[src];
  88. float t = gpud_fir_taps[k];
  89. acc_r += v.x * t;
  90. acc_i += v.y * t;
  91. }
  92. out[idx] = make_float2(acc_r, acc_i);
  93. }
  94. GPUD_API int GPUD_CALL gpud_upload_fir_taps_cuda(const float* taps, int n) {
  95. if (!taps || n <= 0 || n > 256) return -1;
  96. cudaError_t err = cudaMemcpyToSymbol(gpud_fir_taps, taps, (size_t)n * sizeof(float));
  97. return (int)err;
  98. }
  99. GPUD_API int GPUD_CALL gpud_launch_fir_cuda(
  100. const float2* in,
  101. float2* out,
  102. int n,
  103. int num_taps
  104. ) {
  105. if (n <= 0 || num_taps <= 0 || num_taps > 256) return 0;
  106. const int block = 256;
  107. const int grid = (n + block - 1) / block;
  108. gpud_fir_kernel<<<grid, block>>>(in, out, n, num_taps);
  109. return (int)cudaGetLastError();
  110. }
  111. GPUD_API int GPUD_CALL gpud_launch_decimate_cuda(
  112. const float2* in,
  113. float2* out,
  114. int n_out,
  115. int factor
  116. ) {
  117. if (n_out <= 0 || factor <= 0) return 0;
  118. const int block = 256;
  119. const int grid = (n_out + block - 1) / block;
  120. gpud_decimate_kernel<<<grid, block>>>(in, out, n_out, factor);
  121. return (int)cudaGetLastError();
  122. }
  123. GPUD_API __global__ void gpud_am_envelope_kernel(
  124. const float2* __restrict__ in,
  125. float* __restrict__ out,
  126. int n
  127. ) {
  128. int idx = blockIdx.x * blockDim.x + threadIdx.x;
  129. if (idx >= n) return;
  130. float2 v = in[idx];
  131. out[idx] = sqrtf(v.x * v.x + v.y * v.y);
  132. }
  133. GPUD_API int GPUD_CALL gpud_launch_am_envelope_cuda(
  134. const float2* in,
  135. float* out,
  136. int n
  137. ) {
  138. if (n <= 0) return 0;
  139. const int block = 256;
  140. const int grid = (n + block - 1) / block;
  141. gpud_am_envelope_kernel<<<grid, block>>>(in, out, n);
  142. return (int)cudaGetLastError();
  143. }
  144. GPUD_API __global__ void gpud_ssb_product_kernel(
  145. const float2* __restrict__ in,
  146. float* __restrict__ out,
  147. int n,
  148. double phase_inc,
  149. double phase_start
  150. ) {
  151. int idx = blockIdx.x * blockDim.x + threadIdx.x;
  152. if (idx >= n) return;
  153. double phase = phase_start + phase_inc * (double)idx;
  154. float si, co;
  155. sincosf((float)phase, &si, &co);
  156. float2 v = in[idx];
  157. out[idx] = v.x * co - v.y * si;
  158. }
  159. GPUD_API int GPUD_CALL gpud_launch_ssb_product_cuda(
  160. const float2* in,
  161. float* out,
  162. int n,
  163. double phase_inc,
  164. double phase_start
  165. ) {
  166. if (n <= 0) return 0;
  167. const int block = 256;
  168. const int grid = (n + block - 1) / block;
  169. gpud_ssb_product_kernel<<<grid, block>>>(in, out, n, phase_inc, phase_start);
  170. return (int)cudaGetLastError();
  171. }