2

I read a binary array from a digital scope, but then I can't do anything with the binary array. in Matlab this is a simple job: Data is the Array ( 2 bytes per sample, binary format) it can be converted to 16bit integer with this instruction

 Data = typecast(uint8(Data),'int16'); %convert data to int16

and then to float using

 Data = double(Data); %covert data to double to be able to perform math

In Python i have the same array called dataIn , read from an instrument. I cannot find a way to convert the binary array into a 16 bit array of integers, and then to a vector of real values.

If anyone has solved this issue I would appreciate some help. Thanks

1 Answer 1

3

If you're using numpy, this is pretty easy:

data = numpy.frombuffer(bytes_data, dtype=numpy.uint16)
data = numpy.array(data, dtype=float)
Sign up to request clarification or add additional context in comments.

4 Comments

never mind...I have seen the solution posted while i was writing. Thank you so much, it works just as expected. I was looking in the wrong section (struc and array conversion). I will now read the numpy related documentation.
in Python 3.7 I was getting an error, and had to modify the command
In the modern world, I think the numpy API has changed to push people to use np.frombuffer instead of np.fromstring -- I've updated the answer to reflect that.
Yes, this is the case....Numpy update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.