How to extract real part from complex number?

I'll tell you c++ is phenomenally difficult for a beginner. I need to extract the real part from a complex number. My compiler is g++ in Ubuntu 12.04.

Nothing I have tried worked so far and I googled and googled. It is just ridiculous. Let say the number is complC, then complC.real() does not work, std::real (complC) does not work either. Needless to day I tried numerous other variants. This is a simple operation. Why is it made so difficult?

Thanks, - Alex

Last edited on
OK, I have this code:

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
27
28
29
30
31
 
#include "fft.h"
#include <math.h>
#include <fstream>      // std::ofstream
#include <complex>

  int main ()
{
  std::ofstream outFileInput,outFileOutput;
  complex Input[1024]=0, Output[1024]=0;
  complex valueC;
  int N=1024;
  double xx;
  double welshWindow(int n, int N);
  outFileInput.open ("cppFFTinp.dat");
  outFileOutput.open("cppFFTout.dat");
  for (int jj = 0; jj<N-1; jj++)
    {
      xx = welshWindow(jj,N);
      valueC = complex(xx,0);
      Input[jj] = valueC;
      outFileInput << jj << "  " << xx << std::endl;
    }
  CFFT::Forward (Input,Output,N);
  for (int jj = 0; jj<N-1; jj++)
    {
      valueC = Output[jj];
      xx = std::real(valueC);   // this statement does not compile
      outFileOutput << jj << "  " << xx << std::endl;
    }
}            // main  


If I remove the statement marked // This statement does not compile, the file compiles and I can run it. As I said, I need to extract the real part of the complex number in question. In fortran it is elementary. Here it is a monumental problem. It is simply gross.

If I leave the statement in question and to g++ fft.cpp I get these errors:

1
2
3
4
5
 fft.cpp: In function ‘int main()’:
fft.cpp:33:28: error: no matching function for call to ‘real(complex&)’
fft.cpp:33:28: note: candidates are:
/usr/include/c++/4.6/complex:545:5: note: template<class _Tp> _Tp& std::real(std::complex<_Tp>&)
/usr/include/c++/4.6/complex:550:5: note: template<class _Tp> const _Tp& std::real(const std::complex<_Tp>&) 


33 is the number line of that statement. What can I do about the "candidates?" I think my code already complies with this template.
Last edited on
closed account (D80DSL3A)
I found this by following a link you posted in your previous thread on the proprietary complex class you're using:
1
2
3
 //   Basic operations - taking parts
   double re() const { return m_re; }
   double im() const { return m_im; }

Here's where the class functions are described
http://www.librow.com/articles/article-10/appendix-b-1
Last edited on
Thanks, but that leads to terrible problems. In the end if I include complex.h I will get five terminals worth of errors. I've tried it before. When I included these definitions you posted in the file I got a bunch of errors also. I am not sure it is worth even trying to post them unless requested.

There should be a SIMPLE way to get the real part of a complex number. WHAT IS GOING ON? Anybody knows how to do it?
closed account (D80DSL3A)
I'm sorry you're having such problems. I see your other thread on the compilation problem you're having.
I copy/pasted the code for the complex.h and complex.cpp files to my Code::Blocks compiler
and tried this:
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include "complex.h"

int main()
{
    complex c1( 3.0, 5.0 );
    std::cout << c1.re() << std::endl;

    return 0;
}

It built and ran 1st try.

Are you naming the files something other than .h and .cpp ?
Last edited on
The following compiles on my box. Of course, since you didn't provide the definition for double welshWindow(), I had to comment out line 18 and only get zeros as output.

g++ -std=c++11 main.cpp fft.cpp
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
27
28
29
30
#include "fft.h"
#include <math.h>
#include <fstream>      // std::ofstream
#include <complex>

  int main ()
{
  std::ofstream outFileInput,outFileOutput;
  complex Input[1024]={0}, Output[1024]={0};
  complex valueC;
  int N=1024;
  double xx;
  double welshWindow(int n, int N);
  outFileInput.open ("cppFFTinp.dat");
  outFileOutput.open("cppFFTout.dat");
  for (int jj = 0; jj<N-1; jj++)
    {
//      xx = welshWindow(jj,N);
      valueC = (xx,0);
      Input[jj] = valueC;
      outFileInput << jj << "  " << xx << std::endl;
    }
  CFFT::Forward (Input,Output,N);
  for (int jj = 0; jj<N-1; jj++)
    {
      valueC = Output[jj];
      xx = valueC.re(); //std::real(valueC);   // this statement does not compile
      outFileOutput << jj << "  " << xx << std::endl;
    }
}            // main 
norm b, thank you. The welshWindow is here:

1
2
3
4
5
6
7
 double welshWindow (int n, int N)
    {
      double res;
      res = (N-1)/2.0;
      res = 1.0 - pow ((n - res)/res,2);
      return res;
    } 


I got it from Wikipedia article "Window function." You can find it there among others.

Thank you much. WOW!!! It compiled. It works. Thank you very much!
Last edited on
You're welcome. I was just reiterating what fun2code pointed out.

Take heed of the warning on line 19:
valueC = (xx,0);

Everything, everything is working now. Thank you very much for your help. I took care of the warning also.

- Alex
Topic archived. No new replies allowed.