Go Down

Topic: Complex numbers library (Read 3395 times) previous topic - next topic

robtillaart

Mar 11, 2012, 12:12 pm Last Edit: Mar 11, 2012, 12:21 pm by robtillaart Reason: 1
Recently there was a request for complex numbers and yesterday I came across a set of complex math formula's so I decided to Arduinize them into a library.
It took some hours to google a complete set of formulas from different sites which resulted into a rather BIG lib. It definitely needs a review as I assume some parts can be optimized. Fortunately everyone can strip out only the functions they need to minimize the lib (compiler is also helpful).

.cpp and .h and the testcode is attached as zip-file

A playground article is in progress - http://arduino.cc/playground/Main/ComplexMath -

As allways comments and remarks are welcome
Rob Tillaart

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

robtillaart

#1
Mar 11, 2012, 01:06 pm Last Edit: Mar 11, 2012, 01:11 pm by robtillaart Reason: 1
some test update:
- output under IDE 0.22 and IDE 1.0 of the test sketch is identical
- 0.22 size: 18594  
- 1.0 size: 19050
Rob Tillaart

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

stimmer

If multiplication is much slower than addition/subtraction (which it might be on an Arduino), there is a formula for complex multiplication which only uses three multiplications at the expense of needing 5 additions/subtractions:

Code: [Select]

double s=a.re * b.re;
double t=a.im * b.im;
double u=(a.re + a.im)*(b.re + b.im);

c.re = s-t;
c.im = u-s-t;


It might be worth investigating if this works any faster in practice.
Due VGA library - http://arduino.cc/forum/index.php/topic,150517.0.html

robtillaart


Thanks stimmer,

I did no performance tests/tuning yet but this is definitely one to remember.
Rob Tillaart

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

robtillaart

#4
Mar 11, 2012, 08:20 pm Last Edit: Mar 11, 2012, 10:44 pm by robtillaart Reason: 1
Just tested proposed code:
Code: [Select]

 unsigned long start = micros();
 for (int i = 0; i< 10000; i++)
 {
   c5 = c3 * c4;
 }
 unsigned long stop = micros();
 Serial.println(stop - start);
 Serial.println("\n.. Complex done");  


traditional code: 667180 for 10K multiplications
proposed code: 784068 for 10K multiplications
diff: 116888 = 17.5% slower :(

I still have to try some variations

- update -
improved the complex division by 9%  :) - new code -

Code: [Select]
Complex Complex::operator / (Complex c)
{
double f = 1/(c.re*c.re + c.im*c.im);
double r = re * c.re + im * c.im;
double i = re * c.im - im * c.re;
return Complex(r * f, -i * f);
}


- update -
* improved upon other divisions too,
* other optimizations costs a lot of memory
  e.g. c_tan() = c_sin()/c_cos() which can be rewritten in 11 lines (steps) so that no new object is created => -44% but price is 358 bytes!!

Rob Tillaart

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

robtillaart

update to version 0.1.03
- added minor fixes
- zip file also includes test sketch
Rob Tillaart

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

robtillaart

update to version 0.1.04
- added #ifdef's in complex.h file to selectively use functions.
Rob Tillaart

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

robtillaart

update to version 0.1.05
+ derive complex from PRINT ==> so it can be used in Serial.print() and other print derived classes.
+ layout

// note the test program on the playground may not be compatible anymore (I need to check that someday)
Rob Tillaart

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

Go Up