Pages: [1]   Go Down
Author Topic: Number complex  (Read 877 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: March 07, 2012, 12:19:26 pm by taurian » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm new to working with arduino I wanted to add two complex numbers:
Just like you would on any other computer.
Logged

Offline Offline
Edison Member
*
Karma: 35
Posts: 1429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a simple piece of code which uses complex numbers.
Code:
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
  add_complex(&c,&a,&b);
  // 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
Logged

Where are the Nick Gammons of yesteryear?

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

thank you Gil
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to do the DFT definition through the arduino
The what?
Logged

Offline Offline
Edison Member
*
Karma: 35
Posts: 1429
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Where are the Nick Gammons of yesteryear?

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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(-(1i*2*pi*n*k)/N);
    end
     X(k+1)=sum(a);


thank you Gil
« Last Edit: March 08, 2012, 07:17:27 am by taurian » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46257
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 53
Posts: 1801
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged


Gosport, UK
Offline Offline
Faraday Member
**
Karma: 19
Posts: 3114
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

thank Gil
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

"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
Logged

Pages: [1]   Go Up
Jump to: