Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab numerictype/reinterpretcast equivalent in python?

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

numerictype(0,16,8) 

see documentation: https://www.mathworks.com/help/fixedpoint/ref/embedded.numerictype.html

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


EDIT:

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 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.

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.


EDIT:

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.

Here is the command:

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

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
like image 315
CrawfordBenjamin Avatar asked Aug 07 '20 00:08

CrawfordBenjamin


1 Answers

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

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.

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.

like image 176
Warlax56 Avatar answered Nov 06 '22 23:11

Warlax56