gpt4 book ai didi

python - python中的Matlab numerictype/reinterpretcast等价物?

转载 作者:行者123 更新时间:2023-12-03 17:20:04 29 4
gpt4 key购买 nike

在 Matlab 中有一个命令来定义一个新的数字类型,例如:

numerictype(0,16,8) 
参见文档: https://www.mathworks.com/help/fixedpoint/ref/embedded.numerictype.html
numpy 或其他库中是否有等价物?我可以使用类似的命令创建自己的 dtype 吗?

编辑:
由于我被要求提供更多信息,这里是关于定点数字类型在 matlab 中如何工作的引用: https://www.mathworks.com/help/dsp/ug/concepts-and-terminology.html基本上你设置有符号/无符号性质,然后一个单词应该与分数长度一起有多长。因此,例如在我给您的示例中,将有一个带符号的数字,字长为 16,小数长度为 10。
从我读到的关于结构化数组的内容来看,类似的表示可能是这样的:
dtype=[('signed', np.bool_), ('word', np.int16), ('frac', np.int16)]) 
我的最终目标是实现三个独立的 reinterpertcast 语句,即:
reinterpretcast(EVMacq,numerictype(0,16,8))
reinterpretcast(Payload16c,numerictype(1,16,16))
reinterpretcast(Payload32,numerictype(1,32,32))
如果有一种方法可以更简单地做到这些,我非常乐意以不同的方式做到这一点。
这是我在评论中添加的信息的转录:
mathworks.com/help/fixedpoint/ref/reinterpretcast.html 这里是来自 matlab 的 reinterpretcast 的文档。本质上,您传入一个整数或一个定点数,该函数将移动小数点。这使得即使二进制数据没有改变变量的数值也是不同的。
有时,您可以通过正常除法对某些范围的数字实现类似的效果,但这并非万无一失,并且是一种不受欢迎的解决方案。
我也许可以自己写一些可以做到这一点的东西,但如果有人比我更聪明已经做到了,我会更喜欢它。考虑到大多数 matlab 功能都包含在 numpy 中,我认为这也是如此。结构化数组可能是一个不错的选择,但我不确定对它们进行转换的确切方式。

编辑:
我现在意识到,如果有人能告诉我如何做与这个 Actor 完全相同的事情,我真的只想磨练一个命令,我会非常高兴,因为我仍然无法弄清楚。速度不是问题,它只需要运行即可。
这是命令: reinterpretcast(Payload16c,numerictype(1,16,16))其中 Payload16c 是由 np.complex(real,imag) 定义的复数数组.先感谢您。
我尝试了这样的事情,但没有奏效,但可能在正确的轨道上。我似乎偏离了 MatLab 中发生的一些比例因子,但每次都不相同的比例因子:
    i = 0
result = []

#first generate a binary number that is a one in the highest spot and zero elsewhere
comp = 2**wordlength
#next iterate through entire array
while i < array.size:

#check to see if the value of the item is near the largest value it can be
#if so its likely that it is just negative and thats why that bit is high
if(array[i:i+1] < ((2**fracbits)-1000)):
#if it is not near the largest number simply convert divide to move decimal place
real = array[i:i+1] * (2**-fracbits)
else:
#else we subtract comp so that we get the negative number this binary string was supposed to represent.
# print(np.binary_repr(np.uint16(array[i:i+1])))
real = double(array[i:i+1]) - comp

#then we divide it to move the decimal point properly
real = real * (2**-fracbits)

#same for the next number in the array which is the imaginary component
if(array[i+1:i+2] < ((2**fracbits)-2000)):
imag = array[i+1:i+2] * (2**-fracbits)
else:
imag = double(array[i+1:i+2]) - comp
imag = imag * (2**-fracbits)

result.append(np.complex(real,imag))
i+=2
return result

最佳答案

从 Python 程序员的角度来看,真正深入研究数据类型与 Python 本身的性质背道而驰。 python is dynamically typed ,这意味着缺乏效率,但易于编程。为了解决这个问题,many popular libraries are written in c ,因此您可能需要查看类似 numpy 的库得到你的打字修复。 Here is an example of setting datatypes in numpy .但据我所知,这些仅适用于预定义的 c 类型
理论上,您可以定义一个特殊的类来包含您的数据,实现 __add__ , __subtract__ ,以及任何其他必要的关键功能。但是,由于 python 是动态类型的,这实际上可能具有有限的返回。
另一种选择可能是 Cython ,它允许你在 python 中定义 C 类型,但是如果你只是想要一个快速的函数来定义一个类型,那么 Python 的底层本质正在与你作对。

关于python - python中的Matlab numerictype/reinterpretcast等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63293463/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com