c - FFTW 产生真实的而不是复杂的输出

标签 c complex-numbers fftw

我使用以下代码来执行复数数组的 COMPLEX IFFT(我必须获得复数结果):

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <math.h>
#include <complex.h>
#include <fftw3.h>


 int main(void)
{
fftw_complex *in;
fftw_complex *out;
double re,im;
int size;
int i=0;
FILE *file;
fftw_plan ifft;

printf("Insert size");
if (scanf("%d", &size) != 1 || size < 1)
    return 1;

in = fftw_malloc(sizeof(*in)*size);
out = fftw_malloc(sizeof(*out)*size);

file = fopen("numbers.txt", "r");

for (i = 0; i < size && fscanf(file, "%lf+%lf*I\n", &re, &im) == 2; i++)
{
    in[i]= re+im*I;
}

fclose(file);
// Error if i != size?


ifft = fftw_plan_dft_1d(size, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);

fftw_execute(ifft);

printf("Input:    \tOutput:\n");
for(i=0; i<size; i++)
{
printf("%lf+%lf*I\t%lf+%lf*I\n", creal(in[i]), cimag(in[i]), creal(out[i]), cimag(out[i]));
}

fftw_destroy_plan(ifft);
fftw_free(in);
fftw_free(out);
//free(out);

return 0;
}

问题是我总是得到一个实值转换数组(虚部 = 0)。 例如:

    Input:      Output:
    0.000000+0.000000*I -3.018122+0.000000*I
0.734204+-1.072180*I    -2.106427+0.000000*I
-0.055891+1.938470*I    0.808331+0.000000*I
1.117910+0.471070*I -0.171778+0.000000*I
-1.916920+1.203730*I    -0.184422+0.000000*I
-0.114476+0.635334*I    3.623205+0.000000*I
-1.151660+0.278201*I    10.225375+0.000000*I
-0.712223+-0.880753*I   1.746433+0.000000*I
1.179990+0.000000*I -7.119782+0.000000*I
-0.712223+0.880753*I    -8.238863+0.000000*I
-1.151660+-0.278201*I   2.578253+0.000000*I
-0.114476+-0.635334*I   -6.742277+0.000000*I
-1.916920+-1.203730*I   -0.293074+0.000000*I
1.117910+-0.471070*I    -7.627715+0.000000*I
-0.055891+-1.938470*I   6.443361+0.000000*I
0.734204+1.072180*I 10.077501+0.000000*I

我该如何修复它,以获得复杂的结果?

最佳答案

碰巧您的输入信号具有以下 property :

formula

相应频率的输入是相互共轭的。 因此,它是真实信号的 FFT:无需担心输出的虚部为空。这是完全正常的。

更改输入文件的一行,事情就会发生变化。

关于c - FFTW 产生真实的而不是复杂的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27740255/

相关文章:

matlab - MATLAB 中虚部为空时的复数可视化

java - Riemann Siegel Formula in Java的实现,好奇改进余数项

c++ - Reinterpret_cast 在 C++ 中的使用

c - 在 C 中使用 FFTW 求解泊松方程时失去径向对称性

c++ - 如何在 ubuntu 的 vs 代码中链接 c++ 的 fftw3 库?

c - 在集群的单个节点上运行 OpenMP

c - 如何将源文件路径提供给 gcc?

c - 我的程序不接受 scanf 命令的输入

c - 在C中: 2 printf output when all I want is one.

c++ - 提供 `std::complex` 时如何避免嵌入 `T=std::complex<Q>` ?