Whats it doing?

Hi I've very little c++ experience and was wondering could someone explain to me what this bit of code is doing? It's mainly just what the x array is doign which is confusing me. The code is meant to be doing a fourier transform on an array of image data. Also is it adding all details to position 0 and nFFT2 in the array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
TAnalysis::FFT(float* x, float*& re, float*& im)
{
    // Real part
    re[0] = 0.;
    for(int n = 0; n < nFFT; n++) 
           re[0] += x[n];
    re[nFFT2] = 0.;
    for(int n = 0; n < nFFT; n++) {
         float a = (n % 2)? -x[n] : x[n];
         re[nFFT2] += a;
    }
        for(int k = 1; k < nFFT2; k++) {
         re[k] = 0.;
         for(int n = 0; n < nFFT; n++)
                 re[k] += x[n] * Cos[k][n];
    }
 
    // Image part
    im[0] = im[nFFT2] = 0.;
    for(int k = 1; k < nFFT2; k++) {
         im[k] = 0.;
         for(int n = 0; n < nFFT; n++)
                 im[k] += x[n] * Sin[k][n];
    }
}
Last edited on
can nobody help me with this?
please edit your post(codes) and take them inside code syntax, so that people can read them, it is not easy to read code looking like text
sorry, that should be easier to read now
for(int n = 0; n < nFFT; n++) re[0] += x[n]; is an addition operation , add each elements of xi to the first element of re. after that operation the first element of re is updated to sum of all xi

than renFFT2 is set to 0 and

1
2
3
4
for(int n = 0; n < nFFT; n++) {
         float a = (n % 2)? -x[n] : x[n];
         re[nFFT2] += a;
    }


if n is an even number xn element is added to renFFT2

if n is an odd number xn element is subtracted from renFFT2

1
2
3
4
5
for(int k = 1; k < nFFT2; k++) {
         re[k] = 0.;
         for(int n = 0; n < nFFT; n++)
                 re[k] += x[n] * Cos[k][n];
    }


than for each element between 1 and nFFT2
the elements of rek where-> 1 <= k < nFFT2 is updated to the total sum of
xn * Coskn where -> 0<= n < nFFT

and in the last loops the elements of imk where-> 1 <= k < nFFT2 is set to
the total sum of xn * Sinkn where ->0 <= n < nFFT


Dont ask me about the variable, and what they stand for , no idea

Last edited on
thanks very much for your help, its been of great benefit.

just to confirm, on this piece of code:

1
2
3
4
5
6
7
 
for(int k = 1; k < nFFT2; k++) 
{
         re[k] = 0.;
         for(int n = 0; n < nFFT; n++)
                 re[k] += x[n] * Cos[k][n];
}


this is doing x[n] + Cos[k][n] rather than adding the contents of whats currently in re[k] to x[n] * Cos[k][n]?

Thanks again
I've updated the error which you've just seen ->

1
2
3
4
5
for(int k = 1; k < nFFT2; k++) {
         re[k] = 0.;
         for(int n = 0; n < nFFT; n++)
                 re[k] += x[n] * Cos[k][n];
    }


than for each element between 1 and nFFT2
the elements of rek where-> 1 <= k < nFFT2 is updated to the total sum of
xn * Coskn where -> 0<= n < nFFT
Thanks, also to confirm is:

1
2
3
4
for(int n = 0; n < nFFT; n++) {
         float a = (n % 2)? -x[n] : x[n];
         re[nFFT2] += a;
    }


this definately subtracting if n is an odd number (i.e. if n%2!=0 then -x[n]) ? rather than subtracting if n is an even number
float a = (n % 2)? -x[n] : x[n]; it says that

if n is an integer that is "evenly divisible" by 2, i.e., divisible by 2 without remainder than

a = xn,

and if n is an integer that is not evenly divisible by 2 than a = -xn


(n % 2)? -x[n] : x[n]; means

1
2
3
4
5
6

if ( n % 2 == 1 )  than a = -xn
otherwise

a = xn
Last edited on
Topic archived. No new replies allowed.