You could discard the bottom 16 bits:
n=(array_int32>>16).astype(np.int16)
which will give you this:
array([ 485, 1054, 2531], dtype=int16
Answer from Mark Setchell on Stack OverflowYou could discard the bottom 16 bits:
n=(array_int32>>16).astype(np.int16)
which will give you this:
array([ 485, 1054, 2531], dtype=int16
The numbers in your array_int32 are too large to be represented with 16 bits (a signed integer value with 16 bits can only represent a maximum value of 2^16-1=32767).
Apparently, numpy just sets the resulting numbers to zero in this case.
This behavior can be modified by changing the optional casting argument of astype The documentation states
Starting in NumPy 1.9, astype method now returns an error if the string dtype to cast to is not long enough in โsafeโ casting mode to hold the max value of integer/float array that is being casted. Previously the casting was allowed even if the result was truncated.
So, an additional requirement casting='safe' will result in a TypeError, as the conversion from 32 (or 64) bits downto 16, as the maximum value of original type is too large for the new type, e.g.
import numpy as np
array_int32 = np.array([31784960, 69074944, 165871616])
array_int16 = array_int32.astype(np.int16, casting='safe')
results in
TypeError: Cannot cast array from dtype('int64') to dtype('int16') according to the rule 'safe'
Python Unsigned 16-bit Integer Array Elements - Stack Overflow
python - convert numpy int16 audio array to float32 - Stack Overflow
python - What is an efficient way to scale a int16 numpy array to a int8 numpy array - Stack Overflow
python - convert binary array to int16 - Stack Overflow
Try:
byte_array[i] = (((val << 16) >> 8) & 0xFF0000) >> 16
It assumes val is 32 bit integer between 0 and 65535
You can use a mask to benefit from numpy's vectorization (implicit loops), which will be much faster:
mask = pixel_array == paddingVal
byte_array[mask] = 0
byte_array[~mask] = np.round(255.0 * (pixel_array[~mask] - minVal) / (maxVal - minVal - 1.0))
It could also be done like this, which is cleaner because you avoid having to create byte_array beforehand:
byte_array = np.round(255.0 * (pixel_array - minVal) / (maxVal - minVal - 1.0)).astype(np.uint8)
byte_array[pixel_array == paddingVal] = 0
Edit: as Joe Kington points in a comment to the question, this trades of memory for speed.
If you need to go forward to deal with int16 dtype for something custom, I guess you'll need to use something like numpy.array like this (not checked):
import numpy as np
# read file or stream in binary mode into a bin_array list
...
# convert binary input values to numpy array with container type int16
np.array([v for v in bin_array], dtype=np.int16)
Try the PyAudio library. It's a very good library for working with all things audio and is cross platform.
The links here on the pyadio documentation and here from an example on programcreek should get you started with your problem.
SOLVED: 3 Solutions:
-
using Numpy : np.uint16()
-
Using CTypes : ctypes.c_uint16()
-
Using Bitwise : & 0xFFFF
Hi, I'm trying to convert this code to Python from Go / C. It involves declaring a UInt16 variable and run a bit shift operation. However cant seem to create a variable with this specific type. Need some advise here.
Go Code:
package main
import "fmt"
func main() {
var dx uint16 = 38629
var dy uint16 = dx << 8
fmt.Println(dy) //58624 -> Correct value
}
Python Code:
dx = 38629 dy = (dx << 8) print(dy) # 9889024 -> not the expected value print(type(dx)) # <class 'int'> print(type(dy)) # <class 'int'>
I cant seem to figure out a way to cast or similar function to get this into an Unsigned Int 16.\
Please help.