python中的Matlab numerictype/reinterpretcast等价物?

Cra*_*min 8 python matlab numpy

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

numerictype(0,16,8) 
Run Code Online (Sandbox Code Playgroud)

请参阅文档:https : //www.mathworks.com/help/fixedpoint/ref/embedded.numerictype.html

numpy 或其他库中是否有等价物?我可以使用类似的命令创建自己的 dtype 吗?


编辑:

由于我被要求提供更多信息,这里是关于定点数字类型如何在 matlab 中工作的参考:https : //www.mathworks.com/help/dsp/ug/concepts-and-terminology.html基本上你设置了signed/无符号性质,然后一个单词应该有多长以及分数长度。因此,例如在我给您的示例中,将有一个带符号的数字,字长为 16,小数长度为 10。

从我读到的关于结构化数组的内容来看,类似的表示可能是这样的:

dtype=[('signed', np.bool_), ('word', np.int16), ('frac', np.int16)]) 
Run Code Online (Sandbox Code Playgroud)

我的最终目标是实现三个独立的 reinterpertcast 语句,即:

reinterpretcast(EVMacq,numerictype(0,16,8))
reinterpretcast(Payload16c,numerictype(1,16,16))
reinterpretcast(Payload32,numerictype(1,32,32))
Run Code Online (Sandbox Code Playgroud)

如果有一种方法可以更简单地做到这些,我非常乐意以不同的方式做到这一点。

这是我在评论中添加的信息的转录:

mathworks.com/help/fixedpoint/ref/reinterpretcast.html 这里是来自 matlab 的 reinterpretcast 的文档。本质上,您传入一个整数或一个定点数,该函数将移动小数点。这使得即使二进制数据没有改变变量的数值也是不同的。

有时,您可以通过正常除法对某些范围的数字实现类似的效果,但这并非万无一失,并且是一种不受欢迎的解决方案。

我也许可以自己写一些可以做到这一点的东西,但如果有人比我更聪明已经做到了,我会更喜欢它。考虑到大多数 matlab 功能都包含在 numpy 中,我认为这也是如此。结构化数组可能是一个不错的选择,但我不确定对它们进行转换的确切方式。


编辑:

我现在意识到,如果有人能告诉我如何做与这个演员完全相同的事情,我真的只想磨练一个命令,我会非常高兴,因为我仍然无法弄清楚。速度不是问题,它只需要运行即可。

这是命令:

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
Run Code Online (Sandbox Code Playgroud)

War*_*x56 2

从 Python 程序员的角度来看,真正深入了解数据类型与 Python 本身的本质是对立的。python是动态类型的,这意味着缺乏效率,但易于编程。为了解决这个问题,许多流行的库都是用 c 编写的,因此您可能需要使用像numpy这样的库来修复您的打字问题。这是在 numpy 中设置数据类型的示例。但据我所知,这些仅在预定义的 c 类型上起作用

理论上,您可以定义一个特殊的类来包含您的数据,实现__add____subtract__以及任何其他必要的关键功能。然而,由于 python 是动态类型的,因此实际上回报可能有限。

另一个选择可能是Cython,它允许您在 python 中定义 C 类型,但如果您只是想要一个快速函数来定义类型,那么 Python 的底层本质正在与您作对。