A crazy problem with GNURadio fft

Hi all,

I was wonder why my codes in Matlab aren’t compatible with GNURadio
ones!
After all, I found the fft output in GNURadio is different to Matlab!!

Here is my simple code for fft testing with the real inputs of 0 to 63:

#include

#include <gnuradio/fft/fft.h>

using namespace std;

using namespace gr;

int main()

{

int fft_size = 64;

gr::fft::fft_complex *ifft = new gr::fft::fft_complex (fft_size, 

true, 1);

// making complex data

gr_complex in[fft_size];

for (int i=0; i<fft_size; i++)

{

    in[i] = i;

}


memcpy(ifft->get_inbuf(), in, fft_size);

ifft->execute();


cout << "output" << endl;

for (int i=0; i<fft_size; i++)

{

    cout << ifft->get_outbuf()[i] << endl;

}



cout << "THE END!" << endl;

return 0;

}

the output in GNURadio:

(28,0)

(24.3326,-13.0209)

(14.591,-22.0217)

(2.07356,-24.4501)

(-9.13707,-20.1094)

(-15.692,-11.1259)

(-16.1531,-1.03318)

(-11.3785,6.63407)

(-4,9.65685)

(2.75122,7.8416)

(6.35844,2.87496)

(5.93264,-2.55936)

(2.38009,-5.98642)

(-2.18793,-6.14396)

(-5.4952,-3.37881)

(-6.11148,0.684235)

(-4,4)

(-0.398263,5.06997)

(2.86434,3.58162)

(4.26461,0.45671)

(3.27677,-2.67271)

(0.553035,-4.28344)

(-2.45947,-3.67857)

(-4.25578,-1.27436)

(-4,1.65685)

(-1.899,3.64105)

(0.940382,3.727)

(3.08336,1.93207)

(3.48022,-0.795649)

(1.9727,-3.06732)

(-0.646391,-3.74757)

(-3.04078,-2.51227)

(-4,0)

(-3.04078,2.51227)

(-0.646391,3.74757)

(1.9727,3.06732)

(3.48022,0.795649)

(3.08336,-1.93207)

(0.940382,-3.727)

(-1.899,-3.64105)

(-4,-1.65685)

(-4.25578,1.27436)

(-2.45947,3.67857)

(0.553035,4.28344)

(3.27677,2.67271)

(4.26461,-0.45671)

(2.86434,-3.58162)

(-0.398263,-5.06997)

(-4,-4)

(-6.11148,-0.684235)

(-5.4952,3.37881)

(-2.18793,6.14396)

(2.38009,5.98642)

(5.93264,2.55936)

(6.35844,-2.87496)

(2.75122,-7.8416)

(-4,-9.65685)

(-11.3785,-6.63407)

(-16.1531,1.03318)

(-15.692,11.1259)

(-9.13707,20.1094)

(2.07356,24.4501)

(14.591,22.0217)

(24.3326,13.0209)

the output of the Matlab:
1.0e+03 *

2.0160
-0.0320 + 0.6514i
-0.0320 + 0.3249i
-0.0320 + 0.2157i
-0.0320 + 0.1609i
-0.0320 + 0.1278i
-0.0320 + 0.1055i
-0.0320 + 0.0894i
-0.0320 + 0.0773i
-0.0320 + 0.0677i
-0.0320 + 0.0599i
-0.0320 + 0.0534i
-0.0320 + 0.0479i
-0.0320 + 0.0431i
-0.0320 + 0.0390i
-0.0320 + 0.0353i
-0.0320 + 0.0320i
-0.0320 + 0.0290i
-0.0320 + 0.0263i
-0.0320 + 0.0237i
-0.0320 + 0.0214i
-0.0320 + 0.0192i
-0.0320 + 0.0171i
-0.0320 + 0.0151i
-0.0320 + 0.0133i
-0.0320 + 0.0114i
-0.0320 + 0.0097i
-0.0320 + 0.0080i
-0.0320 + 0.0064i
-0.0320 + 0.0047i
-0.0320 + 0.0032i
-0.0320 + 0.0016i
-0.0320
-0.0320 - 0.0016i
-0.0320 - 0.0032i
-0.0320 - 0.0047i
-0.0320 - 0.0064i
-0.0320 - 0.0080i
-0.0320 - 0.0097i
-0.0320 - 0.0114i
-0.0320 - 0.0133i
-0.0320 - 0.0151i
-0.0320 - 0.0171i
-0.0320 - 0.0192i
-0.0320 - 0.0214i
-0.0320 - 0.0237i
-0.0320 - 0.0263i
-0.0320 - 0.0290i
-0.0320 - 0.0320i
-0.0320 - 0.0353i
-0.0320 - 0.0390i
-0.0320 - 0.0431i
-0.0320 - 0.0479i
-0.0320 - 0.0534i
-0.0320 - 0.0599i
-0.0320 - 0.0677i
-0.0320 - 0.0773i
-0.0320 - 0.0894i
-0.0320 - 0.1055i
-0.0320 - 0.1278i
-0.0320 - 0.1609i
-0.0320 - 0.2157i
-0.0320 - 0.3249i
-0.0320 - 0.6514i

Please help me!! I’m totally confused.

Best

memcpy(ifft->get_inbuf(), in, fft_size);

fft_size * sizeof(gr_complex)

Thanks Sylvain,
not finding that drove me crazy (more crazy than usual); the zeroth
element must be sum(range(fftlen))=(63+0)(64/2) = 6332 = 2048 - 32 =
2016, like in the matlab result, yet I did not find where the data
stopped flowing… now,
sum(range(fftlen/sizeof(gr_complex)))=sum(range(8))=(7+0)*8/2=28
explains that :slight_smile:

Also, Mostafa, you call your transform ifft, yet you set forward=true,
which might be a bit confusing in the future.
For the IFFT, Matlab does something different than the pure FFTW that
GNU Radio uses: it divides by fft_size, compare [1] to [2].

Greetings,
Marcus

[1]http://www.mathworks.de/de/help/matlab/ref/fft.html
[2]The 1d Discrete Fourier Transform (DFT) (FFTW 3.3.10)

Excuse me all!
That was my fault because previously I checked the FFTW with Matlab I
did
know its difference as Murcus mentioned. However, I suddenly suspect the
FFTW again!

Thank you so much

On Tue, Aug 26, 2014 at 6:31 PM, Marcus M. [email protected]
wrote:

For the IFFT, Matlab does something different than the pure FFTW that

memcpy(ifft->get_inbuf(), in, fft_size);

[email protected]
Discuss-gnuradio Info Page


Department of Electrical Engineering
Aboureyhan Building
MMWCL LAB
Amirkabir University Of Technology
Tehran
IRAN
Tel: +98 (919) 158-7730
LAB: http://ele.aut.ac.ir/~mmwcl/?page_id=411
Homepage: http://ele.aut.ac.ir/~alizadeh/