在 Python 中确定 wave 文件中允许的最大样本值
Determine maximum allowed sample value in wave file in Python
scipy.io.wavfile.read
returns 数据为整数。要进行音频处理,我想将它们转换为浮点数。我想在输入文件中允许任意位深度,因此我需要知道用于规范化整数数据的正确数字。例如,对于 16 位 wav
文件,我会将整数数组除以 ( 2**15 - 1 )
。但是对于 8 位音频,我必须除以 ( 2**7 - 1 )
有没有简单的方法来获得这个位深度,以便我可以将所有这些文件转换成可比较的浮点数据?我觉得这很基本,但我似乎找不到它。
出于一些原因,至少在目前,我不能安装更多功能包,例如 scikits
。
您可以使用 numpy.iinfo
获取有关整数数据类型的信息。例如,这是一个 16 位有符号整数数组:
In [338]: data = np.array([10, 20, -30], dtype=np.int16)
In [339]: ii = np.iinfo(data.dtype)
In [340]: ii.max
Out[340]: 32767
In [341]: ii.min
Out[341]: -32768
In [342]: ii.bits
Out[342]: 16
这是一个 8 位无符号整数数组:
In [350]: data = np.array([10, 20, 200, 5], dtype=np.uint8)
In [351]: ii = np.iinfo(data.dtype)
In [352]: ii.max
Out[352]: 255
In [353]: ii.min
Out[353]: 0
In [354]: ii.bits
Out[354]: 8
我已经写了一个 tutorial on this and a helper function called pcm2float() 可以满足您的需求。
给定 NumPy 数组中的信号 sig
和目标数据类型 dtype
,它只是这样做:
sig.astype(dtype) / dtype.type(-np.iinfo(sig.dtype).min)
scipy.io.wavfile.read
returns 数据为整数。要进行音频处理,我想将它们转换为浮点数。我想在输入文件中允许任意位深度,因此我需要知道用于规范化整数数据的正确数字。例如,对于 16 位 wav
文件,我会将整数数组除以 ( 2**15 - 1 )
。但是对于 8 位音频,我必须除以 ( 2**7 - 1 )
有没有简单的方法来获得这个位深度,以便我可以将所有这些文件转换成可比较的浮点数据?我觉得这很基本,但我似乎找不到它。
出于一些原因,至少在目前,我不能安装更多功能包,例如 scikits
。
您可以使用 numpy.iinfo
获取有关整数数据类型的信息。例如,这是一个 16 位有符号整数数组:
In [338]: data = np.array([10, 20, -30], dtype=np.int16)
In [339]: ii = np.iinfo(data.dtype)
In [340]: ii.max
Out[340]: 32767
In [341]: ii.min
Out[341]: -32768
In [342]: ii.bits
Out[342]: 16
这是一个 8 位无符号整数数组:
In [350]: data = np.array([10, 20, 200, 5], dtype=np.uint8)
In [351]: ii = np.iinfo(data.dtype)
In [352]: ii.max
Out[352]: 255
In [353]: ii.min
Out[353]: 0
In [354]: ii.bits
Out[354]: 8
我已经写了一个 tutorial on this and a helper function called pcm2float() 可以满足您的需求。
给定 NumPy 数组中的信号 sig
和目标数据类型 dtype
,它只是这样做:
sig.astype(dtype) / dtype.type(-np.iinfo(sig.dtype).min)