Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 同一wav文件的不同内容_Python_Audio_Numpy_Scipy_Wav - Fatal编程技术网

Python 同一wav文件的不同内容

Python 同一wav文件的不同内容,python,audio,numpy,scipy,wav,Python,Audio,Numpy,Scipy,Wav,我试图用python打开一个wav文件并检查其内容。我遵循了3种不同的方法:使用scipy.io.wavfile,使用wave,直接从文件中读取内容 方法1:使用波形模块 >>> import wave >>> import numpy as np >>> wf = wave.open('test.wav','rb') >>> f = wf.readframes(160) >>> data = np.fro

我试图用python打开一个wav文件并检查其内容。我遵循了3种不同的方法:使用scipy.io.wavfile,使用wave,直接从文件中读取内容

方法1:使用波形模块

>>> import wave
>>> import numpy as np
>>> wf = wave.open('test.wav','rb')
>>> f = wf.readframes(160)
>>> data = np.fromstring(f)
>>> print data
[  2.82596915e+103   7.66148517e+283   1.55915666e-083  -7.82893925e-042
  -2.23287375e-293  -4.58038073e+011   8.93193919e-027   1.52567878e+292
   3.88980986e+055  -3.00054045e+099  -1.41604458e-280  -7.16366832e-116
   5.91987737e-176   3.41189939e+258   3.87973542e+168  -4.11714940e+254
  -1.03609882e-226  -4.36842462e-213  -7.51023721e+282   2.32154921e+185
   2.13137319e+248   1.08450144e-203  -1.69687232e-135  -5.92443342e-274
  -1.67041051e+126   5.56090419e+077   1.73494487e+289   4.44998489e-052
  -2.26308769e-013  -3.43049660e-294  -2.02115225e-018   1.52038226e-057
   2.80968683e+288   2.21954395e+082  -9.30599671e+131  -2.27150239e-272
  -1.49931560e-139   1.28957321e-209   1.00441910e+246   6.07714540e+188]
>>> wdata = np.array(data,dtype='int32')
>>> print wdata
[-2147483648 -2147483648           0           0           0 -2147483648
           0 -2147483648 -2147483648 -2147483648           0           0                                                                                                                        
           0 -2147483648 -2147483648 -2147483648           0           0                                                                                                                        
 -2147483648 -2147483648 -2147483648           0           0           0                                                                                                                        
 -2147483648 -2147483648 -2147483648           0           0           0                                                                                                                        
           0           0 -2147483648 -2147483648 -2147483648           0                                                                                                                        
           0           0 -2147483648 -2147483648]                                   
方法2:使用scipy.io.wavfile模块。继续使用以前的方法

>>> import scipy                    
>>> from scipy.io.wavfile import read
>>> fs,wavdata=read("test.wav")
>>> print fs
11025
>>> print wavdata
[    0  7940 15384 ..., 31997 30888 27848]
方法3:直接从文件中读取

>>> print np.fromstring(open('test.wav').read(160),dtype='int32')
[ 1179011410       80036  1163280727   544501094          16       65537
       11025       22050     1048578  1635017060       80000   520355840
  1432960024  1992649059  2061532345  1622700376   784222732  -247394173
 -1218063638 -1888771989 -2094234250 -1783920464 -1034245812   -29761725
   981867849  1751864346  2090366516  1914010417  1266246103   306655331
  -728370426 -1584154406 -2049667020 -2010414313 -1475963714  -577978161
   462290033  1388787954  1973184366  2071690352]

在方法1和方法3中,我必须将数据转换为int32。在方法2中,Scipy默认会这样做。那为什么我会得到不同的输出呢

以获得相同的结果。将第一种方法中的
fromstring
修改为

if w.getsampwidth() == 1:
    data = np.fromstring(f, dtype=np.int8)
elif w.getsampwidth() == 2:
    data = np.fromstring(f, dtype=np.int16)
elif w.getsampwidth() == 4:
    data = np.fromstring(f, dtype=np.int32)
其中
w.getsampwidth()
具有用于表示样本的字节数

np.fromstring
将字节读取为
float64
,然后通过
astype('int32')
将值(不是字节)转换为
int32
。因此,您需要读取
int8、16和32之间对应的
type
字节

第二种方法没有问题

wave文件(可能是RIFF格式)有44个字节的头。所以最后一个不能正常工作