c - 使用 FFTW 对 2D 复数数组进行 FFT

标签 c fftw

我在 C 中有一个大小为 1001(行)* 144(列)的 2D 双复数数组。我想对每一行应用 FFT,最后希望以 4096*144 格式输出。这里N点=4096。最后将结果与matlab输出进行比较。 我正在使用著名的 FFTW C 库。我已阅读tutorials但无法理解如何正确使用。我应该使用哪个例程,例如 1D 例程或 2D 例程,然后如何?

#更新

double complex reshaped_calib[1001][144]={{1.0 + 0.1 * I}};

double complex** input; //[4096][144];
 // have to take dynamic array as I was getting segmentation fault here   
input = malloc(4096 * sizeof(double complex*));
for (i = 0; i < 4096; i++) {
  input[i] = malloc(144* sizeof(double complex));
}
// input is array I am sending to fftw to apply fft

for (i= 0; i< 1001; i++)
{
  for (j= 0; j< 144; j++)
  {
    input[i][j]=reshaped_calib[i][j];
  }
}

// Pad the extra rows
for (i= 1001; i< 4096; i++)
{
  for (j= 0; j< 144; j++)
  {
    input[i][j] = 0.0;
  }
}

 int N=144, howmany=4096;
 fftw_complex* data = (fftw_complex*)  fftw_malloc(N*howmany*sizeof(fftw_complex));

i=0,j=0;
int dataCount=0;
for(i=0;i<4096;i++)
    {
        for(j=0;j<144;j++)
        {
            data[dataCount++]=CMPLX(creal(input[i][j]),cimag(input[i][j]));
        }
    }
int istride=1, idist=N;// as in C data as row major
// ... if data is column-major, set istride=howmany, idist=1
//    if data is row-major, set istride=1, idist=N
fftw_plan p = fftw_plan_many_dft(1,&N,howmany,data,NULL,howmany,1,data,NULL,howmany,1,FFTW_FORWARD,FFTW_MEASURE);
fftw_execute(p);

最佳答案

您尝试用

填充数组
int pad = 4096;
memset(reshaped_calib, 0.0, pad * sizeof(double complex)); //zero padding

本质上会覆盖数组reshape_calib的前4096个值。为了获得正确的填充,您需要将 2D 数组的大小扩展到所需的 4096 x 144 大小,并仅将输入 1001 x 144 范围之外的条目设置为零。

由于您只是扩展行数,因此可以使用以下内容进行填充:

double complex input[1001][144]={{1.0 + 0.1 * I}};

// Allocate storage for the larger 4096x144 2D size, and copy the input.
double complex reshaped_calib[4096][144];
for (int row = 0; row < 1001; row++)
{
  for (int col = 0; col < 144; col++)
  {
    reshaped_calib[row][col] = input[row][col];
  }
}
// Pad the extra rows
for (int row = 1001; row < 4996; row++)
{
  for (int col = 0; col < 144; col++)
  {
    reshaped_calib[row][col] = 0.0;
  }
}

也就是说,如果您想要分别对每一行进行 1D FFT,则应该使用 fftw_plan_dft_1d 并多次调用 fftw_execute,或者使用 fftw_plan_many_dft 。这有更详细的描述in this answer通过 @Dylan .

关于c - 使用 FFTW 对 2D 复数数组进行 FFT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49976386/

相关文章:

c - 如何有效地修补外部库并将其编译到 C 项目的 Makefile 中?

c++ - FFTW 函数 n2fv_64

C - while 循环内 isdigit() 调用后递增的奇怪行为

c - 使用 strncat 进行字符串连接会导致符号错误

c - 多个线程在没有锁的情况下递增共享变量但返回 "unexpected"输出

c - C 中的 FFTW2 例程问题

c - FFTW 1D 的结果以转置方式存储

c++ - Windows 64 位上的 fftw3

python - 如何在 Python 线程池中使用初始化器

c - 如何在c中将一定长度的字符串放入二维数组中