简体   繁体   English

CUDA:如何在cuFFT中使用浮动音频数据?

[英]CUDA: How do I use float audio data with cuFFT?

I'm interested in transforming an audio signal in cuFFT to get the data necessary to create a spectrogram. 我对在cuFFT中转换音频信号以获取创建频谱图所需的数据感兴趣。 I seem to be losing all of my audio data when trying to convert from float to cufftReal before the transform. 尝试在转换前从float转换为cufftReal时,似乎丢失了所有音频数据。 Also, I don't think my actual approach is correct for getting the correct result. 另外,我认为我的实际方法对于获得正确的结果并不正确。 Here is what I have so far: 这是我到目前为止的内容:

void process_data(float *h_in_data_dynamic, sf_count_t samples, int channels) {
int nSamples = (int)samples;
int DATASIZE = 512;
int batch = nSamples / DATASIZE;

cufftHandle plan;

//this makes the data become all 0's.
cufftReal *d_in_data;
cudaMalloc((void**)&d_in_data, sizeof(cufftReal) * nSamples);
cudaMemcpy(d_in_data, (cufftReal*)h_in_data_dynamic, sizeof(cufftReal) * nSamples, cudaMemcpyHostToDevice);


cufftComplex *data;
cudaMalloc((void**)&data, sizeof(cufftComplex) * nSamples);


cufftComplex *hostOutputData = (cufftComplex*)malloc((DATASIZE / 2 + 1) * batch * sizeof(cufftComplex));

if (cudaGetLastError() != cudaSuccess) {
    fprintf(stderr, "Cuda error: Failed to allocate\n");
    return;
}

int rank = 1;                           // --- 1D FFTs
int n[] = { DATASIZE };                 // --- Size of the Fourier transform
int istride = 1, ostride = 1;           // --- Distance between two successive input/output elements
int idist = DATASIZE, odist = (DATASIZE / 2) + 1; // --- Distance between batches
int inembed[] = { 0 };                  // --- Input size with pitch (ignored for 1D transforms)
int onembed[] = { 0 };                  // --- Output size with pitch (ignored for 1D transforms)

cufftPlanMany(&plan, rank, n,
              inembed, istride, idist,
              onembed, ostride, odist, CUFFT_R2C, batch);

/* Use the CUFFT plan to transform the signal in place. */
if (cufftExecR2C(plan, d_in_data, data) != CUFFT_SUCCESS) {
    fprintf(stderr, "CUFFT error: ExecC2C Forward failed");
    return;
}

cudaMemcpy(hostOutputData, data, (DATASIZE / 2) + 1 * batch * sizeof(cufftComplex), cudaMemcpyDeviceToHost);

for (int i=0; i < batch; i++)
    for (int j=0; j < (DATASIZE / 2 + 1); j++)
        printf("%i %i %f %f\n", i, j, hostOutputData[i*(DATASIZE / 2 + 1) + j].x, hostOutputData[i*(DATASIZE / 2 + 1) + j].y);

cufftDestroy(plan);
cudaFree(data);
cudaFree(d_in_data);
}

