Page 1 of 1

DSP code module and power (or root) function

PostPosted: Fri Feb 16, 2018 1:15 am
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)

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

PostPosted: Fri Feb 16, 2018 1:22 am
by KG_is_back
Code: Select all
pow(base,exponent)

I'm not sure if it's missing from the manual. It might be...

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

PostPosted: Fri Feb 16, 2018 2:28 am
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.

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

PostPosted: Fri Feb 16, 2018 3:49 am
by martinvicanek
The pow funtion is quite cpu hungry though. Perhaps that´s also a reason for its scarce use.

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

PostPosted: Fri Feb 16, 2018 11:11 pm
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...

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

PostPosted: Sat Feb 17, 2018 12:52 am
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.

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

PostPosted: Sat Feb 17, 2018 6:18 am
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.

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

PostPosted: Sat Feb 17, 2018 9:40 pm
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?