function of 2d array

A

Andi

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<Dim;i++) {
double amplitude=getAmplitude(arg);
double phase=getPhase(arg);
double normamplitude=amplitude/Dim;
arg[0]=getRe(normamplitude*phase);
arg[1]=getIm(normamplitude*phase);
}
}
 
X

XtremDev

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 );
 
I

Ivan Vecerina

: 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.
:
: // the following 4 work
: double getAmplitude (double arg[])
If the parameter must always be a 2-item array, it
would be pass that array by reference:
double getAmplitude( double const (&arg)[2] )

: {
: double amplitude=sqrt(pow(arg[0],2.)+pow(arg[1],2.));
: return amplitude;
: }
:
: double getPhase (double arg[]) {
Same: double getPhase ( double const (&arg)[2] )

: 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<Dim;i++) {
: double amplitude=getAmplitude(arg);
: double phase=getPhase(arg);
: double normamplitude=amplitude/Dim;
: arg[0]=getRe(normamplitude*phase);
: arg[1]=getIm(normamplitude*phase);
A simple change to the above lines allows your code to compile:
arg[0]=getRe(normamplitude,phase);
arg[1]=getIm(normamplitude,phase);

: }
: }

This said: working with "naked" array is tough, and calls for
trouble. Why not use a struct, or std::complex ?
[ or even better, an existing FFT implementation ... ]


: But I don't know how to handle these 1D/2D arrays. Any advices (maybe
a
: book etc.?)

I have no specific book advice, but you could study pointers,
references and arrays in your favorite C+ book.
http://accu.org/index.php/book_reviews?url=search.xqy?field=subject&term=beginner's+c++
 
S

stevenj

Andi said:
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<double>* (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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top