DSP code module and power (or root) function

DSP related issues, mathematics, processing and techniques
Post Reply
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

DSP code module and power (or root) function

Post by tulamide »

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)
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

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

Post by KG_is_back »

Code: Select all

pow(base,exponent)

I'm not sure if it's missing from the manual. It might be...
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

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

Post by tulamide »

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)
User avatar
martinvicanek
Posts: 1334
Joined: Sat Jun 22, 2013 8:28 pm

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

Post by martinvicanek »

The pow funtion is quite cpu hungry though. Perhaps that´s also a reason for its scarce use.
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

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

Post by KG_is_back »

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...
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

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

Post by tulamide »

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)
User avatar
martinvicanek
Posts: 1334
Joined: Sat Jun 22, 2013 8:28 pm

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

Post by martinvicanek »

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.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

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

Post by tulamide »

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)
Post Reply