0

I'm having a really hard time translating this Matlab code to Python.

I'll show you my effort so far.

This is the matlab code

Sigma=BW1/(2*(2*(-log(10^(att_bw/10)))^(1/Order))^(1/2))

Now I tried to used Python power operator as I studied earlier this morning **

My code is

BW1 = np.array([100])
att_bw = np.array([-3])
Order = np.array([1])
Sigma = BW1/(2*(2*(-np.log(10**(att_bw[0]/10)))**(1/Order))**(1/2))

However it says that it cannot handle negative powers unfortunately

The result for sigma should be 42.539

EDIT: it seems my code runs perfectly fine in Python 3. However I'm stuck with Python 2.7. So is there any easy way to port it?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
claw91
  • 161
  • 4
  • 11
  • @TedLyngmo BW instead of BW1 it's a typo. I have negative power. att_bw is -3 – claw91 Jun 19 '20 at 12:18
  • might be worth exploiting some math identities as well; e.g. `np.log(10**x)` is `np.log(10)*x` but won't over- or under-flow – Sam Mason Jun 19 '20 at 12:25
  • @TedLyngmo can you please add the answer here as a comment? I'd greatly appreciate it – claw91 Jun 19 '20 at 13:51
  • I don't understand how my code is working for you. I get ValueError: Integers to negative integer powers are not allowed. Because at one point I'm raising something to att_btw[0] which is -3 and a negative number, which in turn cause the program to fail – claw91 Jun 19 '20 at 14:12
  • I'm using Python 2.7.18. Could that be the cause? – claw91 Jun 19 '20 at 14:35
  • Is there any other way to do it since I cannot upgrade to Python 3? – claw91 Jun 19 '20 at 14:43
  • @TedLyngmo so the mistery is solved! It was a Python version issue then. Didn't know different Python version tags were required, my bad! – claw91 Jun 22 '20 at 09:21
  • @claw91 Easy thing to miss :-) – Ted Lyngmo Jun 22 '20 at 09:37

1 Answers1

1

In python2 you need to make sure you use floating point numbers. To make them so, add . after each integer you now have in your formula.

Like this:

import numpy as np

BW1 = np.array([100])
att_bw = np.array([-3])
Order = np.array([1])
Sigma = BW1/(2.*(2.*(-np.log(10.**(att_bw[0]/10.)))**(1./Order))**(1./2.))
print Sigma

Output

[42.53892736]
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108