 # Number complex

hello I'm new to working with arduino I wanted to add two complex numbers: (a + bi) + (a + bi) how can I do this.

Thanks Gil

I'm new to working with arduino I wanted to add two complex numbers:

Just like you would on any other computer.

Here’s a simple piece of code which uses complex numbers.

``````struct complex {
double r;
double i;
} a,b,c;

// Set a complex number. Real(p)=cr, Imag(p)=ci
void set_complex(struct complex *p,double cr,double ci)
{
p->r = cr;
p->i = ci;
}

// Add two complex numbers and place the result in a third
// c = a+b
void add_complex(struct complex *c,struct complex *a,struct complex *b)
{
c->r = a->r + b->r;
c->i = a->i + b->i;
}
void setup(void)
{
Serial.begin(9600);

// Set a to (1,2)
set_complex(&a,1,2);
// set b to (3.2,-4.1)
set_complex(&b,3.2,-4.1);
// add a and b and put the result in c
// Now print c
Serial.print("c = ");
Serial.print(c.r,1);
if(c.i >= 0)Serial.print("+");
Serial.print(c.i,1);
Serial.println("i");
}

void loop(void)
{
}
``````

But assuming you can add the two complex numbers, what are you going to do with the result?

Pete

I want to do the DFT definition through the arduino so I use complex numbers.

thank you Gil

I want to do the DFT definition through the arduino

The what?

Good luck with that. Have a look at this thread, which has code to do a fixed-point DFT. http://arduino.cc/forum/index.php/topic,72729.0.html

Pete

I looked up DFT - Detroit Fine Theater. I can't imagine how you can do that on the Arduino, or what complex numbers have to do with it.

A nice complex number class, here is a starter - http://www.dreamincode.net/code/snippet75.htm -

hello

I made this function to calculate the DFT (discrete Fourier transform) but I do not run the arduino has errors the compiler does not interpret the (I> complex number)

X=zeros(1,N);
for k=0:N-1
a=zeros(1,N);
for n=0:N-1
a(n+1)=x(n+1)exp(-(1i2pin*k)/N);
end
X(k+1)=sum(a);

thank you Gil

Perhaps you should try writing the function in C, instead of Basic.

Hello,

In language C i dont know what the libraries that i have to use. Arduino doesn´t uses C language its a bit diferent, then i have a C function maybe i can develope one for Arduino.

Can you help me?

Thanks Gil

Arduino doesn´t uses C language its a bit diferent

Nope, its C and C++. There is no 'Arduino specific' language, it is just a C style API for use with Arduino boards.

You should consider merging the threads you have on this subject into one.

yeah. I making the code in C or C + + how can I convert to the arduino?

thank Gil

"You should consider merging the threads you have on this subject into one"

I was wrong and I put 2 Posts sorry I'll join the two

thank Gil