Velocity Reviews > C++ > function of 2d array

# function of 2d array

Andi
Guest
Posts: n/a

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

XtremDev
Guest
Posts: n/a

 11-07-2006
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" <(E-Mail Removed)> 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 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[i]);
> double phase=getPhase(arg[i]);
> double normamplitude=amplitude/Dim;
> arg[i][0]=getRe(normamplitude*phase);
> arg[i][1]=getIm(normamplitude*phase);
> }
>
>
>
> }- Masquer le texte des messages précédents -- Afficher le texte des messages précédents -

Daniel T.
Guest
Posts: n/a

 11-07-2006
"XtremDev" <(E-Mail Removed)> wrote:

> may be you could first create a complex class :

'complex' already exists in the standard.

--
To send me email, put "sheltie" in the subject.

Ivan Vecerina
Guest
Posts: n/a

 11-07-2006
"Andi" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
: 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[i]);
: double phase=getPhase(arg[i]);
: double normamplitude=amplitude/Dim;
: arg[i][0]=getRe(normamplitude*phase);
: arg[i][1]=getIm(normamplitude*phase);
A simple change to the above lines allows your code to compile:
arg[i][0]=getRe(normamplitude,phase);
arg[i][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=beg inner's+c%2b%2b

--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com

stevenj@alum.mit.edu
Guest
Posts: n/a

 11-07-2006
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<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