Search code examples
c++fftwdft

Why does my 2D IDFT produce double the expected amplitudes? (FFTW)


I am using a complex-to-real 2D IDFT to visualize complex signals as an image. I initialize the complex frequency domain by manually setting modes. However, some modes produce real output that seems to be double what is expected.

My code:

int N = 8;
int logical_width = N / 2 + 1; // Logical width of frequency domain data

double* T = new double[N * N];
fftw_complex* F = (fftw_complex*)fftw_alloc_complex(N * logical_width);

fftw_plan plan = fftw_plan_dft_c2r_2d(N, N, F, T, FFTW_MEASURE);

// Initialize all frequency modes to 0
for (int i = 0; i < N * logical_width; i++) {
    F[i][REAL] = 0.0;
    F[i][IMAG] = 0.0;
}

F[1][REAL] = 16.0; // Set mode k[0, 1]

fftw_execute(plan);

printTime(T, N); // Print time domain to console

Output of printTime():
Output of printTime():

The amplitude of the signal seems to be 32 after the IDFT. However, I would expect it to be 16, given that the only contributing mode is k[0, 1] = 16 + 0i.

Why does this happen? Should I transform the signals somehow before executing the IDFT?


Solution

  • You are applying a C2R transform. As I explained in my answer to your previous question, it expects a conjugate symmetric input. You provide half the input, the other half is assumed to be the conjugate symmetric of your input. Thus, you are setting two modes, not one, each with a magnitude of 16. Together they form a sinusoid of amplitude 32.

    Note that FFTW’s inverse DFT doesn’t normalize. There are different definitions for the DFT, some put the normalization in the forward transform, some put it in the inverse transform. FFTW doesn’t normalize at all. This leads to IFFT(FFT(f)) = Nf (with N the number of samples). You need to manually normalize somewhere for the equality to hold. Most commonly the inverse transform is normalized. This would lead, in your case, to a sinusoid with an amplitude of 32/N, and is what you would see in most signal processing textbooks. See for example page 7 in this PDF.