Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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文件切碎为10ms数据_Python_Audio_Signal Processing_Wav_Nonlinear Optimization - Fatal编程技术网

Python 如何将wav文件切碎为10ms数据

Python 如何将wav文件切碎为10ms数据,python,audio,signal-processing,wav,nonlinear-optimization,Python,Audio,Signal Processing,Wav,Nonlinear Optimization,我试图将从wav检索到的数据划分为10毫秒的数据段,以便进行动态时间扭曲 import wave import contextlib data = np.zeros((1, 7000)) rate, wav_data = wavfile.read(file_path) with contextlib.closing(wave.open(file_path, 'r')) as f: frames = f.getnframes()

我试图将从wav检索到的数据划分为10毫秒的数据段,以便进行动态时间扭曲

    import wave
    import contextlib

    data = np.zeros((1, 7000))
    rate, wav_data = wavfile.read(file_path)
    with contextlib.closing(wave.open(file_path, 'r')) as f:
        frames = f.getnframes()
        rate = f.getframerate()
        duration = frames / float(rate)
现有的图书馆有这样做的吗


谢谢

如果您对后处理数据感兴趣,您可能会将其作为numpy数据使用

>>> import wave
>>> import numpy as np
>>> f = wave.open('911.wav', 'r')
>>> data = f.readframes(f.getnframes())
>>> data[:10]  # just to show it is a string of bytes
'"5AMj\x88\x97\xa6\xc0\xc9'
>>> numeric_data = np.fromstring(data, dtype=np.uint8)
>>> numeric_data
array([ 34,  53,  65, ..., 128, 128, 128], dtype=uint8)
>>> 10e-3*f.getframerate()  # how many frames per 10ms?
110.25
这不是一个整数,所以除非你要插值你的数据,否则你需要用零填充你的数据以获得110帧长的样本(在这个帧速率下大约是10毫秒)

所以现在,每一行
数组大约有10毫秒长

>>> numeric_data.shape, f.getnframes()  # there are just as many samples in the numpy array as there were frames
((186816,), 186816)
>>> padding_length = 110 - numeric_data.shape[0]%110 
>>> padded = np.hstack((numeric_data, np.zeros(padding_length)))
>>> segments = padded.reshape(-1, 110)
>>> segments
array([[  34.,   53.,   65., ...,  216.,  222.,  228.],
       [ 230.,  227.,  224., ...,   72.,   61.,   45.],
       [  34.,   33.,   32., ...,  147.,  158.,  176.],
       ..., 
       [ 128.,  128.,  128., ...,  128.,  128.,  128.],
       [ 127.,  128.,  128., ...,  128.,  129.,  129.],
       [ 129.,  129.,  128., ...,    0.,    0.,    0.]])
>>> segments.shape
(1699, 110)