【问题标题】:FFT of 2D complex array using FFTW使用 FFTW 对二维复数数组进行 FFT
【发布时间】:2018-04-23 08:19:12
【问题描述】:

我在 C 中有一个大小为 1001(行)*144(列)的二维双复数数组。我想对每一行应用 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);

【问题讨论】:

  • 您只需将输入数组补零至所需大小 (4096x144),然后使用简单的复数到复数 2D FFT。
  • @PaulR 我试图实现你的想法。明显做错了什么。出现分段错误。可以看看吗?
  • 如果我们要帮助您调试,您需要发布minimal reproducible example

标签: c fftw


【解决方案1】:

您尝试用

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

基本上覆盖数组reshaped_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_dftin this answer@Dylan 对此进行了更详细的描述。

【讨论】:

  • 谢谢。我更新了代码。但是我必须对输入数组使用动态内存分配,否则我会在该部分出现分段错误。但是现在当我创建 fft 例程“fftw_plan_many_dft”时,我在那里遇到了分段错误。从错误来看,它似乎是一个不兼容的变量类型:(你能再帮我一次吗?
  • 解决了分割问题。我将输入数组从 2d 展平为 1D,所有警告和分段错误都消失了。再次感谢:)
  • 还有一个问题。现在没有执行错误。但是输出是空的。我的意思是所有的值都是零。任何想法,为什么我会得到这个?
  • 您输入的大多数行(除了第一行)都是零,并且零向量的 FFT 是零向量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
相关资源
最近更新 更多