本文整理汇总了Python中numpy.int16方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.int16方法的具体用法?Python numpy.int16怎么用?Python numpy.int16使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.int16方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: wavefile_to_waveform
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def wavefile_to_waveform(wav_file, features_type):
data, sr = sf.read(wav_file)
if features_type == 'vggish':
tmp_name = str(int(np.random.rand(1)*1000000)) + '.wav'
sf.write(tmp_name, data, sr, subtype='PCM_16')
sr, wav_data = wavfile.read(tmp_name)
os.remove(tmp_name)
# sr, wav_data = wavfile.read(wav_file) # as done in VGGish Audioset
assert wav_data.dtype == np.int16, 'Bad sample type: %r' % wav_data.dtype
data = wav_data / 32768.0 # Convert to [-1.0, +1.0]
# at least one second of samples, if not repead-pad
src_repeat = data
while (src_repeat.shape[0] < sr):
src_repeat = np.concatenate((src_repeat, data), axis=0)
data = src_repeat[:sr]
return data, sr
示例2: frompointer
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def frompointer(pointer, count, dtype=float):
'''Interpret a buffer that the pointer refers to as a 1-dimensional array.
Args:
pointer : int or ctypes pointer
address of a buffer
count : int
Number of items to read.
dtype : data-type, optional
Data-type of the returned array; default: float.
Examples:
>>> s = numpy.ones(3, dtype=numpy.int32)
>>> ptr = s.ctypes.data
>>> frompointer(ptr, count=6, dtype=numpy.int16)
[1, 0, 1, 0, 1, 0]
'''
dtype = numpy.dtype(dtype)
count *= dtype.itemsize
buf = (ctypes.c_char * count).from_address(pointer)
a = numpy.ndarray(count, dtype=numpy.int8, buffer=buf)
return a.view(dtype)
示例3: test_no_offset_scale
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def test_no_offset_scale():
# Specific tests of no-offset scaling
SAW = SlopeArrayWriter
# Floating point
for data in ((-128, 127),
(-128, 126),
(-128, -127),
(-128, 0),
(-128, -1),
(126, 127),
(-127, 127)):
aw = SAW(np.array(data, dtype=np.float32), np.int8)
assert_equal(aw.slope, 1.0)
aw = SAW(np.array([-126, 127 * 2.0], dtype=np.float32), np.int8)
assert_equal(aw.slope, 2)
aw = SAW(np.array([-128 * 2.0, 127], dtype=np.float32), np.int8)
assert_equal(aw.slope, 2)
# Test that nasty abs behavior does not upset us
n = -2**15
aw = SAW(np.array([n, n], dtype=np.int16), np.uint8)
assert_array_almost_equal(aw.slope, n / 255.0, 5)
示例4: test_writer_maker
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def test_writer_maker():
arr = np.arange(10, dtype=np.float64)
aw = make_array_writer(arr, np.float64)
assert_true(isinstance(aw, SlopeInterArrayWriter))
aw = make_array_writer(arr, np.float64, True, True)
assert_true(isinstance(aw, SlopeInterArrayWriter))
aw = make_array_writer(arr, np.float64, True, False)
assert_true(isinstance(aw, SlopeArrayWriter))
aw = make_array_writer(arr, np.float64, False, False)
assert_true(isinstance(aw, ArrayWriter))
assert_raises(ValueError, make_array_writer, arr, np.float64, False)
assert_raises(ValueError, make_array_writer, arr, np.float64, False, True)
# Does calc_scale get run by default?
aw = make_array_writer(arr, np.int16, calc_scale=False)
assert_equal((aw.slope, aw.inter), (1, 0))
aw.calc_scale()
slope, inter = aw.slope, aw.inter
assert_false((slope, inter) == (1, 0))
# Should run by default
aw = make_array_writer(arr, np.int16)
assert_equal((aw.slope, aw.inter), (slope, inter))
aw = make_array_writer(arr, np.int16, calc_scale=True)
assert_equal((aw.slope, aw.inter), (slope, inter))
示例5: test_able_int_type
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def test_able_int_type():
# The integer type cabable of containing values
for vals, exp_out in (
([0, 1], np.uint8),
([0, 255], np.uint8),
([-1, 1], np.int8),
([0, 256], np.uint16),
([-1, 128], np.int16),
([0.1, 1], None),
([0, 2**16], np.uint32),
([-1, 2**15], np.int32),
([0, 2**32], np.uint64),
([-1, 2**31], np.int64),
([-1, 2**64-1], None),
([0, 2**64-1], np.uint64),
([0, 2**64], None)):
assert_equal(able_int_type(vals), exp_out)
示例6: test_isolation
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def test_isolation(self):
# Test image isolated from external changes to header and affine
img_klass = self.image_class
arr = np.arange(3, dtype=np.int16)
aff = np.eye(4)
img = img_klass(arr, aff)
assert_array_equal(img.get_affine(), aff)
aff[0,0] = 99
assert_false(np.all(img.get_affine() == aff))
# header, created by image creation
ihdr = img.get_header()
# Pass it back in
img = img_klass(arr, aff, ihdr)
# Check modifying header outside does not modify image
ihdr.set_zooms((4,))
assert_not_equal(img.get_header(), ihdr)
示例7: test_calculate_scale
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def test_calculate_scale():
# Test for special cases in scale calculation
npa = np.array
# Here the offset handles it
res = calculate_scale(npa([-2, -1], dtype=np.int8), np.uint8, True)
assert_equal(res, (1.0, -2.0, None, None))
# Not having offset not a problem obviously
res = calculate_scale(npa([-2, -1], dtype=np.int8), np.uint8, 0)
assert_equal(res, (-1.0, 0.0, None, None))
# Case where offset handles scaling
res = calculate_scale(npa([-1, 1], dtype=np.int8), np.uint8, 1)
assert_equal(res, (1.0, -1.0, None, None))
# Can't work for no offset case
assert_raises(ValueError,
calculate_scale, npa([-1, 1], dtype=np.int8), np.uint8, 0)
# Offset trick can't work when max is out of range
res = calculate_scale(npa([-1, 255], dtype=np.int16), np.uint8, 1)
assert_not_equal(res, (1.0, -1.0, None, None))
示例8: test_can_cast
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def test_can_cast():
tests = ((np.float32, np.float32, True, True, True),
(np.float64, np.float32, True, True, True),
(np.complex128, np.float32, False, False, False),
(np.float32, np.complex128, True, True, True),
(np.float32, np.uint8, False, True, True),
(np.uint32, np.complex128, True, True, True),
(np.int64, np.float32, True, True, True),
(np.complex128, np.int16, False, False, False),
(np.float32, np.int16, False, True, True),
(np.uint8, np.int16, True, True, True),
(np.uint16, np.int16, False, True, True),
(np.int16, np.uint16, False, False, True),
(np.int8, np.uint16, False, False, True),
(np.uint16, np.uint8, False, True, True),
)
for intype, outtype, def_res, scale_res, all_res in tests:
assert_equal(def_res, can_cast(intype, outtype))
assert_equal(scale_res, can_cast(intype, outtype, False, True))
assert_equal(all_res, can_cast(intype, outtype, True, True))
示例9: test_nifti1_init
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def test_nifti1_init():
bio = BytesIO()
shape = (2,3,4)
hdr = Nifti1Header()
arr = np.arange(24, dtype=np.int16).reshape(shape)
write_raw_data(arr, hdr, bio)
hdr.set_slope_inter(2, 10)
ap = ArrayProxy(bio, hdr)
assert_true(ap.file_like == bio)
assert_equal(ap.shape, shape)
# Check there has been a copy of the header
assert_false(ap.header is hdr)
# Get the data
assert_array_equal(np.asarray(ap), arr * 2.0 + 10)
with InTemporaryDirectory():
f = open('test.nii', 'wb')
write_raw_data(arr, hdr, f)
f.close()
ap = ArrayProxy('test.nii', hdr)
assert_true(ap.file_like == 'test.nii')
assert_equal(ap.shape, shape)
assert_array_equal(np.asarray(ap), arr * 2.0 + 10)
示例10: testCreateBaseType
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def testCreateBaseType(self):
dt = hdf5dtype.createDataType('H5T_STD_U32BE')
self.assertEqual(dt.name, 'uint32')
self.assertEqual(dt.byteorder, '>')
self.assertEqual(dt.kind, 'u')
dt = hdf5dtype.createDataType('H5T_STD_I16LE')
self.assertEqual(dt.name, 'int16')
self.assertEqual(dt.kind, 'i')
dt = hdf5dtype.createDataType('H5T_IEEE_F64LE')
self.assertEqual(dt.name, 'float64')
self.assertEqual(dt.kind, 'f')
dt = hdf5dtype.createDataType('H5T_IEEE_F32LE')
self.assertEqual(dt.name, 'float32')
self.assertEqual(dt.kind, 'f')
typeItem = { 'class': 'H5T_INTEGER', 'base': 'H5T_STD_I32BE' }
typeSize = hdf5dtype.getItemSize(typeItem)
dt = hdf5dtype.createDataType(typeItem)
self.assertEqual(dt.name, 'int32')
self.assertEqual(dt.kind, 'i')
self.assertEqual(typeSize, 4)
示例11: synthesize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def synthesize(frames,filename,stride,sr=16000,deemph=0,ymax=0.98,normalize=False):
# Generate stream
y=torch.zeros((len(frames)-1)*stride+len(frames[0]))
for i,x in enumerate(frames):
y[i*stride:i*stride+len(x)]+=x
# To numpy & deemph
y=y.numpy().astype(np.float32)
if deemph>0:
y=deemphasis(y,alpha=deemph)
# Normalize
if normalize:
y-=np.mean(y)
mx=np.max(np.abs(y))
if mx>0:
y*=ymax/mx
else:
y=np.clip(y,-ymax,ymax)
# To 16 bit & save
wavfile.write(filename,sr,np.array(y*32767,dtype=np.int16))
return y
########################################################################################################################
示例12: save_wav
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def save_wav(audio, output_wav_file):
wav.write(output_wav_file, 16000, np.array(np.clip(np.round(audio), -2**15, 2**15-1), dtype=np.int16))
print('output dB', db(audio))
示例13: _convert
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def _convert(self, vals):
res = {}
for k, v in vals.items():
if isinstance(v, (np.int, np.int8, np.int16, np.int32, np.int64)):
v = int(v)
elif isinstance(v, (np.float, np.float16, np.float32, np.float64)):
v = float(v)
elif isinstance(v, Labels):
v = list(v)
elif isinstance(v, np.ndarray):
v = v.tolist()
elif isinstance(v, dict):
v = self._convert(v)
res[k] = v
return res
示例14: _toscalar
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def _toscalar(v):
if isinstance(v, (np.float16, np.float32, np.float64,
np.uint8, np.uint16, np.uint32, np.uint64,
np.int8, np.int16, np.int32, np.int64)):
return np.asscalar(v)
else:
return v
示例15: convert
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int16 [as 别名]
def convert(self, complex_iq):
intlv = self._interleave(complex_iq)
clipped = self._clip(intlv, limit=1.0)
converted = 2047. * clipped
bladerf_out = converted.astype(np.int16)
return bladerf_out