446,300 Members | 1,306 Online
Need help? Post your question and get tips & solutions from a community of 446,300 IT Pros & Developers. It's quick & easy.

# function of 2d array

 P: n/a Hi, I want to define the following functions before main(); I work with 2D arrays (fftw_complex) wich are arrays with array[][0] is real and array[][1] the imaginary part of a complex number. Now I want to do some operations like norming, swapping, coord transforms as functions. But I don't know how to handle these 1D/2D arrays. Any advices (maybe a book etc.?) // the following 4 work double getAmplitude (double arg[]) { double amplitude=sqrt(pow(arg[0],2.)+pow(arg[1],2.)); return amplitude; } double getPhase (double arg[]) { double phase=atan(arg[1]/arg[0]); return phase; } double getRe (double amplitude, double phase) { double result=amplitude*cos(phase); return result; } double getIm (double amplitude, double phase) { double result=amplitude*sin(phase); return result; } // functions -- Do not work void coord_transform (fftw_complex arg[][2]); void norm (fftw_complex arg[][2]) { int Dim=((int)(sizeof(arg)/(sizeof(arg[0])))); for (int i=0;i
4 Replies

 P: n/a may be you could first create a complex class : class complex { public: complex(); complex(double p_dRe, double p_dIm); complex(const complex & p_complex); ~complex(); complex & operator=(const complex & p_complex); double GetRe() const; double GetIm() const; double GetMod() const; double GetArg() const; void SetRe(dconst double p_dValue); void SetIm(const double p_dValue); etc...... opertor+, operator-, operator +=, operator -= protected: double m_dRe; double m_dIm; }; and after you could manage a 1D array of complex; Complex * l_pArrComplex = new Complex[256]; Finally, you could use the complex array pointer in a Fft class or function like this : void FftTransform( double * p_pdInSignal, Complex * p_pcplxOutFft ); On 7 nov, 13:30, "Andi"

 P: n/a "XtremDev"

 P: n/a "Andi"

 P: n/a Andi wrote: Hi, I want to define the following functions before main(); I work with 2D arrays (fftw_complex) wich are arrays with array[][0] is real and array[][1] the imaginary part of a complex number. Now I want to do some operations like norming, swapping, coord transforms as functions. But I don't know how to handle these 1D/2D arrays. Any advices (maybe a book etc.?) The simplest solution is to just take your fftw_complex *arg array and typecast it to std::complex* (or vice versa). See http://www.fftw.org/doc/Complex-numbers.html for why this works (in short, all extant std::complex implementations store as real part followed by imaginary part, the same as C99, and this is slated to become part of the standard if it hasn't already). Note also that your code is wrong; you should use atan2 to get the phase of a complex number, not atan (which loses sign information). (It's also crazy to use the pow function just to square a number, BTW.) Note also that you don't need to convert a complex number to polar form just to divide by a real number -- it is much simpler and more efficient to just divide the real and imaginary parts separately. You may have a deeper problem if you want to use FFTs but don't know how to do complex arithmetic given the real and imaginary parts of a number. Regards, Steven G. Johnson Nov 7 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.