Matlab的numerictype/reinterpretcast等效于python? [英] Matlab numerictype/reinterpretcast equivalent in python?

查看:81
本文介绍了Matlab的numerictype/reinterpretcast等效于python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Matlab there is a command to define a new numeric type for example:

numerictype(0,16,8) 

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

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

Is there an equivalent in numpy or another library? Can I create my own dtype with a similar command?

由于有人问我更多信息,因此这里提供了有关定点数字类型如何在matlab中工作的参考:

Since I was asked for more info here is a reference on how fixed point numeric types work in matlab: https://www.mathworks.com/help/dsp/ug/concepts-and-terminology.html basically you set the signed/unsigned nature and then how long a word should be along with with the fraction length. So for instance in the example I gave you would have a signed number with word length 16, and fraction length 10.

根据我对结构化数组的了解,似乎类似的表示可能类似于以下内容:

From what I've read about structured arrays it seems that a similar representation might be something along the lines of:

dtype=[('signed', np.bool_), ('word', np.int16), ('frac', np.int16)]) 

我的最终目标是实现三个独立的转播声明,即:

My ultimate goal is to achieve is three separate reinterpertcast statements namely:

reinterpretcast(EVMacq,numerictype(0,16,8))
reinterpretcast(Payload16c,numerictype(1,16,16))
reinterpretcast(Payload32,numerictype(1,32,32))

如果有一种方法可以更简单地完成这些操作,那么我很乐意以其他方式进行操作.

If there is a way to do these more simply I am more than happy to do it a different way.

以下是我在评论中添加的信息的抄写:

Here is a transcription of the info I added in the comments:

mathworks.com/help/fixedpoint/ref/reinterpretcast.html 这里是来自 matlab 的 reinterpretcast 文档.从本质上讲,您传入整数或定点数字,该函数将有点移动小数点.即使二进制数据未更改,也是如此,因此变量的数值是不同的.

mathworks.com/help/fixedpoint/ref/reinterpretcast.html here is the documentation of reinterpretcast from matlab. Essentially you pass in an integer or a fixed point number and the function will sort of move the decimal point around. This makes it so even though the binary data has not changed the numeric value of the variable is different.

有时候,您可以通过常规除法在某些范围的数字上获得类似的效果,但这并不是万无一失的,这是不受欢迎的解决方案.

Occasionally you can achieve a similar effect on certain ranges of numbers by normal division however this isn't foolproof and is an undesirable solution.

我也许可以自己写些可以做到的事情,但是如果有人比我聪明,我会喜欢的.考虑到大多数MATLAB功能都包含在numpy中,我认为这也是一样.结构化数组可能是一个不错的选择,但是我不确定对它们进行强制转换的方式.

I could maybe write something myself that would do this but I would prefer it if someone smarter than me had already done it. Considering that most matlab functionality is included in numpy I figured this would be as well. Structured Arrays might be a good choice but I'm unsure exactly how casting to them works.

我现在意识到,如果有人可以告诉我如何执行与该转换完全相同的操作,我真的只想磨练一个命令,我会很高兴,因为我仍然无法弄清楚.速度不是问题,它只需要运行.

I realize now that I really just want to hone in on one single command if someone can tell me how to do something exactly equivalent to this cast I will be over joyed as I still cannot figure it out. Speed is not an issue it just needs to run.

这是命令:

reinterpretcast(Payload16c,numerictype(1,16,16))其中Payload16c是由 np.complex(real,imag)定义的复数数组.预先谢谢你.

reinterpretcast(Payload16c,numerictype(1,16,16)) where Payload16c is an array of complex numbers defined by np.complex(real,imag). Thank you in advance.

我尝试了类似的方法,但是它没有用,但可能在正确的轨道上.我似乎在MatLab中会遇到一些比例因子,但每次都不相同:

I tried something like this and it did not work but might be on the right track. I seem to be off by some scale factor from what would happen in MatLab but not the same scale factor every time:

    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 是动态输入的,这意味着缺乏效率,但易于编程.为了解决这个问题,许多流行的库都是用c编写的,因此您可能希望使用 numpy 之类的库来获取输入修正.此处是在numpy中设置数据类型的示例.但是据我所知,这些仅对预定义的c类型起作用

From a Python programmers perspective, getting really in the weeds with datatypes is antithetical to the nature of python itself. python is dynamically typed, which implies lack of efficiency, but ease of program-ability. To get around this, many popular libraries are written in c, so you may want to look to libraries like numpy to get your typing fix. Here is an example of setting datatypes in numpy. But to my knowledge, these only function on pre-defined c types

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

theoretically, you might be able to define a special class to contain your data, implementing __add__, __subtract__, and whatever other key functions are necessary. However, as python is dynamically typed, this may have limited returns practically.

另一个选择可能是 Cython ,该选项可以您可以在python中定义C类型,但是如果您只想使用一个快速的函数来定义类型,那么Python的本质就在与您抗争.

One more option might be Cython, which allows you to define C types in python, but if you just want a quick function to define a type, the underlying nature of Python is fighting against you.

这篇关于Matlab的numerictype/reinterpretcast等效于python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