title: 2D fourier transform with Eigen and FFTW
body: <p>I'm trying to do a real-valued 2d Fourier Transform with FFTW. My data is stored in a dynamically sized Eigen Matrix. Here's the wrapper class I wrote:</p>
  
  <p>FFT2D.h:</p>
  
  <pre><code>#include &lt;Eigen&gt;
  
  class FFT2D {
  public:
  
      enum FFT_TYPE {FORWARD=0, REVERSE=1};
      FFT2D(EMatrix &amp;input, EMatrix &amp;output, FFT_TYPE type_ = FORWARD);
      ~FFT2D();
  
      void execute();
  
  private:
      EMatrix&amp; input;
      EMatrix&amp; output;
      fftw_plan plan;
      FFT_TYPE type;
  };
  </code></pre>
  
  <p>FFT2D.cpp:</p>
  
  <pre><code>#include "FFT2D.h"
  #include &lt;fftw3.h&gt;
  #include "Defs.h"
  
  
  FFT2D::FFT2D(EMatrix &amp;input_, EMatrix &amp;output_, FFT_TYPE type_)
          : type(type_), input(input_), output(output_) {
  
      if (type == FORWARD)
          plan = fftw_plan_dft_2d((int) input.rows(), (int) input.cols(),
                  (fftw_complex *) &amp;input(0), (fftw_complex *) &amp;output(0),
                  FFTW_FORWARD, FFTW_ESTIMATE);
      else
          // placeholder for ifft-2d code, unwritten
  }
  
  
  FFT2D::~FFT2D() {
      fftw_destroy_plan(plan);
  }
  
  void FFT2D::execute() {
      fftw_execute(plan);  // seg-fault here
  }
  </code></pre>
  
  <p>And a definition for <code>EMatrix</code>:</p>
  
  <pre><code>typedef Eigen::Matrix&lt;double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor&gt; EMatrix;
  </code></pre>
  
  <p>The problem is, is I'm getting a seg fault in <code>FFT2D::execute()</code>. I know I'm setting something up wrong in the constructor, and I've tried a number of different ways, but I can't seem to make any headway on this. </p>
  
  <p>Things I've tried include: changing <code>EMatrix</code> typedef to <code>Eigen::ColMajor</code>, passing <code>(fftw_complex *) input.data()</code> to <code>fftw_plan_dft_2d</code>, using different fftw plans (<code>fftw_plan_dft_r2c_2d</code>).</p>
  
  <p>My C++ is (clearly) rusty, but at the end of the day what I need is to do a 2D FT on a real-valued 2D Eigen Matrix of doubles. Thanks in advance.</p>