猿问

在python中使用scipy和librosa读取wav文件

我正在尝试.wav使用 scipy 文件夹在 Python 中加载文件。我的最终目标是创建该音频文件的频谱图。读取文件的代码可以总结如下:


import scipy.io.wavfile as wav

(sig, rate) = wav.read(_wav_file_)

对于某些.wav文件,我收到以下错误:


WavFileWarning:无法理解块(非数据),跳过它。WavFileWarning) ** ValueError: 不完整的 wav 块。


因此,我决定使用 librosa 使用以下命令读取文件:


import librosa

(sig, rate) = librosa.load(_wav_file_, sr=None)

这适用于所有情况,但是,我注意到光谱图的颜色有所不同。然而,虽然它是完全相同的数字,但不知何故颜色颠倒了。更具体地说,我注意到当保持相同的功能来计算规格并仅改变我阅读的方式时,.wav就会出现这种差异。知道什么可以产生那种东西吗?这两种方法读取.wav文件的方式之间是否存在默认差异?


编辑:


(rate1, sig1) = wav.read(spec_file) # rate1 = 16000

sig, rate = librosa.load(spec_file) # rate 22050

sig = np.array(α*sig, dtype = "int16") 

几乎有效的方法是将 sig 的结果与常数αalpha 相乘,该 alpha 是来自 scipy wavread 的信号的最大值与来自 librosa 的信号之间的比例。尽管信号速率不同。


陪伴而非守候
浏览 991回答 3
3回答

慕姐4208626

这听起来像是一个量化问题。如果波形文件中的样本存储为 asfloat并且 librosa 只是对 an 执行直接转换int,则小于 1 的值将被截断为 0。很有可能,这就是为什么sig是全零数组的原因。在float必须被缩放以将其映射到的范围int。例如,>>> a = sp.randn(10)>>> aarray([-0.04250369,  0.244113  ,  0.64479281, -0.3665814 , -0.2836227 ,       -0.27808428, -0.07668698, -1.3104602 ,  0.95253315, -0.56778205])将 a 转换为类型int而不缩放>>> a.astype(int)array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])将 a 转换int为 16 位整数的缩放比例>>> b = (a* 32767).astype(int)>>> barray([ -1392,   7998,  21127, -12011,  -9293,  -9111,  -2512, -42939,        31211, -18604])转换缩放int回float>>> c = b/32767.0>>> carray([-0.04248177,  0.24408704,  0.64476455, -0.36655782, -0.28360851,       -0.27805414, -0.0766625 , -1.31043428,  0.9525132 , -0.56776635])cb由于量化为,并且仅等于大约 3 或 4 个小数位int。如果 librosa 返回 a float,您可以按比例缩放2**15并将其转换为 anint以获得与 scipy wave reader 返回的相同范围的值。由于 librosa 正在返回 a float,因此值可能会位于一个小得多的范围内,例如[-1, +1],而不是将在[-32768, +32767]. 所以你需要缩放一个以获得匹配的范围。例如,sig, rate = librosa.load(spec_file, mono=True)sig = sig × 32767

HUH函数

补充一下,Librosa 有一个实用程序可以将整数数组转换为浮点数。float_audio = librosa.util.buf_to_float(sig)在生成 Pydub 音频段的频谱图时,我使用它取得了巨大的成功。请记住,它的参数之一是每个样本的字节数。它默认为 2。您可以在此处的文档中了解更多信息。这是源代码:def buf_to_float(x, n_bytes=2, dtype=np.float32):&nbsp; &nbsp; """Convert an integer buffer to floating point values.&nbsp; &nbsp; This is primarily useful when loading integer-valued wav data&nbsp; &nbsp; into numpy arrays.&nbsp; &nbsp; See Also&nbsp; &nbsp; --------&nbsp; &nbsp; buf_to_float&nbsp; &nbsp; Parameters&nbsp; &nbsp; ----------&nbsp; &nbsp; x : np.ndarray [dtype=int]&nbsp; &nbsp; &nbsp; &nbsp; The integer-valued data buffer&nbsp; &nbsp; n_bytes : int [1, 2, 4]&nbsp; &nbsp; &nbsp; &nbsp; The number of bytes per sample in `x`&nbsp; &nbsp; dtype : numeric type&nbsp; &nbsp; &nbsp; &nbsp; The target output type (default: 32-bit float)&nbsp; &nbsp; Returns&nbsp; &nbsp; -------&nbsp; &nbsp; x_float : np.ndarray [dtype=float]&nbsp; &nbsp; &nbsp; &nbsp; The input data buffer cast to floating point&nbsp; &nbsp; """&nbsp; &nbsp; # Invert the scale of the data&nbsp; &nbsp; scale = 1./float(1 << ((8 * n_bytes) - 1))&nbsp; &nbsp; # Construct the format string&nbsp; &nbsp; fmt = '<i{:d}'.format(n_bytes)&nbsp; &nbsp; # Rescale and format the data buffer&nbsp; &nbsp; return scale * np.frombuffer(x, fmt).astype(dtype)
随时随地看视频慕课网APP

相关分类

Python
我要回答