There are a few issues that I can see. 我可以看到一些问题。

  1. You should properly indent your code for readability. 您应该适当缩进代码以提高可读性。
  2. Any time you're having trouble, do proper error checking. 每当遇到麻烦时,请进行正确的错误检查。 It's a nitpick, but you didn't check the return code of the call to cufftPlanMany . 这是一个nitpick,但是您没有检查对cufftPlanMany的调用的返回码。 You also aren't doing proper error checking on the last cudaMemcpy call. 您也没有对最后一个cudaMemcpy调用进行正确的错误检查。
  3. The sizes of these 2 allocations should match. 这两个分配的大小应匹配。 They don't: 他们不:

     cudaMalloc((void**)&data, sizeof(cufftComplex) * nSamples); cufftComplex *hostOutputData = (cufftComplex*)malloc((DATASIZE / 2 + 1) * batch * sizeof(cufftComplex)); 

    the size of the second allocation above is the correct one, and it should be duplicated for the first one. 上面第二个分配的大小是正确的,应该与第一个重复。

  4. You have a basic typo in this line. 您在此行中有一个基本的错字。 You should have parenthesis where I have indicated: 您应该在我指出的地方加上括号:

     cudaMemcpy(hostOutputData, data, (DATASIZE / 2) + 1 * batch * sizeof(cufftComplex), cudaMemcpyDeviceToHost); ^ ^ 
  5. SO expects when you are seeking debugging help, that you provide an MCVE . SO 希望在寻求调试帮助时提供MCVE It's not the responsibility of others to create a main routine for you, and synthesize data, and guess at what headers you are including and what sf_count_t is, and what you are trying to accomplish generally. 别人没有责任为您创建一个main例程并合成数据,然后猜测您要包括的头文件以及sf_count_t是什么,以及您通常会尝试完成什么。

  6. Your routine is not taking into account channels . 您的例行工作没有考虑到channels Likewise I have not either, since that is the not the issue here. 同样,我也没有,因为这不是这里的问题。 But the use of multi-channel data will probably have an impact on the code, depending on data layout. 但是,根据数据布局的不同,使用多通道数据可能会对代码产生影响。

When I fix the above issues, I get something that makes sense to me. 解决以上问题后,我得到了对我有意义的信息。

$ cat t621.cu
#include <cufft.h>
#include <math.h>
#include <stdio.h>

#define FFTSIZE 512
#define DEBUG 0

typedef size_t sf_count_t;

void process_data(float *h_in_data_dynamic, sf_count_t samples, int channels) {
  int nSamples = (int)samples;
  int DATASIZE = FFTSIZE;
  int batch = nSamples / DATASIZE;

  cufftHandle plan;

  cufftReal *d_in_data;
  cudaMalloc((void**)&d_in_data, sizeof(cufftReal) * nSamples);
  cudaMemcpy(d_in_data, (cufftReal*)h_in_data_dynamic, sizeof(cufftReal) * nSamples, cudaMemcpyHostToDevice);

  cufftComplex *data;
  cudaMalloc((void**)&data, sizeof(cufftComplex) * batch * (DATASIZE/2 + 1));

  cufftComplex *hostOutputData = (cufftComplex*)malloc((DATASIZE / 2 + 1) * batch * sizeof(cufftComplex));

  if (cudaGetLastError() != cudaSuccess) {
    fprintf(stderr, "Cuda error: Failed to allocate\n");
    return;
  }

  int rank = 1;                           // --- 1D FFTs
  int n[] = { DATASIZE };                 // --- Size of the Fourier transform
  int istride = 1, ostride = 1;           // --- Distance between two successive input/output elements
  int idist = DATASIZE, odist = (DATASIZE / 2) + 1; // --- Distance between batches
  int inembed[] = { 0 };                  // --- Input size with pitch (ignored for 1D transforms)
  int onembed[] = { 0 };                  // --- Output size with pitch (ignored for 1D transforms)

  if(cufftPlanMany(&plan, rank, n,
              inembed, istride, idist,
              onembed, ostride, odist, CUFFT_R2C, batch) != CUFFT_SUCCESS){
    fprintf(stderr, "CUFFT error: Plan failed");
    return;
  }

/* Use the CUFFT plan to transform the signal in place. */
  if (cufftExecR2C(plan, d_in_data, data) != CUFFT_SUCCESS) {
    fprintf(stderr, "CUFFT error: ExecR2C Forward failed");
    return;
  }

  cudaMemcpy(hostOutputData, data, ((DATASIZE / 2) + 1) * batch * sizeof(cufftComplex), cudaMemcpyDeviceToHost);
  if (cudaGetLastError() != cudaSuccess) {
    fprintf(stderr, "Cuda error: Failed results copy\n");
    return;
  }

  float *spectrum = (float *)malloc((DATASIZE/2)*sizeof(float));
  for (int j = 0; j < (DATASIZE/2); j++) spectrum[j] = 0.0f;
  for (int i=0; i < batch; i++)
    for (int j=0; j < (DATASIZE / 2 + 1); j++){
#if DEBUG
        printf("%i %i %f %f\n", i, j, hostOutputData[i*(DATASIZE / 2 + 1) + j].x, hostOutputData[i*(DATASIZE / 2 + 1) + j].y);
#endif
        // compute spectral magnitude
        // note that cufft induces a scale factor of FFTSIZE
        if (j < (DATASIZE/2)) spectrum[j] += sqrt(pow(hostOutputData[i*(DATASIZE/2 +1) +j].x, 2) + pow(hostOutputData[i*(DATASIZE/2 +1) +j].y, 2))/(float)(batch*DATASIZE);
        }
  //assumes Fs is half of FFTSIZE, or we could pass Fs separately
  printf("Spectrum\n Hz:   Magnitude:\n");
  for (int j = 0; j < (DATASIZE/2); j++) printf("%.3f %.3f\n", j/2.0f, spectrum[j]);

  cufftDestroy(plan);
  cudaFree(data);
  cudaFree(d_in_data);
}

