Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

DSP code module and power (or root) function

DSP related issues, mathematics, processing and techniques

DSP code module and power (or root) function

Postby tulamide » Fri Feb 16, 2018 1:15 am

It seems I'm missing something. I know that simple powers can be realized with simple multiplication (x * x for x^2, etc.). But without a power or root function, how would I go to realize this?

Code: Select all
y = x^(1/x)
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: DSP code module and power (or root) function

Postby KG_is_back » Fri Feb 16, 2018 1:22 am

Code: Select all
pow(base,exponent)

I'm not sure if it's missing from the manual. It might be...
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: DSP code module and power (or root) function

Postby tulamide » Fri Feb 16, 2018 2:28 am

Thanks a lot!

Yes, it is missing in the manual! Also, when looking at other people's codes, I never see this expression used. Instead, it's always worked around, similar to what I pointed to in the first post.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: DSP code module and power (or root) function

Postby martinvicanek » Fri Feb 16, 2018 3:49 am

The pow funtion is quite cpu hungry though. Perhaps that´s also a reason for its scarce use.
User avatar
martinvicanek
 
Posts: 1328
Joined: Sat Jun 22, 2013 8:28 pm

Re: DSP code module and power (or root) function

Postby KG_is_back » Fri Feb 16, 2018 11:11 pm

tulamide wrote:Thanks a lot!

Yes, it is missing in the manual! Also, when looking at other people's codes, I never see this expression used. Instead, it's always worked around, similar to what I pointed to in the first post.

martinvicanek wrote:The pow funtion is quite cpu hungry though. Perhaps that´s also a reason for its scarce use.


definitely... one would expect such a basic operation to be natively included in a CPU. Instead is is split into two operations 1. raising float to an integer and 2. y*2^x where x is in <0-1) range. So to calculate power you first need to multiply the exponent by is log2 then calculate its rounddown and modulo 1, do exponentiation by each of them and multiply the results. It can be done faster using SSE (2 orders of magnitude faster as seen in Martin's stream math functions) but the algorithm is pretty much the same.

In general you should avoid it like a plague if you care about speed, because it's pretty much guaranteed to be the bottleneck in your code...
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: DSP code module and power (or root) function

Postby tulamide » Sat Feb 17, 2018 12:52 am

KG_is_back wrote:In general you should avoid it like a plague if you care about speed, because it's pretty much guaranteed to be the bottleneck in your code...

The following introduction is not for KG or Martin, but for other people, who might read here and are not so much into math. KG, Martin, please head over to the last paragraph.

The function I wrote in the first post is called "inverse power function". A power function is pretty easy to understand. Just imagine the exponent tells you, how many times you have to multiply the base.
Code: Select all
x^3 = x * x * x


Inverse power tells you the opposite. Let's put x = 2, to get comprehensible results.
Code: Select all
x^(1/x) == 2^(1/2) == 2^0.5

Obviously we can't multiply x with itself just a half time. Instead it tells us that the result, we are looking for is the number, that multiplied with itself results in 2. And that is easy in this case. It's the square root of 2.
2√2 = 1,4142135623730950488016887242097
Code: Select all
2^(1/2) == 2√2
x^(1/x) == x√x


Both, power and inverse power are building blocks in quite some DSP functions. So, I really wonder, do any of you know of a way to realize inverse power with just simple arithmetics? That's way over my head.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: DSP code module and power (or root) function

Postby martinvicanek » Sat Feb 17, 2018 6:18 am

tulamide wrote: So, I really wonder, do any of you know of a way to realize inverse power with just simple arithmetics?

There is no closed form in terms of a finite number of elementary operations (+,-,*,/) except for integer powers. In practice you can use iteration until you reach the desired accuracy or other sorts of approximation. Since this can be done to machine precision, it is a solved problem in numerical analysis.
User avatar
martinvicanek
 
Posts: 1328
Joined: Sat Jun 22, 2013 8:28 pm

Re: DSP code module and power (or root) function

Postby tulamide » Sat Feb 17, 2018 9:40 pm

martinvicanek wrote:
tulamide wrote: So, I really wonder, do any of you know of a way to realize inverse power with just simple arithmetics?

There is no closed form in terms of a finite number of elementary operations (+,-,*,/) except for integer powers. In practice you can use iteration until you reach the desired accuracy or other sorts of approximation. Since this can be done to machine precision, it is a solved problem in numerical analysis.

I confess, I hoped for a dsp code example. Did nobody ever need to use inverse power?
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany


Return to DSP

Who is online

Users browsing this forum: No registered users and 45 guests