0

I'm using the FFTW3 library in MSVS 2008 to do a r2c DFT (n=128) of some data. I already found out that just the first half of the output of real data DFTs is used... which seems to be correct if I'm looking at my output:

0-64 --> seems to be the real part of the transform of my input.

65-127 --> is always 4.8367e-026 (I don't know why I was expecting it to be zero as it's not used according to the FFTW doc)

So far it seems to work correctly but I want to draw a power density spectrum so I would need the imaginary part too, right? The problem is I wasn't able to find out how to access the imaginary part of the transform I thought it would be possible by just using:

for(int i=0; i < 128; i++)
{
    std::cout << "FFT Im-Part: " << *out[i][1] << "\n";
}

How can I do that?

Thanks for your help!

  • `out[i][0]` is the real part of complex bin i, `out[i][1]` is the imaginary part. Change your test code to: `std::cout << "FFT Im-Part: " << out[i][1] << "\n";` – Paul R Mar 06 '12 at 11:27
  • 1
    Thank you so much this worked out fine! Another question: how do the outputs 0-64 correspond to the frequency? In the FFTW Reference they say that the k-th output corresponds to the frequency k/n or k/T. If my n = 128 does that mean that the 1st element of my output corresponds to a frequency of 1/128? --> I'm just asking because that doesn't seem plausible to me. – Dominik Koller Mar 06 '12 at 13:20
  • OK, good - I've made this an answer now for anyone who might be browsing this question in future, and have added a note on bin frequencies - see below. – Paul R Mar 06 '12 at 13:23

1 Answers1

3

out[i][0] is the real part of complex bin i, out[i][1] is the imaginary part.

Change your test code to:

for(int i=0; i < 128; i++)
{
    std::cout << "FFT Im-Part: " << out[i][1] << "\n";
}

As for bin frequencies: for an N point FFT and a sample rate of Fs, the frequency corresponding to bin k is:

f = Fs * k / N

So if Fs = 44.1 kHz and you have a 128 point FFT then bin 0 = 0 Hz, bin 1 = 44100 * 1 / 128 = 344.5 Hz, bin 2 = 44100 * 2 / 128 = 689 Hz, etc.

See this answer for a fuller explanation.

Community
  • 1
  • 1
Paul R
  • 208,748
  • 37
  • 389
  • 560