int main(){

  const int nsets = 20;
  const float sampling_rate = FFTSIZE/2;
  const float amplitude = 1.0;
  const float fc1 = 6.0;
  const float fc2 = 4.5;
  float *my_data;

  my_data = (float *)malloc(nsets*FFTSIZE*sizeof(float));
  //generate synthetic data that is a mix of 2 sine waves at fc1 and fc2 Hz
  for (int i = 0; i < nsets*FFTSIZE; i++)
    my_data[i] = amplitude*sin(fc1*(6.283/sampling_rate)*i)
               + amplitude*sin(fc2*(6.283/sampling_rate)*i);

  process_data(my_data, nsets*FFTSIZE, 1);
  return 0;
}


$ nvcc -arch=sm_20 -o t621 t621.cu -lcufft
$ ./t621
 Hz:   Magnitude:
0.000 0.000
0.500 0.000
1.000 0.000
1.500 0.000
2.000 0.000
2.500 0.000
3.000 0.000
3.500 0.000
4.000 0.000
4.500 0.500
5.000 0.000
5.500 0.000
6.000 0.500
6.500 0.000
7.000 0.000
7.500 0.000
8.000 0.000
8.500 0.000
9.000 0.000
9.500 0.000
10.000 0.000
10.500 0.000
11.000 0.000
11.500 0.000
12.000 0.000
12.500 0.000
13.000 0.000
13.500 0.000
14.000 0.000
14.500 0.000
15.000 0.000
15.500 0.000
16.000 0.000
16.500 0.000
17.000 0.000
17.500 0.000
18.000 0.000
18.500 0.000
19.000 0.000
19.500 0.000
20.000 0.000
20.500 0.000
21.000 0.000
21.500 0.000
22.000 0.000
22.500 0.000
23.000 0.000
23.500 0.000
24.000 0.000
24.500 0.000
25.000 0.000
25.500 0.000
26.000 0.000
26.500 0.000
27.000 0.000
27.500 0.000
28.000 0.000
28.500 0.000
29.000 0.000
29.500 0.000
30.000 0.000
30.500 0.000
31.000 0.000
31.500 0.000
32.000 0.000
32.500 0.000
33.000 0.000
33.500 0.000
34.000 0.000
34.500 0.000
35.000 0.000
35.500 0.000
36.000 0.000
36.500 0.000
37.000 0.000
37.500 0.000
38.000 0.000
38.500 0.000
39.000 0.000
39.500 0.000
40.000 0.000
40.500 0.000
41.000 0.000
41.500 0.000
42.000 0.000
42.500 0.000
43.000 0.000
43.500 0.000
44.000 0.000
44.500 0.000
45.000 0.000
45.500 0.000
46.000 0.000
46.500 0.000
47.000 0.000
47.500 0.000
48.000 0.000
48.500 0.000
49.000 0.000
49.500 0.000
50.000 0.000
50.500 0.000
51.000 0.000
51.500 0.000
52.000 0.000
52.500 0.000
53.000 0.000
53.500 0.000
54.000 0.000
54.500 0.000
55.000 0.000
55.500 0.000
56.000 0.000
56.500 0.000
57.000 0.000
57.500 0.000
58.000 0.000
58.500 0.000
59.000 0.000
59.500 0.000
60.000 0.000
60.500 0.000
61.000 0.000
61.500 0.000
62.000 0.000
62.500 0.000
63.000 0.000
63.500 0.000
64.000 0.000
64.500 0.000
65.000 0.000
65.500 0.000
66.000 0.000
66.500 0.000
67.000 0.000
67.500 0.000
68.000 0.000
68.500 0.000
69.000 0.000
69.500 0.000
70.000 0.000
70.500 0.000
71.000 0.000
71.500 0.000
72.000 0.000
72.500 0.000
73.000 0.000
73.500 0.000
74.000 0.000
74.500 0.000
75.000 0.000
75.500 0.000
76.000 0.000
76.500 0.000
77.000 0.000
77.500 0.000
78.000 0.000
78.500 0.000
79.000 0.000
79.500 0.000
80.000 0.000
80.500 0.000
81.000 0.000
81.500 0.000
82.000 0.000
82.500 0.000
83.000 0.000
83.500 0.000
84.000 0.000
84.500 0.000
85.000 0.000
85.500 0.000
86.000 0.000
86.500 0.000
87.000 0.000
87.500 0.000
88.000 0.000
88.500 0.000
89.000 0.000
89.500 0.000
90.000 0.000
90.500 0.000
91.000 0.000
91.500 0.000
92.000 0.000
92.500 0.000
93.000 0.000
93.500 0.000
94.000 0.000
94.500 0.000
95.000 0.000
95.500 0.000
96.000 0.000
96.500 0.000
97.000 0.000
97.500 0.000
98.000 0.000
98.500 0.000
99.000 0.000
99.500 0.000
100.000 0.000
100.500 0.000
101.000 0.000
101.500 0.000
102.000 0.000
102.500 0.000
103.000 0.000
103.500 0.000
104.000 0.000
104.500 0.000
105.000 0.000
105.500 0.000
106.000 0.000
106.500 0.000
107.000 0.000
107.500 0.000
108.000 0.000
108.500 0.000
109.000 0.000
109.500 0.000
110.000 0.000
110.500 0.000
111.000 0.000
111.500 0.000
112.000 0.000
112.500 0.000
113.000 0.000
113.500 0.000
114.000 0.000
114.500 0.000
115.000 0.000
115.500 0.000
116.000 0.000
116.500 0.000
117.000 0.000
117.500 0.000
118.000 0.000
118.500 0.000
119.000 0.000
119.500 0.000
120.000 0.000
120.500 0.000
121.000 0.000
121.500 0.000
122.000 0.000
122.500 0.000
123.000 0.000
123.500 0.000
124.000 0.000
124.500 0.000
125.000 0.000
125.500 0.000
126.000 0.000
126.500 0.000
127.000 0.000
127.500 0.000
$

