简体   繁体   English

为什么FFTW输入非零,输出返回全零(C ++)

[英]why is FFTW input non-zero, output returns all zeros (C++)

Ok, so I have been working at the following problem for a while now and can't seem to come to an answer. 好的,所以我已经在下面的问题上工作了一段时间了,似乎无法解决。 In short, I am implementing tomographic imaging for a radar signal, and can't seem to get FFTW to give me an output other than zeros. 简而言之,我正在为雷达信号实现断层成像,并且似乎无法获得FFTW来提供除零以外的输出。 I am implementing via C++ and using the FFTW3 library. 我正在通过C ++并使用FFTW3库来实现。

The input for now is a simulated point scatterer at the origin of the scene, which has a phase response of all real ones (see rxphase variable). 现在的输入是场景起点处的模拟点散射器,该点具有所有真实相位的相位响应(请参阅rxphase变量)。 Because of bandwidth used etc, the IFFT of the filtered received signal should be fairly large (I haven't scaled it yet) but I am getting zeros for each pulse. 由于使用的带宽等原因,滤波后的接收信号的IFFT应该相当大(我尚未对其进行缩放),但是每个脉冲的取值为零。

excerpt from my code: 摘录自我的代码:

void ifftshift(std::vector<phdata> &argin, std::size_t count){
  int k = 0;
  int c = (int)floor((float)count/2);

  if (count % 2 == 0)
  {
    for (k=0; k<c;k++)
    {
      complex<double> tmp = argin[k];
      argin[k] = argin[k+c];
      argin[k+c] = tmp;
    }
  }
  else
  {
    complex<double> tmp = argin[count-1];
    for (k=c-1; k>=0; k--)
    {
      argin[c+k+1] = argin[k];
      argin[k] = argin[c+k];
    }
    argin[c] = tmp;
  }
};

void main(){

  std::vector<complex<double> > filteredData;
  // Number of pulses across the aperture
  std::int pulses = 11;
  // Define the frequency vector
  std::vector<double> freq;
  std::double freqmin = 9 * pow(10,9);
  std::double freqmax = 11 * pow(10,9);
  std::vector<complex<double> > outData;
  std::vector<complex<double> > rxphase;

  for  (int i = 0; i<64; i++)
  {
    freq.push_back(freqmin + (((freqmax-freqmin)/64)*i));
  }

  // Create a (samples X pulses) array of complex doubles
  rxphase.assign(64, std::vector<complex<double> > (11, {1,0}) );

  fftw_complex *in, *out;
  fftw_plan plan;

  in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * image.Nfft );
  out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * image.Nfft );
  plan = fftw_plan_dft_1d(image.Nfft, in, out, FFTW_BACKWARD, FFTW_MEASURE);

  // iterate through pulses to back project across the aperture
  for ( int i=0; i<=pulses; i++ )
  {
    // Reset vectors for each pulse
    filteredData.clear();
    outData.clear();

    for ( int ii=0; ii<freq.size(); ii++ )
    {
      // Apply simple ramp filter
      filteredData.push_back(freq[ii]*rxphase[ii][i]);
    }

    // Shift the data to put the center frequency at DC position (zero index)
    ifftshift(filteredData, filteredData.size());

    for (int ii=0; ii<filteredData.size(); ii++)
    {
      std::cout << filteredData[ii] ;
    }
    std::cout << std::endl;
    // filteredData is what I expect it to be.

    // Recast the vector to the type needed for FFTW and compute FFT
    in = reinterpret_cast<fftw_complex*>(&filteredData[0]);

    for (int ii=0; ii<filteredData.size(); ii++)
    {
      std::cout << "(" << (in[ii])[0] << "," << (in[ii])[1] << ");";
    }
    std::cout << std::endl;
    // values for in are the same as for filteredData, as expected.

    fftw_execute(plan);

    for (int ii=0; ii<filteredData.size(); ii++)
    {
      std::cout << "(" << (out[ii])[0] << "," << (out[ii])[1] << ");";
    }
    std::cout << std::endl;
    // The values for out are all (0,0)
  }

  fftw_destroy_plan(plan);
  //fftw_free(in); error here
  //fftw_free(out); error here
};

any help would be greatly appreciated. 任何帮助将不胜感激。

EDIT: code should be a little more complete. 编辑:代码应该更完整一些。

The confusion probably originates with the line 混乱可能源于生产线

in = reinterpret_cast<fftw_complex*>(&filteredData[0]);

While this updates the local variable in to points to the area in memory where your data is stored (the storage buffer of the filteredData vector), it does not update the pointer kept internally by the plan . 虽然这种更新本地变量in ,以点到您的数据存储在内存中的区域(存储缓冲区的filteredData矢量),它不更新指针由内部保持plan The area of memory that FFTW knows about and uses as input buffer is thus still the one you had originally allocated with FFTW知道并用作输入缓冲区的内存区域因此仍然是您最初分配的内存区域

in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * image.Nfft );

and specified as argument to 并指定为

fftw_plan_dft_1d(image.Nfft, in, out, FFTW_BACKWARD, FFTW_MEASURE);
//                           ^^

Note that this memory area remains uninitialized (which may happen to be zeros) throughout your program. 请注意,在整个程序中,该存储区都保持未初始化状态( 可能恰好为零)。

You should instead write directly to the in input buffer with: 您应该使用以下命令直接写入in输入缓冲区:

for ( int ii=0; ii<freq.size(); ii++ )
{
    // Apply simple ramp filter
    std::complex<double> value = freq[ii]*rxphase[ii][i];
    in[ii][0] = value.real();
    in[ii][1] = value.imag();
}

Needless to say that in this case the in = reinterpret_cast<fftw_complex*>(&filteredData[0]) line should also be removed. 不用说,在这种情况下,也应该删除in = reinterpret_cast<fftw_complex*>(&filteredData[0])行。

Alternatively if you are using a compiler where fftw_complex and std::complex<double> are binary compatible (see FFTW documentation of Complex numbers type ), you may prefer: 或者,如果您使用的编译器中fftw_complexstd::complex<double>是二进制兼容的(请参阅FFTW文档,复数类型type ),则您可能更喜欢:

std::complex<double>* filteredData = reinterpret_cast<std::complex<double>*>(in);
for ( int ii=0; ii<freq.size(); ii++ )
{
    // Apply simple ramp filter
    filteredData[ii] = freq[ii]*rxphase[ii][i];
}

对于思考这个的快速和肮脏的方式,为您打造fftw_planfftw_plan_dft_1d 第一 ,修改有效输入数组的内容fftw_plan_dft_1d为SleuthEye提到, 然后调用fftw_execute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM