Go Down

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

robtillaart

Mar 11, 2012, 12:12 pmLast Edit: Oct 18, 2015, 12:15 pm by robtillaart
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

Playground article - http://arduino.cc/playground/Main/ComplexMath - (not up to date)

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 pmLast 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

#2
Mar 11, 2012, 03:05 pm
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

#3
Mar 11, 2012, 07:03 pm

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 pmLast 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

#5
May 19, 2012, 10:40 am
update to version 0.1.03
- 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

#6
Sep 23, 2013, 01:45 pm
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

#7
Oct 05, 2013, 07:20 pm
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)

robtillaart

#8
Mar 06, 2015, 10:25 amLast Edit: Mar 06, 2015, 10:29 am by robtillaart
update to version 0.1.07 (attachment)
+ refactored interface to decrease footprint
+ added referenceOutput.txt to test sketch

As always 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

#9
Mar 07, 2015, 09:10 pm
update to version 0.1.08
+ refactored to decrease footprint
+ replaced divisions with multiplies (faster)

As always 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

#10
Oct 18, 2015, 12:14 pm
update to version 0.1.09
+ removed unneeded ifdef constructs

As always 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)

Go Up

Please enter a valid email to subscribe