- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我对在 cuFFT 中转换音频信号以获得创建频谱图所需的数据很感兴趣。在转换之前尝试从 float 转换为 cufftReal 时,我似乎丢失了所有音频数据。另外,我认为我的实际方法对于获得正确的结果并不正确。这是我目前所拥有的:
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);
}
最佳答案
我可以看到一些问题。
cufftPlanMany
的返回代码。您也没有对最后一个 cudaMemcpy
调用进行正确的错误检查。这 2 个分配的大小应该匹配。他们不会:
cudaMalloc((void**)&data, sizeof(cufftComplex) * nSamples);
cufftComplex *hostOutputData = (cufftComplex*)malloc((DATASIZE / 2 + 1) * batch * sizeof(cufftComplex));
上面第二个分配的大小是正确的,应该为第一个分配复制。
您在这一行中有一个基本的拼写错误。你应该在我指出的地方加上括号:
cudaMemcpy(hostOutputData, data, (DATASIZE / 2) + 1 * batch * sizeof(cufftComplex), cudaMemcpyDeviceToHost);
^ ^
所以 expects当您寻求调试帮助时,请提供 MCVE .为您创建 main
例程、合成数据、猜测您包含的 header 以及 sf_count_t
是什么以及您正在尝试什么,这不是其他人的责任一般完成。
您的例程没有考虑 channel
。同样,我也没有,因为这不是这里的问题。但是使用多 channel 数据可能会对代码产生影响,这取决于数据布局。
当我解决上述问题时,我得到了一些对我有意义的东西。
$ 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
$
所示频谱在 4.5Hz 和 6.0Hz 处有尖峰,正如我们根据合成输入数据的组成所预期的那样。请注意,这个问题似乎与光谱计算的机制无关,我也不是这方面的专家。目的是生成一组输出数据,使我们能够轻松验证结果。我并不是说这种光谱计算对任何特定目的都有用,或者根据任何数学都是正确的。此处的目的是根除代码中的潜在 cuda 错误。
作为附加评论,您的代码设置为对任意长度的输入数据集大小执行分段 FFT(我的解释,基于您对 batch
的使用)。这就是我制作结果的方式。我认为这样做是合理的,但我不知道它是否对您的特定用例有意义。
关于CUDA:如何在 cuFFT 中使用浮点音频数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27324686/
我在网上搜索但没有找到任何合适的文章解释如何使用 javascript 使用 WCF 服务,尤其是 WebScriptEndpoint。 任何人都可以对此给出任何指导吗? 谢谢 最佳答案 这是一篇关于
我正在编写一个将运行 Linux 命令的 C 程序,例如: cat/etc/passwd | grep 列表 |剪切-c 1-5 我没有任何结果 *这里 parent 等待第一个 child (chi
所以我正在尝试处理文件上传,然后将该文件作为二进制文件存储到数据库中。在我存储它之后,我尝试在给定的 URL 上提供文件。我似乎找不到适合这里的方法。我需要使用数据库,因为我使用 Google 应用引
我正在尝试制作一个宏,将下面的公式添加到单元格中,然后将其拖到整个列中并在 H 列中复制相同的公式 我想在 F 和 H 列中输入公式的数据 Range("F1").formula = "=IF(ISE
问题类似于this one ,但我想使用 OperatorPrecedenceParser 解析带有函数应用程序的表达式在 FParsec . 这是我的 AST: type Expression =
我想通过使用 sequelize 和 node.js 将这个查询更改为代码取决于在哪里 select COUNT(gender) as genderCount from customers where
我正在使用GNU bash,版本5.0.3(1)-发行版(x86_64-pc-linux-gnu),我想知道为什么简单的赋值语句会出现语法错误: #/bin/bash var1=/tmp
这里,为什么我的代码在 IE 中不起作用。我的代码适用于所有浏览器。没有问题。但是当我在 IE 上运行我的项目时,它发现错误。 而且我的 jquery 类和 insertadjacentHTMl 也不
我正在尝试更改标签的innerHTML。我无权访问该表单,因此无法编辑 HTML。标签具有的唯一标识符是“for”属性。 这是输入和标签的结构:
我有一个页面,我可以在其中返回用户帖子,可以使用一些 jquery 代码对这些帖子进行即时评论,在发布新评论后,我在帖子下插入新评论以及删除 按钮。问题是 Delete 按钮在新插入的元素上不起作用,
我有一个大约有 20 列的“管道分隔”文件。我只想使用 sha1sum 散列第一列,它是一个数字,如帐号,并按原样返回其余列。 使用 awk 或 sed 执行此操作的最佳方法是什么? Accounti
我需要将以下内容插入到我的表中...我的用户表有五列 id、用户名、密码、名称、条目。 (我还没有提交任何东西到条目中,我稍后会使用 php 来做)但由于某种原因我不断收到这个错误:#1054 - U
所以我试图有一个输入字段,我可以在其中输入任何字符,但然后将输入的值小写,删除任何非字母数字字符,留下“。”而不是空格。 例如,如果我输入: 地球的 70% 是水,-!*#$^^ & 30% 土地 输
我正在尝试做一些我认为非常简单的事情,但出于某种原因我没有得到想要的结果?我是 javascript 的新手,但对 java 有经验,所以我相信我没有使用某种正确的规则。 这是一个获取输入值、检查选择
我想使用 angularjs 从 mysql 数据库加载数据。 这就是应用程序的工作原理;用户登录,他们的用户名存储在 cookie 中。该用户名显示在主页上 我想获取这个值并通过 angularjs
我正在使用 autoLayout,我想在 UITableViewCell 上放置一个 UIlabel,它应该始终位于单元格的右侧和右侧的中心。 这就是我想要实现的目标 所以在这里你可以看到我正在谈论的
我需要与 MySql 等效的 elasticsearch 查询。我的 sql 查询: SELECT DISTINCT t.product_id AS id FROM tbl_sup_price t
我正在实现代码以使用 JSON。 func setup() { if let flickrURL = NSURL(string: "https://api.flickr.com/
我尝试使用for循环声明变量,然后测试cols和rols是否相同。如果是,它将运行递归函数。但是,我在 javascript 中执行 do 时遇到问题。有人可以帮忙吗? 现在,在比较 col.1 和
我举了一个我正在处理的问题的简短示例。 HTML代码: 1 2 3 CSS 代码: .BB a:hover{ color: #000; } .BB > li:after {
我是一名优秀的程序员,十分优秀!