Números Complexos e Transformada de Fourier Discreta

"The code that is the hardest to debug is the code that you know cannot possibly be wrong."

-Unknown



Classe CComplex (CComplex.h e CComplex.cpp)-

Header da classe CComplex seguido da implementação.


// Header para números complexos
// Apenas um exercício

#ifndef CCOMPLEX_H
#define CCOMPLEX_H

#include <iostream> 

using std::istream;
using std::ostream;
 
class CComplex {

	friend ostream &operator<<( ostream &, const CComplex & );
	friend istream &operator>>( istream &, CComplex & );
	friend CComplex &operator+( const CComplex &, CComplex &);

public:
 
 CComplex(double = 0.0 , double = 0.0);//contrutor
 
 ~CComplex();//destrutor

 
 //inicializa número complexo Cartesiano
 void setComplexCart(double *, double *);
 
 //inicializa número complexo Polar
 void setComplexPolar(double *, double *);
 
 //Acessores
 double getX( CComplex ) ;
 double getY( CComplex ) ; 

 //converte um número complexo para coordenadas polares
 void C2P( CComplex );
 
 //converte um número complexo para coordenadas cartesianas
 CComplex P2C( double, double );
 
 //retorna o módulo de um número complexo                                             
 double moduloComplex( CComplex );
 
 //retorna argumento de um numero complexo angulo entre coord x e y
 double argumentoComplex( CComplex );
                            
 
 //imprime na tela um n.o complexo
 void printComplex( CComplex ) const;
 

  //raio*(cos(teta)+j*sen(teta))
 double raio;
 double argumento;
 
 // ax +b
 double a;
 double b;


};
#endif
  

//implementação da classe Complex
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <iomanip>

using std::setw;

#include <cmath>

#include "CComplex.h"

//-------------------------------------------
CComplex::CComplex(double real_, double img_){
 
	setComplexCart(&real_, &img_);

}
//-------------------------------------------
CComplex::~CComplex(){
	
}
//-------------------------------------------


//Operadores sobrecarregados <<,>> e +
//declarados como funções globais
istream &operator<<( istream & input, CComplex &c)
{
	input >> setw(3) >> c.a;
	input.ignore();
	input >> setw(3) >> c.b;
	return input;
}

ostream &operator<<( ostream &output, const CComplex &c)
{
	output << c.a << " + j*" << c.b;
	return output;
}


CComplex &operator+(const CComplex &c, CComplex &d)
{
 
  CComplex f;

  double x1 = c.a + d.a;
  double y1 = c.b + d.b;
  
  f.setComplexCart(&x1,&y1);

  return f;

}
////////////////////////////////////
	
double CComplex::getX(CComplex c) 
{
  double aux = c.a;
  return aux;
} 
double CComplex::getY(CComplex c) 
{
  double aux = c.b;
  return aux;
}

//-------------------------------------------
void CComplex::setComplexCart(double * x, double  * y) {
 a = *x;
 b = *y;
}
//-------------------------------------------
void CComplex::setComplexPolar(double * radio, double * angulo){
 raio = *radio;
 argumento = *angulo;
}
//-------------------------------------------
double CComplex::moduloComplex(CComplex c){
 return sqrt(pow(c.a,2)+pow(c.b,2));
}
//-------------------------------------------
double CComplex::argumentoComplex(CComplex c){
 return atan((c.a/c.b));
}
//-------------------------------------------
void CComplex::C2P(CComplex c){ 
 argumento = argumentoComplex(c);
 raio = moduloComplex(c);
}
//------------------------------------------- 
CComplex CComplex::P2C(double radio, double angulo){
 
 double jaca1;
 double jaca2;
 double r = radio;
 double ang = angulo;
 
 jaca1 = r*cos(ang);
 jaca2 = r*sin(ang);
 CComplex d;
 d.setComplexCart(&jaca1,&jaca2);
 return d;
}  
//-------------------------------------------  
void CComplex::printComplex(CComplex c) const{
 cout << c.a << " + j*" << c.b << "\n";
}




Classe Transformada de Fourier(Transforms.h e Transforms.cpp)

Implementação da transformada de Fourier Discreta 1D

//Header para a classe Transforms
//Transformadas 

#ifndef TRANSFORMS_H
#define TRANSFORMS_H



#include "CComplex.h"	// Added by ClassView
class Transforms  
{
public:
	CComplex * DFT(double fn[],double , int );
	Transforms();
	virtual ~Transforms();

};

#endif //fim da declaração do Header da classe 


//////////////////////////////////////////////////////////////////////
//
// Transforms.cpp: .
//
// Implementação da classe transforms
//////////////////////////////////////////////////////////////////////

#include <<iostream>>

using std::cout;
using std::cin;
using std::endl;

#include "Transforms.h"
#include "CComplex.h"	// Added by ClassView
#include <cmath>


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Transforms::Transforms()
{

}

Transforms::~Transforms()
{

}

//////////////////////////////////////////////////////////////////////
// Funções membro da classe
//////////////////////////////////////////////////////////////////////

CComplex *Transforms::DFT(double fn[], double k, int n)
{
 int i;
 double aux1;
 double aux2;
 
 aux1 = 0;
 aux2 = 0;
 const double pi = 3.141;
 
 for(i = 0; i < n; i++){
  aux2 = fn[i]*sin(2*pi*k*i/n) + aux2;
  aux1 = fn[i]*cos(2*pi*k*i/n) + aux1;
 };
 
 aux1 = sqrt(n)*aux1;
 aux2 = -sqrt(n)*aux2;

 CComplex *d = new CComplex(&aux1,&aux2);

 return d;

 delete d;

 
}



Programa principal

Obviamente, trata-se de um programa de testes, mas ilustra a funcionalidade das classe CComplex e Transforms, incluindo a sobrecarga dos operadores <<, >> e +.

///////////////////////////////////////////////////////////////
// Programa para calcular a Transformada Discreta
// de Fourier de uma função discreta fn[xi]
///////////////////////////////////////////////////////////////

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include <cmath>

#include "Transforms.h"
#include "CComplex.h"	// Added by ClassView


///////////////////////////////////////////////////////////////
//Programa principal
///////////////////////////////////////////////////////////////
int main () {


	int j;
	const int pi = 3.141;

	const int FunctionDim = 10;
	double Function[FunctionDim];
	double xvalues[FunctionDim];
	CComplex cc[FunctionDim];
	double modules[FunctionDim];



	for(j=0; j < 10 ; j++){
		xvalues[j]= (double)j*pi/6;
		Function[j]= (double)sin((double)j+pi/6);
	};
	
	
	 Transforms *t = new Transforms();	



	 for(j=0; j < 10; j++){
		 cc[j]=*(t->DFT(Function,xvalues[j],10));
		 modules[j]= cc[j].moduloComplex(cc[j]);
		 cout << modules[j] << endl;
	 };


	 CComplex jaca;
	 CComplex jaca2;

	 cout << "1.o complex" << endl;
	 cin >> jaca;

	 cout << "2.o complex" << endl;
	 cin >> jaca2;
	 
	 CComplex jaca3 = jaca + jaca2;
	 
	 cout << jaca3;
	

	delete t;	
	return 0;
}//fim do programa




VOLTAR À PÁGINA INICIAL. 1