测试以下代码,我得到正确的答案:
#include <fftw3.h>
void dump_vector(float *a, int size)
{
int i;
for (i = 0; i < size; i++) {
printf("%f\t", a[i]);
}
printf("\n");
}
int main()
{
float a[] = {1, 2, 3, 4};
dump_vector(a, 4);
fftw_plan plan = fftw_plan_r2r_1d(2 * 2, a, a, FFTW_REDFT10, FFTW_ESTIMATE);
fftw_execute(plan);
dump_vector(a, 4);
}
结果:
./main
1.000000 2.000000 3.000000 4.000000
3.992188 4.501953 -334878449985782808576.000000 3.886292
但是我想为dct做一个天真的包装,所以:
#include <fftw3.h>
void dump_vector(float *a, int size)
{
int i;
for (i = 0; i < size; i++) {
printf("%f\t", a[i]);
}
printf("\n");
}
void dct(float * array, int xsize, int ysize)
{
fftw_plan plan = fftw_plan_r2r_1d(xsize * ysize, array, array, FFTW_REDFT10, FFTW_ESTIMATE);
fftw_execute(plan);
}
int main()
{
float a[] = {1, 2, 3, 4};
dump_vector(a, 4);
dct(a, 2, 2);
dump_vector(a, 4);
}
结果:
./main
1.000000 2.000000 3.000000 4.000000
3.992188 4.501953 -334878449985782808576.000000 3.886292
[1] 10880 segmentation fault ./main
有什么我想念的吗?
我需要执行此操作,因为然后我想生成一个.so
文件以供在另一个应用程序中使用。
这个问题比预期的要简单得多:fftw_plan_r2r_1d
在的数组上执行DCT / DST double
。
数组float a[]
太小了:4个float
存储在4x4 = 16个字节中,而fftw_plan_r2r_1d
期望存储4个double
(32个字节)。因此,fftw_plan_r2r_1d
将尝试读取传递实际数组的末尾。这会触发未定义的行为,例如细分错误或错误的结果。
两种解决方案:
float a[]
为double a[]
与dump_vector(float *a,...
对dump_vector(double *a,...
fftwf_plan_r2r_1d()
建立一个fftwf_plan
用于数组float
。请参阅http://www.fftw.org/doc/Precision.html:libfftw3f.a
必须通过添加-lfftw3f
到编译器命令来链接该库。本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句