Hi All,
I have a matlab program in which i have to calculate values of an expression containing complex numbers.
I have to convert the same program to C language so as to calculate the enegry estimate using another software.
Now the bone of contention here for me is how can i change the complex number expression in MATLAB to that of the equivalent in C .
Please help, in case one needs i can put the expression here as well.
Thanks,
Chandra.
解决方案Poorly speaking, complex numbers are just an ordered pair of real ones, following certain rules. Hence you may code them this way:
typedef struct tagComplex
{
double re; //real part
double im; //imaginary part
} Complex;
Then you may define the functions acting on them (and enabling you to solve expressions).
For instance,
setu.dixit19 wrote:Qen[n1]=exp(j*(n1-1)*(vv0[k]-vv0[l]))
It looks you need the complex exp function
void complex_exp(Complex * pOperand, Complex * pResult)
{
double er = exp(pOperand->re);
pResult->re = er * cos( pOperand->im);
pResult->im = er * sin( pOperand->im);
}
The most basic way would be to create a struct defining each component of the number. Assuming you are only working in 1 dimension then your complex number will be of the form ai+b, and so your struct would look like
typedef struct {
float a; //The "imaginary" part. Might be a int instead
float b; //The real part. Might be a int instead
} ComplexNumber;
Could you please provide the equation and indicate weather it needs to work on other similar equations or only this exact one and the operations that you are wanting to perform.
If it is a relatively simple equation like sqrt(-1) then there can be a simple answer without having to implement a complex numbers system.
ComplexNumber ComplexSqrt(float nNumber) {
ComplexNumber cnRes = { 0, 0 };
if (nNumber < 0) { //nNumber is negative. Result will be complex
cnRes.a = sqrt(-nNumber);
} else { //nNumber is positive. Result will be real
cnRes.b = sqrt(nNumber);
}
return cnRes;
}
Thanks Andrew for your response.
However the equation that i am reffering to as given below:
"Qen[n1]=exp(j*(n1-1)*(vv0[k]-vv0[l]))"
This expression is the one that i need to calculate and the concern is how to remove the ''j'' part from the equation.
Looking forward to your further replies ..
cheers
Setu