racanu
racanu

Reputation: 89

fftwf_execute_dft_c2r writes the input buffer

I have the following code:

auto in = std::array<std::complex<float>, 60>();
in[0] = 10000.0f;
auto out = std::array<float, 100>();
auto plan = fftwf_plan_dft_c2r_2d(10, 10, reinterpret_cast<fftwf_complex*>(in.data()), out.data(), FFTW_ESTIMATE | FFTW_UNALIGNED);
fftwf_execute_dft_c2r(plan, reinterpret_cast<fftwf_complex*>(in.data()), out.data());

When I run it, my in array gets written (specifically the first column is set to 10000.0). Is this normal? Can I avoid the in array being written?

Upvotes: 2

Views: 620

Answers (1)

b-fg
b-fg

Reputation: 4137

As explained in the documentation, the input data will be overwritten when using c2r transforms

Second, the inverse transform (complex to real) has the side-effect of overwriting its input array, by default. Neither of these inconveniences should pose a serious problem for users, but it is important to be aware of them.

As noted above, the c2r transform destroys its input array even for out-of-place transforms. This can be prevented, if necessary, by including FFTW_PRESERVE_INPUT in the flags, with unfortunately some sacrifice in performance. This flag is also not currently supported for multi-dimensional real DFTs (next section).

So even if FFTW_PRESERVE_INPUT is used, it will not work in this case. From the FFTW_PRESERVE_INPUT documentation

FFTW_PRESERVE_INPUT specifies that an out-of-place transform must not change its input array. This is ordinarily the default, except for c2r and hc2r (i.e. complex-to-real) transforms for which FFTW_DESTROY_INPUT is the default. In the latter cases, passing FFTW_PRESERVE_INPUT will attempt to use algorithms that do not destroy the input, at the expense of worse performance; for multi-dimensional c2r transforms, however, no input-preserving algorithms are implemented and the planner will return NULL if one is requested.

Hence, the easy way to solve this is just make a copy of the input array before passing it to the plan function.

Upvotes: 3

Related Questions