The indicated spectrum has spikes at 4.5Hz and 6.0Hz, as we would expect based on the composition of the synthetic input data. 正如我们根据合成输入数据的组成所预期的那样,所示频谱在4.5Hz和6.0Hz处有尖峰。 Note that the question does not appear to be about the mechanics of spectral computation, and I am not an expert in that. 请注意,这个问题似乎与频谱计算的机制无关,我不是专家。 The purpose is to generate a set of output data that allows us to easily validate the results. 目的是生成一组输出数据,使我们可以轻松地验证结果。 I'm not suggesting this spectral computation is useful for any particular purpose, or correct according to any mathematics. 我并不是说这种频谱计算对于任何特定目的都是有用的,或者根据任何数学方法都是正确的。 The purpose here is to root out the underlying cuda errors in your code. 目的是在代码中根除潜在的cuda错误。

As an additional comment, your code was set up to do a piecewise FFT on an arbitrary length input data set size (my interpretation, based on your usage of batch ). 另外,您的代码设置为对任意长度的输入数据集大小(我的解释,基于您的batch用法)进行分段FFT。 So that is how I crafted my result. 这就是我制作结果的方式。 I think it's a reasonable thing to do, but whether it makes sense for your particular use-case, I don't know. 我认为这样做是合理的,但是对于您的特定用例是否有意义,我不知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM